Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. int main(int argc, char** argv) {
  2.   int fd, rawfd, fdfw, ret, n;
  3.   int on=1;
  4.   struct sockaddr_in bindPort, sin;
  5.   int sinlen;
  6.   struct iphdr *hdr;
  7.   unsigned char packet[BUFSIZE];
  8.   struct in_addr addr;
  9.   int i, direction;
  10.   struct ip_mreq mreq;
  11.  
  12.   if (argc!=2) {
  13.     fprintf(stderr, "Usage: %s <port number>\n", argv[0]);
  14.     exit(1);
  15.   }
  16.   progname=argv[0];
  17.  
  18.   fprintf(stderr,"%s:Creating a socket\n",argv[0]);
  19.   /* open a divert socket */
  20.   fd=socket(AF_INET, SOCK_RAW, IPPROTO_DIVERT);
  21.  
  22.   if (fd==-1) {
  23.     fprintf(stderr,"%s:We could not open a divert socket\n",argv[0]);
  24.     exit(1);
  25.   }
  26.  
  27.   bindPort.sin_family=AF_INET;
  28.   bindPort.sin_port=htons(atol(argv[1]));
  29.   bindPort.sin_addr.s_addr=0;
  30.  
  31.   fprintf(stderr,"%s:Binding a socket\n",argv[0]);
  32.   ret=bind(fd, &bindPort, sizeof(struct sockaddr_in));
  33.  
  34.   if (ret!=0) {
  35.     close(fd);
  36.     fprintf(stderr, "%s: Error bind(): %s",argv[0],strerror(ret));
  37.     exit(2);
  38.   }
  39. #ifdef FIREWALL
  40.   /* fill in the rule first */
  41.   bzero(&fw, sizeof (struct ip_fw));
  42.   fw.fw_proto=1; /* ICMP */
  43.   fw.fw_redirpt=htons(bindPort.sin_port);
  44.   fw.fw_spts[1]=0xffff;
  45.   fw.fw_dpts[1]=0xffff;
  46.   fw.fw_outputsize=0xffff;
  47.  
  48.   /* fill in the fwuser structure */
  49.   ipfu.ipfw=fw;
  50.   memcpy(ipfu.label, fw_policy, strlen(fw_policy));
  51.  
  52.   /* fill in the fwchange structure */
  53.   ipfc.fwc_rule=ipfu;
  54.   memcpy(ipfc.fwc_label, fw_chain, strlen(fw_chain));
  55.  
  56.   /* open a socket */
  57.   if ((fw_sock=socket(AF_INET, SOCK_RAW, IPPROTO_RAW))==-1) {
  58.     fprintf(stderr, "%s: could not create a raw socket: %s\n", argv[0], strerror(errno));
  59.     exit(2);
  60.   }
  61.  
  62.   /* write a rule into it */
  63.   if (setsockopt(fw_sock, IPPROTO_IP, IP_FW_APPEND, &ipfc, sizeof(ipfc))==-1) {
  64.     fprintf(stderr, "%s could not set rule: %s\n", argv[0], strerror(errno));
  65.     exit(2);
  66.   }
  67.  
  68.   /* install signal handler to delete the rule */
  69.   signal(SIGINT, intHandler);
  70. #endif /* FIREWALL */
  71.  
  72.   printf("%s: Waiting for data...\n",argv[0]);
  73.   /* read data in */
  74.   sinlen=sizeof(struct sockaddr_in);
  75.   while(1) {
  76.     n=recvfrom(fd, packet, BUFSIZE, 0, &sin, &sinlen);
  77.     hdr=(struct iphdr*)packet;
  78.    
  79.     printf("%s: The packet looks like this:\n",argv[0]);
  80.         for( i=0; i<40; i++) {
  81.                 printf("%02x ", (int)*(packet+i));
  82.                 if (!((i+1)%16)) printf("\n");
  83.         };
  84.     printf("\n");
  85.  
  86.     addr.s_addr=hdr->saddr;
  87.     printf("%s: Source address: %s\n",argv[0], inet_ntoa(addr));
  88.     addr.s_addr=hdr->daddr;
  89.     printf("%s: Destination address: %s\n", argv[0], inet_ntoa(addr));
  90.     printf("%s: Receiving IF address: %s\n", argv[0], inet_ntoa(sin.sin_addr));
  91.     printf("%s: Protocol number: %i\n", argv[0], hdr->protocol);
  92.  
  93.     /* reinjection */
  94.  
  95. #ifdef MULTICAST
  96.    if (IN_MULTICAST((ntohl(hdr->daddr)))) {
  97.         printf("%s: Multicast address!\n", argv[0]);
  98.         addr.s_addr = hdr->saddr;
  99.         errno = 0;
  100.         if (sin.sin_addr.s_addr == 0)
  101.             printf("%s: set_interface returns %i with errno =%i\n", argv[0], setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr)), errno);
  102.     }
  103. #endif
  104.  
  105. #ifdef REINJECT
  106.    printf("%s Reinjecting DIVERT %i bytes\n", argv[0], n);
  107.    n=sendto(fd, packet, n ,0, &sin, sinlen);
  108.    printf("%s: %i bytes reinjected.\n", argv[0], n);
  109.  
  110.    if (n<=0)
  111.      printf("%s: Oops: errno = %i\n", argv[0], errno);
  112.    if (errno == EBADRQC)
  113.      printf("errno == EBADRQC\n");
  114.    if (errno == ENETUNREACH)
  115.      printf("errno == ENETUNREACH\n");
  116. #endif
  117.   }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement