[嵌入式linux] 超高速端口扫描器 syn rawsocket

[复制链接]
 楼主| sinanjj 发表于 2010-1-30 20:09 | 显示全部楼层 |阅读模式
本帖最后由 sinanjj 于 2012-5-21 00:37 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <netdb.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <netinet/tcp.h>
  14. #include <net/if.h>
  15. #include <sys/ioctl.h>

  16. unsigned long int get_host_ip ()
  17. {
  18.         int tempSock = socket(PF_INET, SOCK_DGRAM, 0);
  19.         struct ifreq ifr;
  20.         strcpy(ifr.ifr_name, "eth0");
  21.         if (ioctl(tempSock, SIOCGIFADDR, &ifr) < 0) { printf("error: ioctl\n"); exit (0); }
  22.         close (tempSock);
  23.         unsigned long int host_ip = (((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr).s_addr;
  24.         return host_ip;
  25. }

  26. long int get_random (void)
  27. {
  28.         int fd = 0;
  29.         if ((fd = open ("/dev/urandom", O_RDONLY | O_NONBLOCK)) == -1) { printf ("error: open /dev/urandom. exit\n"); exit (0); }
  30.         long int buf;
  31.         if (read (fd, (char *)&buf, 4) != 4) { printf ("error: read /dev/urandom. exit\n"); exit (0); }
  32.         if (close (fd) != 0) { printf ("error: close() fd. exit\n"); exit (0); }
  33.         return buf;
  34. }

  35. unsigned short csum (unsigned short *buf, int nwords)        /* generates header checksums */
  36. {
  37.         unsigned long sum;
  38.         for (sum = 0; nwords > 0; nwords--)
  39.                 sum += *buf++;
  40.         sum = (sum >> 16) + (sum & 0xffff);
  41.         sum += (sum >> 16);
  42.         return ~sum;
  43. }

  44. struct pseudohdr {
  45.         u_int32_t saddr;        /* source IP address */
  46.         u_int32_t daddr;        /* destination IP address */
  47.         u_int8_t res;                /* contains binary zeroes.(0) */
  48.         u_int8_t ptcl;                /* protocol. can be tcp (6), udp(17), icmp(1) */
  49.         u_int16_t len;                /* TCP/UDP packet length. (unit:byte) */
  50. };

  51. int main (int argc, char *argv[])
  52. {
  53.         if (argc != 5) { printf ("usage: %s fromip toip fromport toport\n", argv[0]); exit (0); }
  54.         unsigned long fromip, toip;
  55.         fromip = ntohl(inet_addr(argv[1])); toip = ntohl(inet_addr(argv[2]));        /* inet_aton() returns non-zero if the address is valid, zero if not. */
  56.         if ((fromip == 0) || (toip == 0) || ((toip-fromip) < 0)) { printf ("error: fromip or toip\n"); exit (0); }
  57.         unsigned short fromport, toport;
  58.         fromport = atoi (argv[3]); toport = atoi (argv[4]);
  59.         if ((fromport == 0) || (toport == 0) || ((toport-fromport) < 0)) { printf ("error: fromport or toport\n"); exit (0); }

  60.         int raw_sock_tcp;
  61.         if ((raw_sock_tcp = socket(PF_INET, SOCK_RAW, IPPROTO_TCP)) == -1) { printf("socket() failed!\nMust be root to make raw socket.\n"); exit(0); }

  62.         unsigned char packet[80]; bzero (packet, 80);
  63.         struct tcphdr *tcph = (struct tcphdr *)packet;

  64.         /* fill in the tcp header values */
  65. //        tcph->source = htons (1234);
  66.         tcph->source = htons (get_random());
  67.         tcph->dest = htons (1);
  68.         tcph->seq = get_random ();        /* in a SYN packet, the sequence is a random */
  69.         tcph->ack_seq = 0;        /* number, and the ack sequence is 0 in the 1st packet */
  70.         tcph->doff = 5;
  71.         tcph->syn = 1;        /* initial connection request */
  72.         tcph->window = htonl (65535);        /* maximum allowed window size */
  73.         tcph->check = 0;
  74.         tcph->urg_ptr = 0;

  75.         struct sockaddr_in dest; bzero (&dest, sizeof (dest));
  76.         dest.sin_family = PF_INET; dest.sin_addr.s_addr = inet_addr ("1.2.3.4"); dest.sin_port = htons (1);
  77.         unsigned long int host_ip = get_host_ip ();

  78.         int ip_i,port_i;
  79.         for(ip_i = 0; ip_i <= (toip-fromip); ip_i++) {
  80.                 for(port_i = 0; port_i <= (toport-fromport); port_i++) {
  81.                         dest.sin_addr.s_addr = htonl (fromip + ip_i); dest.sin_port = htons (fromport + port_i);
  82.                         tcph->dest = htons (fromport + port_i);

  83.                         tcph->source = htons (get_random());
  84.                         tcph->seq = get_random ();        /* in a SYN packet, the sequence is a random */

  85.                         unsigned char buf[512]; bzero (buf, 512);
  86.                         struct pseudohdr *pseudohdr;
  87.                         pseudohdr = (struct pseudohdr *)buf;
  88.                         /* full pseudohdr */
  89.                         pseudohdr->daddr = dest.sin_addr.s_addr;
  90.                         pseudohdr->saddr = host_ip;
  91.                         pseudohdr->res = 0; pseudohdr->ptcl = 0x06;
  92.                         pseudohdr->len = htons((tcph->doff)<<2);

  93.                         tcph->check = 0;
  94.                         memcpy((unsigned char *)pseudohdr+sizeof(struct pseudohdr), (unsigned char *)tcph, (tcph->doff)<<2);
  95.                         tcph->check = csum((unsigned short *)pseudohdr, (((tcph->doff)<<2) + sizeof(struct pseudohdr))>>1);

  96.                         if (sendto (raw_sock_tcp, packet,        /* the buffer containing headers and data */
  97.                                 ((tcph->doff)<<2),        /* total length of our packet */
  98.                                 0, (struct sockaddr *) &dest, sizeof (struct sockaddr)) < 0)
  99.                                 printf ("error: sendto()\n");

  100.                         fd_set readfs;        /* file descriptor set */
  101.                         FD_ZERO (&readfs); FD_SET (raw_sock_tcp, &readfs);
  102.                         struct timeval timeout = {0, 0};        /* 0s */
  103.                         while (select (raw_sock_tcp+1, &readfs, NULL, NULL, &timeout)) {
  104.                                 bzero (buf, 512);
  105.                                 int n_recv;
  106.                                 n_recv = recvfrom(raw_sock_tcp, buf, 512, 0, NULL, NULL);
  107.                                 if(n_recv < 40) { continue; }        /* 20byte ip header + 20byte tcp header */
  108.                                 if(buf [33] == 0x12) {
  109.                                         printf ("%d.%d.%d.%d %d\n", buf[12], buf[13], buf[14], buf[15],
  110.                                                 ((buf[20]<<8)&0XFF00 | buf[21]&0XFF));
  111.                                 }
  112.                         }
  113.                 }
  114.         }
  115.         return 0;
  116. }
 楼主| sinanjj 发表于 2012-5-21 00:39 | 显示全部楼层

udp_sniffer.c

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <time.h>
#include <sys/time.h>

void be_daemon (void)
{
    switch (fork()) {    // fork off the parent process
        case -1: { printf ("error: fork()\n"); exit(0); }
        case 0: break;
        default: exit (0);
    }
    umask (0);    // change the file mode mask
    setsid();    // create a new SID for the child process
    chdir ("/");    // change the current working directory
    close (STDIN_FILENO); close (STDOUT_FILENO); close (STDERR_FILENO);    // close out the standard file descriptors
}

int main()
{
    FILE * log_f;
    int raw_sock, n_rcved, i;
    unsigned char buf[65535];
    if ((raw_sock = socket(PF_INET, SOCK_RAW, IPPROTO_UDP))<0) { printf("error: socket()\n"); exit(0); }

    be_daemon ();

    for(;;) {
        n_rcved = recvfrom(raw_sock, buf, 65535, 0, NULL, NULL);
        if (n_rcved <= 0) continue;

        if ((buf[12] == 0x7f) && (buf[13]==0x00) && (buf[14] == 0x00) && (buf[15] == 0x01)) {    // filter 127.0.0.1
            continue;
        }

        log_f = fopen("/udp_log","a+"); if (log_f == NULL) { exit(0); }
        struct timeval time_now; gettimeofday(&time_now,NULL);
        struct tm *tm_ptr;
        tm_ptr = localtime(&time_now.tv_sec);
        fprintf(log_f, "time:%04d-%02d-%02d %02d:%02d:%02d:%06d source:%d.%d.%d.%d sport:%d dport:%d length:%d data:", tm_ptr->tm_year+1900,tm_ptr->tm_mon+1,tm_ptr->tm_mday,tm_ptr->tm_hour,tm_ptr->tm_min,tm_ptr->tm_sec,(int)time_now.tv_usec,buf[12],buf[13],buf[14],buf[15],(((unsigned int)buf[20])<<8) + buf[21],(((unsigned int)buf[22])<<8) + buf[23], n_rcved-28);

        for (i=0; i<(n_rcved-28); i++) fprintf(log_f, "%02x ", buf[i+28]);
        fprintf(log_f, "\n");
        fclose(log_f);
    }
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| sinanjj 发表于 2014-4-5 22:54 | 显示全部楼层
天命风流 发表于 2018-5-21 18:27 | 显示全部楼层
顶!!!!!!               
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:In God We Trust 独立的个人,体赖科学技术工具提供针对个人的产品与服务,是通向幸福的唯一道路 工程师,设计师等可以个人创业的群体,将逐步瓦解官僚体制公司,成为中国中产。(重复劳动,工厂等,将逐步机械化) seacer.co

456

主题

6300

帖子

25

粉丝
快速回复 在线客服 返回列表 返回顶部