Advertisement
Guest User

Untitled

a guest
Jan 14th, 2012
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.62 KB | None | 0 0
  1. /*
  2. Copyright (c) 2012 Joji Antony
  3. All right reserved
  4. Licensce Affero GPL v3
  5. */
  6. #include "spectranet.h"
  7.  
  8. int
  9. check_internet_connection(void)
  10. {
  11.   char *externhost = "74.125.236.81";
  12.   char *externport ="80";
  13.   char *request = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
  14.   int sock;
  15.   if( (sock = connect_to_server(externhost,externport)) < 0 ) {
  16.     return FALSE;
  17.   }
  18.   if( send_data(sock,request,strlen(request)) < 0 ) {
  19.       close(sock);
  20.       return FALSE;
  21.   }
  22.   char match_string[13],ch;
  23.   match_string[12] = '\0';
  24.   int i,count =0;
  25.   while( count < 500 ) {
  26.       count++;
  27.       i = 0;
  28.       if( read(sock,&ch,1) < 0 ) {
  29.           close(sock);
  30.           return FALSE;
  31.       }
  32.       while( i < 11 ) {
  33.           match_string[i] = match_string[i+1];
  34.           i++;
  35.       }
  36.       match_string[i] = ch;
  37.       //      printf("%s\n",match_string);
  38.       if( ! strcmp(match_string,"203.122.18.8") ) {
  39.         close(sock);
  40.         return FALSE;
  41.       }
  42.   }
  43.   return TRUE;
  44. }
  45. void
  46. connect_to_internet(char *username,char *password)
  47. {
  48.   fprintf(stderr,"Connecting to Internet\n");
  49.   char request[500];
  50.   /*
  51.     1. Make a GET / to 1.254.254.254 as though to facebook
  52.   */
  53.  
  54.   strcpy(request,"GET / HTTP/1.1\r\nHost: www.facebook.com\r\n\r\n");
  55.   int sock;
  56.   if( (sock = connect_to_server("1.254.254.254","80")) < 0 ) {
  57.     return;
  58.   }
  59.   if( send_data(sock,request,strlen(request)) < 0 ) {
  60.     return;
  61.   }
  62.  
  63.   /*
  64.     2. Take the part after "/userportal" in the META REFRESH tag in html.
  65.   */
  66.   char *match_string = "/userportal";
  67.   int i=0,len = strlen(match_string);
  68.   char ch,path[200];
  69.   while( 1 ) {
  70.  
  71.     /*
  72.        Umm. I did some obfuscation :( This is rather a little complicated.
  73.        First read one byte, if error just return.
  74.     */
  75.     if( read(sock,&ch,1) < 0 ) {  /* eh ? */
  76.       close(sock);
  77.       return;
  78.     }
  79.  
  80.     if( i >= len ) {
  81.       /* The substring "/userportal" has been matched. Just copy until you find a quote */
  82.       if( ch == '\"' ) {
  83.     close(sock);
  84.     path[i] = '\0';
  85.     break;
  86.       }
  87.       else {
  88.     path[i++] = ch;
  89.       }
  90.     }
  91.     else {
  92.       /* Try and match substring "/userportal" */
  93.       if( match_string[i] == ch )
  94.     path[i++] = ch;
  95.       else
  96.     i = 0;
  97.     }
  98.   }
  99.  
  100.   /*
  101.     3. Make a request to 203.92.63.70 with the path from the above URL
  102.   */
  103.  
  104.   sprintf(request,"GET %s HTTP/1.1\r\nHost: 203.92.63.70\r\n\r\n",path);
  105.   if( (sock = connect_to_server("203.92.63.70","80")) < 0 ) {
  106.     return;
  107.   }
  108.   if( send_data(sock,request,strlen(request)) < 0 ) {
  109.     return;
  110.   }
  111.  
  112.  
  113.   /*
  114.     4. From the response extract the cookie for and use it for subsequent requests
  115.   */
  116.   char *resp,cookie[100],*start,*stop;
  117.   strcpy(cookie,"");
  118.   while( 1 ) {
  119.     resp = recv_data(sock);
  120.  
  121.     if( (start = strstr(resp,"JSESSIONID=")) != NULL ) {
  122.       i=0;
  123.       stop = strstr(resp,";");
  124.       if( stop == NULL ) { /* eh? */
  125.         close(sock);
  126.     return;
  127.       }
  128.       while(start != stop)
  129.         cookie[i++] = *(start++);
  130.       cookie[i] = '\0';
  131.       free(resp);
  132.       resp = NULL;
  133.       close(sock);
  134.       break;
  135.     }
  136.  
  137.     if(!strcmp(resp,"Content-Type: text/html;charset=ISO-8859-1\r\n") ) {
  138.       free(resp);
  139.       resp = NULL;
  140.       close(sock);
  141.       break;
  142.     }
  143.    
  144.   }
  145.   /*
  146.     5. Now connect to 203.92.63.70 and POST to /userportal/login.do?requesturi=http%3A%2F%2Fwww.facebook.com%2F
  147.   */
  148.   strcpy(request,"");
  149.   strcat(request,"POST /userportal/login.do?requesturi=http%3A%2F%2Fwww.facebook.com%2F HTTP/1.1\r\nHost: 203.92.63.70\r\nCookie: ");
  150.   strcat(request,cookie);
  151.   strcat(request,"\r\n\r\n");
  152.   if( (sock = connect_to_server("203.92.63.70","80")) < 0 ) {
  153.     return;
  154.   }
  155.   if( send_data(sock,request,strlen(request)) < 0 ) {
  156.     return;
  157.   }
  158.  
  159.   close(sock);
  160.  
  161.   /*
  162.     6. Now connect to 203.92.63.70 and POST to /userportal/newlogin.do?phone=0 the data "type=2&username=<username>&password=<password>
  163.   */
  164.   int postlen = 26 + strlen(username) + strlen(password);
  165.   char content_len[5];
  166.   sprintf(content_len,"%d",postlen);
  167.   strcpy(request,"");
  168.   strcat(request,"POST /userportal/newlogin.do?phone=0 HTTP/1.1\r\nContent-Length: ");
  169.   strcat(request,content_len);
  170.   strcat(request,"\r\nHost: 203.92.63.70\r\nContent-Type: application/x-www-form-urlencoded\r\nReferer: http://203.92.63.70/userportal/login.do?requesturi=http%3A%2F%2Fwww.facebook.com%2F\r\nCookie: ");
  171.   strcat(request,cookie);
  172.   strcat(request,"\r\n\r\ntype=2&username=");
  173.   strcat(request,username);
  174.   strcat(request,"&password=");
  175.   strcat(request,password);
  176.   if( (sock = connect_to_server("203.92.63.70","80")) < 0 ) {
  177.     return;
  178.   }
  179.   if( send_data(sock,request,strlen(request)) < 0 ) {
  180.     return;
  181.   }
  182.  
  183.   close(sock);
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement