raw socket

一个很newbee的例子,可惜还得改

 1 import sys
 2 import time
 3 
 4 import socket
 5 import struct
 6 
 7 import random
 8 
 9 def SendPacketData (Buffer = None , DestIP = "127.0.0.1" , DestPort = 10000) :
10     """SendPacketData"""
11     
12     if Buffer is None :
13         return False
14     
15     try:
16         Socket = socket.socket(socket.AF_INET,socket.SOCK_RAW)
17         Socket.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1)
18         Socket.setsockopt(socket.SOL_SOCKET,socket.SO_SNDTIMEO,2000)
19  
20     except:
21         Socket.close()
22         return False
23     
24     try:
25         Socket.sendto(Buffer,0,(DestIP , DestPort))
26             
27     except:
28         Socket.close()
29             
30     return True
31 
32 def SetPacketAddress (Number = 1) :
33     """SetPakcetAddress"""
34         
35     return [".".join(["%d" % random.randint(1 , 0xFF) for i in range(0,4)]) for n in range(0 , Number)]
36 
37 def SetPacketData (Length = 32) :
38     """SetPacketData"""
39 
40     return "".join(["%s" % chr(random.randint(0 , 255)) for n in range(Length)])
41 
42 def SetPacketCheckSum (Buffer = None) :
43     """SetPacketCheckSum"""
44     
45     if Buffer == None :
46         return False
47     
48     if len(Buffer) % 2 :
49         Buffer += '/0'
50 
51     return ~sum([(ord(Buffer[n + 1]) << 8) + ord(Buffer[n]) for n in range(0 , len(Buffer) , 2)])
52 #or return ~sum([ord(Buffer[n]) + ord(Buffer[n + 1]) * 256  for n in range(0 , len(Buffer) , 2)])
53 
54 def SynSendInit (DestIP = "127.0.0.1" , DestPort = 80) :
55     """SynSendInit"""
56     
57     IpHdrLen = 20
58     TcpHdrLen = 20
59     
60     IpVerlen    = (4 << 4 | IpHdrLen / 4)
61     IpTotal_len = socket.htons(IpHdrLen + TcpHdrLen)
62     
63     IpDestIP = struct.unpack('i',socket.inet_aton(DestIP))[0]
64     IpSourceIP = struct.unpack('i',socket.inet_aton(SetPacketAddress()[0]))[0]
65     
66     TcpSport = socket.htons(random.randint(1024 , 65000))
67     TcpDport = socket.htons(DestPort)
68     
69     TcpLenres = (TcpHdrLen / 4 << 4 | 0)
70     TcpSeq = socket.htonl(int(time.time()))
71     
72     Buffer = struct.pack("LLBBHHHLLBBHHH",IpSourceIP,IpDestIP,0,socket.IPPROTO_TCP,socket.htons(TcpHdrLen)
73                                          ,TcpSport,TcpDport,TcpSeq,0,TcpLenres,2,socket.htons(8192),0,0)
74     TcpChecksum = SetPacketCheckSum(Buffer)
75     
76     Buffer = struct.pack("BBHHHBBHLL",IpVerlen,0,IpTotal_len,1,0x40,255,socket.IPPROTO_TCP,0,IpSourceIP,IpDestIP)
77     IpChecksum = SetPacketCheckSum(Buffer)
78     
79     tcpcsp = struct.pack("L",TcpChecksum)
80     ipcsp = struct.pack("L",IpChecksum)
81     
82     return struct.pack("BBHHHBBHLLHHLLBBHHH",IpVerlen,0,IpTotal_len,1,0x40,255,socket.IPPROTO_TCP
83                                             ,(ord(ipcsp[1]) << 8) + ord(ipcsp[0]),IpSourceIP,IpDestIP
84                                             ,TcpSport,TcpDport,TcpSeq,0,TcpLenres,2,socket.htons(8192)
85                                             ,(ord(tcpcsp[1]) << 8) + ord(tcpcsp[0]),0)
86 
87 def SynSendMain (DestIP = "127.0.0.1" , DestPort = 80) :
88     """SynSendMain"""
89     
90     return SendPacketData(SynSendInit(DestIP , DestPort) , DestIP , DestPort)
91 
92 if __name__ == "__main__" :
93     try:
94         SynSendMain()
95     except:
96         pass

 

你可能感兴趣的:(raw socket)