Advertisement
FlyFar

worm/duh.c

Mar 24th, 2024
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | Cybersecurity | 0 0
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <arpa/inet.h>
  4. #include <stdlib.h>
  5. #include <netdb.h>
  6. #include <string.h>
  7.  
  8. int create_tcp_socket();
  9. char *get_ip(char *host);
  10. char *build_get_query(char *host, char *page);
  11. void usage();
  12.  
  13. #define HOST "127.0.0.1"
  14. #define PAGE "/"
  15. #define PORT 80
  16. #define USERAGENT "HTMLGET 1.0"
  17.  
  18. int main(int argc, char **argv)
  19. {
  20.   struct sockaddr_in *remote;
  21.   int sock;
  22.   int tmpres;
  23.   char *ip;
  24.   char *get;
  25.   char buf[BUFSIZ+1];
  26.   char *host;
  27.   char *page;
  28.  
  29.   host = argv[1];
  30.   if(argc > 2){
  31.     page = argv[2];
  32.   }else{
  33.     page = PAGE;
  34.   }
  35.   sock = create_tcp_socket();
  36.   ip = get_ip(host);
  37.   remote = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *));
  38.   remote->sin_family = AF_INET;
  39.   tmpres = inet_pton(AF_INET, ip, (void *)(&(remote->sin_addr.s_addr)));
  40.   if( tmpres < 0)  
  41.   {
  42.     perror("Can't set remote->sin_addr.s_addr");
  43.     exit(1);
  44.   }else if(tmpres == 0)
  45.   {
  46.     fprintf(stderr, "%s is not a valid IP address\n", ip);
  47.     exit(1);
  48.   }
  49.   remote->sin_port = htons(PORT);
  50.  
  51.   if(connect(sock, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0){
  52.     perror("Could not connect");
  53.     exit(1);
  54.   }
  55.   get = build_get_query(host, page);
  56.  
  57.  
  58.   int sent = 0;
  59.   while(sent < strlen(get))
  60.   {
  61.     tmpres = send(sock, get+sent, strlen(get)-sent, 0);
  62.     if(tmpres == -1){
  63.       perror("Can't send query");
  64.       exit(1);
  65.     }
  66.     sent += tmpres;
  67.   }
  68.                    
  69.   memset(buf, 0, sizeof(buf));
  70.   int htmlstart = 0;
  71.   char * htmlcontent;
  72.   while((tmpres = recv(sock, buf, BUFSIZ, 0)) > 0){
  73.     if(htmlstart == 0)
  74.     {
  75.       htmlcontent = strstr(buf, "\r\n\r\n");
  76.       if(htmlcontent != NULL){
  77.         htmlstart = 1;
  78.         htmlcontent += 4;
  79.       }
  80.     }else{
  81.       htmlcontent = buf;
  82.     }
  83.     if(htmlstart){
  84.       fprintf(stdout, htmlcontent);
  85.     }
  86.  
  87.     memset(buf, 0, tmpres);
  88.   }
  89.   if(tmpres < 0)
  90.   {
  91.     perror("Error receiving data");
  92.   }
  93.   free(get);
  94.   free(remote);
  95.   free(ip);
  96.   close(sock);
  97.   return 0;
  98. }
  99.  
  100. int create_tcp_socket()
  101. {
  102.   int sock;
  103.   if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
  104.     perror("Can't create TCP socket");
  105.     exit(1);
  106.   }
  107.   return sock;
  108. }
  109.  
  110.  
  111. char *get_ip(char *host)
  112. {
  113.   struct hostent *hent;
  114.   int iplen = 15;
  115.   char *ip = (char *)malloc(iplen+1);
  116.   memset(ip, 0, iplen+1);
  117.   if((hent = gethostbyname(host)) == NULL)
  118.   {
  119.     herror("Can't get IP");
  120.     exit(1);
  121.   }
  122.   if(inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, iplen) == NULL)
  123.   {
  124.     perror("Can't resolve host");
  125.     exit(1);
  126.   }
  127.   return ip;
  128. }
  129.  
  130. char *build_get_query(char *host, char *page)
  131. {
  132.   char *query;
  133.   char *getpage = page;
  134.   char *tpl = "GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\n\r\n";
  135.   if(getpage[0] == '/'){
  136.     getpage = getpage + 1;
  137.   }
  138.  
  139.   query = (char *)malloc(strlen(host)+strlen(getpage)+strlen(USERAGENT)+strlen(tpl)-5);
  140.   sprintf(query, tpl, getpage, host, USERAGENT);
  141.   return query;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement