home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / sniffit.0.3.5 / sn_generation.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  12.5 KB  |  349 lines

  1. /* Sniffit Packet Generation File                                         */
  2. /*   - Idea/development/code:Amlan Saha                                   */
  3. /*   - Packet code/debugging: Brecht Claerhout                            */
  4.  
  5. #include "sn_config.h"
  6. #ifdef INCLUDE_INTERFACE
  7. #ifdef GENERATION
  8. #include <unistd.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>     
  11. #include "sn_curses.h"
  12. #include "sn_defines.h"
  13. #include "sn_structs.h"
  14. #include "sn_generation.h"  
  15.  
  16. extern volatile int screen_busy;
  17.  
  18. void exec_generate(struct generate_mask *generate)
  19. {
  20. WINDOW *Msg_dsp;
  21. int count=0, count_ptr, fd;
  22. char msg[80];
  23. char dummy_data[]="This Packet was fired with Sniffit!";
  24.  
  25. #ifdef DEBUG
  26.         debug_msg("Gener: Start of execution");
  27. #endif
  28.  
  29. count_ptr=(generate->pkt_no);
  30.  
  31. Msg_dsp=newwin(1,COLS,LINES-1,0);
  32. wattrset(Msg_dsp,A_BOLD);
  33. wattrset(Msg_dsp,COLOR_PAIR(WIN_COLOR_PKTCNT));
  34.  
  35. fd=open_sending();
  36. while(count<count_ptr) 
  37.     {
  38.     /* Packet firing routine here */
  39. #ifdef DEBUG
  40.         debug_msg("Gener: Transmitting Packet");
  41. #endif
  42.     transmit_UDP(fd, dummy_data,0 ,strlen(dummy_data), 
  43.                 generate->source_ip, generate->source_port,
  44.                 generate->dest_ip, generate->dest_port);
  45.  
  46. #ifdef DEBUG
  47.         debug_msg("Gener:  End");
  48. #endif
  49.     if(count==count_ptr-1)
  50.         {
  51.         sprintf(msg,"DISPATCH COMPLETE-Press ENTER");
  52.         } 
  53.     else {    
  54.         sprintf(msg,"Packet No: %d dispatched.",count+1);    
  55.          }
  56.     mvwaddstr(Msg_dsp,0,27,msg);
  57.     while(screen_busy!=0) {};
  58.     wnoutrefresh(Msg_dsp);
  59.     doupdate();
  60.     count++;
  61.     }
  62. delwin(Msg_dsp);
  63. close(fd);
  64. input_field(msg,NULL,0);
  65. forced_refresh();
  66. }
  67.  
  68. /**************************************************************************/
  69. /* Actual packet generation functions below                               */
  70. /* code stolen from Spoofit (my own ;)                                    */
  71. /*                                                                        */
  72. /* int open_sending (void)                                                */ 
  73. /*   Returns a filedescriptor to the sending socket.                      */
  74. /*   close it with close (int filedesc)                                   */
  75. /*                                                                        */ 
  76. /* void transmit_TCP (int sp_fd, char *sp_data,                           */
  77. /*                  int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen,  */
  78. /*                    char *sp_source, unsigned short sp_source_port,     */ 
  79. /*                    char *sp_dest,unsigned short sp_dest_port,          */ 
  80. /*                    unsigned long sp_seq, unsigned long sp_ack,         */ 
  81. /*                    unsigned short sp_flags)                            */ 
  82. /*   fire data away in a TCP packet                                       */
  83. /*    sp_fd         : raw socket filedesc.                                */ 
  84. /*    sp_data       : IP options (you should do the padding)              */
  85. /*                    TCP options (you should do the padding)             */
  86. /*                    data to be transmitted                              */
  87. /*                    (NULL is nothing)                                   */
  88. /*                    note that all is optional, and IP en TCP options are*/
  89. /*                    not often used.                                     */
  90. /*                    All data is put after eachother in one buffer.      */
  91. /*    sp_ipoptlen   : length of IP options (in bytes)                     */
  92. /*    sp_tcpoptlen  : length of TCP options (in bytes)                    */
  93. /*    sp_datalen    : amount of data to be transmitted (bytes)            */
  94. /*    sp_source     : spoofed host that"sends packet"                     */
  95. /*    sp_source_port: spoofed port that "sends packet"                    */
  96. /*    sp_dest       : host that should receive packet                     */
  97. /*    sp_dest_port  : port that should receive packet                     */
  98. /*    sp_seq        : sequence number of packet                           */
  99. /*    sp_ack        : ACK of packet                                       */
  100. /*    sp_flags      : flags of packet (URG,ACK,PSH,RST,SYN,FIN)           */
  101. /*                                                                        */
  102. /* void transmit_UDP (int sp_fd, char *sp_data,                           */
  103. /*                    int sp_ipoptlen, int sp_datalen,                    */
  104. /*              char *sp_source, unsigned short sp_source_port,     */
  105. /*                    char *sp_dest, unsigned short sp_dest_port)         */
  106. /*   fire data away in an UDP packet                                      */
  107. /*    sp_fd         : raw socket filedesc.                                */ 
  108. /*    sp_data       : IP options                                          */
  109. /*                    data to be transmitted                              */
  110. /*                    (NULL if none)                                      */
  111. /*    sp_ipoptlen   : length of IP options (in bytes)                     */
  112. /*    sp_datalen    : amount of data to be transmitted                    */ 
  113. /*    sp_source     : spoofed host that"sends packet"                     */
  114. /*    sp_source_port: spoofed port that "sends packet"                    */
  115. /*    sp_dest       : host that should receive packet                     */
  116. /*    sp_dest_port  : port that should receive packet                     */
  117. /*                                                                        */
  118. /**************************************************************************/
  119.  
  120. #define SP_IP_HEAD_BASE     20          /* using fixed lengths to send   */ 
  121. #define SP_TCP_HEAD_BASE     20                /* no options etc...       */ 
  122. #define SP_UDP_HEAD_BASE     8              /* Always fixed */ 
  123.  
  124.  
  125. int open_sending (void)
  126. {
  127. struct protoent *sp_proto;   
  128. int sp_fd;
  129. int dummy=1;
  130.  
  131. /* they don't come rawer */
  132. if ((sp_fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW))==-1) 
  133.         perror("Couldn't open Socket."), exit(1);
  134.  
  135. #ifdef DEBUG
  136.     printf("Raw socket ready\n");
  137. #endif
  138. return sp_fd;
  139. }
  140.  
  141. void sp_send_packet (struct sp_data_exchange *sp, unsigned char proto)
  142. {
  143. int sp_status;
  144. struct sockaddr_in sp_server;
  145. struct hostent *sp_help;
  146. int HEAD_BASE;
  147.  
  148. /* Construction of destination */
  149. bzero((char *)&sp_server, sizeof(struct sockaddr)); 
  150. sp_server.sin_family = AF_INET;
  151. sp_server.sin_addr.s_addr = sp->dest; 
  152.  
  153. /*
  154. if (sp_server.sin_addr.s_addr == (unsigned int)-1)
  155.         {  
  156.         if (!(sp_help=gethostbyname(sp->dest))) 
  157.         fprintf(stderr,"unknown host %s\n", sp->dest), exit(1);
  158.         bcopy(sp_help->h_addr, (caddr_t)&sp_server.sin_addr, sp_help->h_length);
  159.         };
  160. */
  161. switch(proto)
  162.        {
  163.     case 6: HEAD_BASE = SP_TCP_HEAD_BASE;  break;                  /* TCP */
  164.     case 17: HEAD_BASE = SP_UDP_HEAD_BASE; break;                  /* UDP */
  165.     default: exit(1); break;
  166.     };
  167. sp_status = sendto(sp->fd, (char *)(sp->buffer), sp->datalen+HEAD_BASE+SP_IP_HEAD_BASE+sp->IP_optlen, 0, 
  168.             (struct sockaddr *)&sp_server,sizeof(struct sockaddr)); 
  169. if (sp_status < 0 || sp_status != sp->datalen+HEAD_BASE+SP_IP_HEAD_BASE+sp->IP_optlen)
  170.         {
  171.         if (sp_status < 0)
  172.           perror("Sendto"), exit(1);
  173.         printf("hmm... Only transmitted %d of %d bytes.\n", sp_status, 
  174.                         sp->datalen+HEAD_BASE);
  175.         };
  176. #ifdef DEBUG
  177.     printf("Packet transmitted...\n");
  178. #endif
  179. }
  180.  
  181. void sp_fix_IP_packet (struct sp_data_exchange *sp, unsigned char proto)
  182. struct IP_header *sp_help_ip;
  183. int HEAD_BASE;
  184.  
  185. switch(proto)
  186.        {
  187.     case 6: HEAD_BASE = SP_TCP_HEAD_BASE;  break;                  /* TCP */
  188.     case 17: HEAD_BASE = SP_UDP_HEAD_BASE; break;                  /* UDP */
  189.     default: exit(1); break;
  190.     };
  191.  
  192. sp_help_ip = (struct IP_header *) (sp->buffer);
  193. sp_help_ip->verlen = (IP_VERSION << 4) | ((SP_IP_HEAD_BASE+sp->IP_optlen)/4);
  194. sp_help_ip->type = 0;
  195. sp_help_ip->length = htons(SP_IP_HEAD_BASE+HEAD_BASE+sp->datalen+sp->IP_optlen+sp->TCP_optlen);
  196. sp_help_ip->ID = htons(12545);                                  /* TEST */ 
  197. sp_help_ip->flag_offset = 0;
  198. sp_help_ip->TTL = 69;
  199. sp_help_ip->protocol = proto;
  200. sp_help_ip->source = sp->source;
  201. sp_help_ip->destination =  sp->dest;
  202. sp_help_ip->checksum=in_cksum((unsigned short *) (sp->buffer), 
  203.                         SP_IP_HEAD_BASE+sp->IP_optlen);
  204. #ifdef DEBUG
  205.     printf("IP header fixed...\n");
  206. #endif
  207. }
  208.  
  209. void sp_fix_TCP_packet (struct sp_data_exchange *sp)
  210. char sp_pseudo_ip_construct[MTU];
  211. struct TCP_header *sp_help_tcp;
  212. struct pseudo_IP_header *sp_help_pseudo;
  213. int i;
  214.  
  215. for(i=0;i<MTU;i++)
  216.   {sp_pseudo_ip_construct[i]=0;}
  217.  
  218. sp_help_tcp = (struct TCP_header *) (sp->buffer+SP_IP_HEAD_BASE+sp->IP_optlen);
  219. sp_help_pseudo = (struct pseudo_IP_header *) sp_pseudo_ip_construct;
  220.  
  221. sp_help_tcp->offset_flag = htons( (((SP_TCP_HEAD_BASE+sp->TCP_optlen)/4)<<12) | sp->flags); 
  222. sp_help_tcp->seq_nr = htonl(sp->seq);
  223. sp_help_tcp->ACK_nr = htonl(sp->ack);
  224. sp_help_tcp->source = htons(sp->source_port);
  225. sp_help_tcp->destination = htons(sp->dest_port);
  226. sp_help_tcp->window = htons(0x7c00);             /* dummy for now 'wujx' */
  227.  
  228. sp_help_pseudo->source = sp->source;
  229. sp_help_pseudo->destination =  sp->dest;
  230. sp_help_pseudo->zero_byte = 0;
  231. sp_help_pseudo->protocol = 6;
  232. sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+SP_TCP_HEAD_BASE+sp->TCP_optlen);
  233.  
  234. memcpy(sp_pseudo_ip_construct+12, sp_help_tcp, sp->TCP_optlen+sp->datalen+SP_TCP_HEAD_BASE);
  235. sp_help_tcp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct, 
  236.                   sp->datalen+12+SP_TCP_HEAD_BASE+sp->TCP_optlen);
  237. #ifdef DEBUG
  238.     printf("TCP header fixed...\n");
  239. #endif
  240. }
  241.  
  242. void transmit_TCP (int sp_fd, char *sp_data, 
  243.                int sp_ipoptlen, int sp_tcpoptlen, int sp_datalen, 
  244.                    unsigned long sp_source, unsigned short sp_source_port,
  245.                unsigned long sp_dest, unsigned short sp_dest_port,
  246.                            unsigned long sp_seq, unsigned long sp_ack, 
  247.                            unsigned short sp_flags)
  248. {
  249. char sp_buffer[1500];
  250. struct sp_data_exchange sp_struct;
  251.  
  252. bzero(sp_buffer,1500);
  253. if (sp_ipoptlen!=0) 
  254.     memcpy(sp_buffer+SP_IP_HEAD_BASE,sp_data,sp_ipoptlen);
  255.  
  256. if (sp_tcpoptlen!=0) 
  257.     memcpy(sp_buffer+SP_IP_HEAD_BASE+SP_TCP_HEAD_BASE+sp_ipoptlen,
  258.                                 sp_data+sp_ipoptlen,sp_tcpoptlen);
  259. if (sp_datalen!=0) 
  260.     memcpy(sp_buffer+SP_IP_HEAD_BASE+SP_TCP_HEAD_BASE+sp_ipoptlen+sp_tcpoptlen,
  261.             sp_data+sp_ipoptlen+sp_tcpoptlen,sp_datalen);
  262.  
  263. sp_struct.fd          = sp_fd; 
  264. sp_struct.data        = sp_data;
  265. sp_struct.datalen     = sp_datalen;
  266. sp_struct.source      = sp_source;
  267. sp_struct.source_port = sp_source_port;
  268. sp_struct.dest        = sp_dest;
  269. sp_struct.dest_port   = sp_dest_port;
  270. sp_struct.seq         = sp_seq;
  271. sp_struct.ack         = sp_ack;
  272. sp_struct.flags       = sp_flags;
  273. sp_struct.buffer      = sp_buffer;
  274. sp_struct.IP_optlen   = sp_ipoptlen;          
  275. sp_struct.TCP_optlen  = sp_tcpoptlen;          
  276.  
  277. sp_fix_TCP_packet(&sp_struct);
  278. sp_fix_IP_packet(&sp_struct, 6);
  279. sp_send_packet(&sp_struct, 6);
  280. }
  281.  
  282. void sp_fix_UDP_packet (struct sp_data_exchange *sp)
  283. char sp_pseudo_ip_construct[MTU];
  284. struct UDP_header *sp_help_udp;
  285. struct pseudo_IP_header *sp_help_pseudo;
  286. int i;
  287.  
  288. for(i=0;i<MTU;i++)
  289.   {sp_pseudo_ip_construct[i]=0;}
  290.  
  291. sp_help_udp = (struct UDP_header *) (sp->buffer+SP_IP_HEAD_BASE+sp->IP_optlen);
  292. sp_help_pseudo = (struct pseudo_IP_header *) sp_pseudo_ip_construct;
  293.  
  294. sp_help_udp->source = htons(sp->source_port);
  295. sp_help_udp->destination = htons(sp->dest_port);
  296. sp_help_udp->length =  htons(sp->datalen+SP_UDP_HEAD_BASE);
  297.  
  298. sp_help_pseudo->source = sp->source;
  299. sp_help_pseudo->destination =  sp->dest;
  300. sp_help_pseudo->zero_byte = 0;
  301. sp_help_pseudo->protocol = 17;
  302. sp_help_pseudo->TCP_UDP_len = htons(sp->datalen+SP_UDP_HEAD_BASE);
  303.  
  304. memcpy(sp_pseudo_ip_construct+12, sp_help_udp, sp->datalen+SP_UDP_HEAD_BASE);
  305. sp_help_udp->checksum=in_cksum((unsigned short *) sp_pseudo_ip_construct, 
  306.                              sp->datalen+12+SP_UDP_HEAD_BASE);
  307. #ifdef DEBUG
  308.     printf("UDP header fixed...\n");
  309. #endif
  310. }
  311.  
  312. void transmit_UDP (int sp_fd, char *sp_data, 
  313.                int sp_ipoptlen, int sp_datalen, 
  314.                    unsigned long sp_source, unsigned short sp_source_port,
  315.                unsigned long sp_dest, unsigned short sp_dest_port)
  316. {
  317. char sp_buffer[1500];
  318. struct sp_data_exchange sp_struct;
  319. bzero(sp_buffer,1500);
  320.  
  321.  
  322. if (sp_ipoptlen!=0) 
  323.     memcpy(sp_buffer+SP_IP_HEAD_BASE,sp_data,sp_ipoptlen);
  324. if (sp_data!=NULL) 
  325.     memcpy(sp_buffer+SP_IP_HEAD_BASE+SP_UDP_HEAD_BASE+sp_ipoptlen,
  326.                          sp_data+sp_ipoptlen,sp_datalen);
  327.  
  328. sp_struct.fd          = sp_fd; 
  329. sp_struct.data        = sp_data;
  330. sp_struct.datalen     = sp_datalen;
  331. sp_struct.source      = sp_source;
  332. sp_struct.source_port = sp_source_port;
  333. sp_struct.dest        = sp_dest;
  334. sp_struct.dest_port   = sp_dest_port;
  335. sp_struct.buffer      = sp_buffer;
  336. sp_struct.IP_optlen   = sp_ipoptlen;
  337. sp_struct.TCP_optlen  = 0;
  338.  
  339. sp_fix_UDP_packet(&sp_struct);
  340. sp_fix_IP_packet(&sp_struct, 17);
  341. sp_send_packet(&sp_struct, 17);
  342. }
  343.  
  344. #endif
  345. #endif
  346.