python伪造udp数据包
短信预约 -IT技能 免费直播动态提醒
#!/usr/bin/python
#coding:utf-8
import socket
import struct
from random import randint
def checksum(data):
s = 0
n = len(data) % 2
for i in range(0, len(data) - n, 2):
s += ord(data[i]) + (ord(data[i + 1]) << 8)
if n:
s += ord(data[i + 1])
while (s >> 16):
s = (s & 0xFFFF) + (s >> 16)
s = ~s & 0xffff
return s
class IP(object):
def __init__(self, source, destination, payload='', proto=socket.IPPROTO_TCP):
self.version = 4
self.ihl = 5 # Internet Header Length
self.tos = 0 # Type of Service
self.tl = 20 + len(payload)
self.id = 0 # random.randint(0, 65535)
self.flags = 0 # Don't fragment
self.offset = 0
self.ttl = 255
self.protocol = proto
self.checksum = 2 # will be filled by kernel
self.source = socket.inet_aton(source)
self.destination = socket.inet_aton(destination)
def pack(self):
ver_ihl = (self.version << 4) + self.ihl
flags_offset = (self.flags << 13) + self.offset
ip_header = struct.pack("!BBHHHBBH4s4s",
ver_ihl,
self.tos,
self.tl,
self.id,
flags_offset,
self.ttl,
self.protocol,
self.checksum,
self.source,
self.destination)
self.checksum = checksum(ip_header)
ip_header = struct.pack("!BBHHHBBH4s4s",
ver_ihl,
self.tos,
self.tl,
self.id,
flags_offset,
self.ttl,
self.protocol,
socket.htons(self.checksum),
self.source,
self.destination)
return ip_header
class UDP(object):
def __init__(self, class="lazy" data-src, dst, payload=''):
# def __init__(self, class="lazy" data-src, dst):
self.class="lazy" data-src = class="lazy" data-src
self.dst = dst
self.payload = payload
self.checksum = 0
self.length = 8 # UDP Header length
def pack(self, class="lazy" data-src, dst, proto=socket.IPPROTO_UDP):
length = self.length + len(self.payload)
pseudo_header = struct.pack('!4s4sBBH',
socket.inet_aton(class="lazy" data-src), socket.inet_aton(dst), 0,
proto, length)
self.checksum = checksum(pseudo_header)
packet = struct.pack('!HHHH',
self.class="lazy" data-src, self.dst, length, 0)
return packet
s = socket.socket(socket.AF_INET,
socket.SOCK_RAW,
socket.IPPROTO_RAW)
fakeclass="lazy" data-src = "10.1.1.1"
dst = "175.155.234.155"
dstport = 1900
payload = "UDP fake packet test"
# packobj = UDP(fakeclass="lazy" data-src, dst, payload)
# packet = packobj.pack(fakeclass="lazy" data-src, dst)
# s.sendto(packet, (dst, dstport))
udp = UDP(randint(1, 65535), dstport, payload).pack(fakeclass="lazy" data-src, dst)
ip = IP(fakeclass="lazy" data-src, dst, udp, proto=socket.IPPROTO_UDP).pack()
s.sendto(ip + udp + payload, (dst, dstport))
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341