Python Raw Socket使用樣本(發送TCP SYN資料包)

來源:互聯網
上載者:User
import sysimport timeimport socketimport structimport randomdef SendPacketData (Buffer = None , DestIP = "127.0.0.1" , DestPort = 0) :    """SendPacketData"""        if Buffer is None :        return False        try:        Socket = socket.socket(socket.AF_INET,socket.SOCK_RAW)        Socket.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1)        Socket.setsockopt(socket.SOL_SOCKET,socket.SO_SNDTIMEO,2000)     except:        Socket.close()        return False        try:        Socket.sendto(Buffer,0,(DestIP , DestPort))                except:        Socket.close()                return Truedef SetPacketAddress (Number = 1) :    """SetPakcetAddress"""            return [".".join(["%d" % random.randint(1 , 0xFF) for i in range(0,4)]) for n in range(0 , Number)]def SetPacketData (Length = 32) :    """SetPacketData"""    return "".join(["%s" % chr(random.randint(0 , 255)) for n in range(Length)])def SetPacketCheckSum (Buffer = None) :    """SetPacketCheckSum"""        if Buffer == None :        return False        if len(Buffer) % 2 :        Buffer += '/0'    return ~sum([(ord(Buffer[n + 1]) << 8) + ord(Buffer[n]) for n in range(0 , len(Buffer) , 2)])#or return ~sum([ord(Buffer[n]) + ord(Buffer[n + 1]) * 256  for n in range(0 , len(Buffer) , 2)])def SynSendInit (DestIP = "127.0.0.1" , DestPort = 80) :    """SynSendInit"""        IpHdrLen = 20    TcpHdrLen = 20        IpVerlen    = (4 << 4 | IpHdrLen / 4)    IpTotal_len = socket.htons(IpHdrLen + TcpHdrLen)        IpDestIP = struct.unpack('i',socket.inet_aton(DestIP))[0]    IpSourceIP = struct.unpack('i',socket.inet_aton(SetPacketAddress()[0]))[0]        TcpSport = socket.htons(random.randint(1024 , 65000))    TcpDport = socket.htons(DestPort)        TcpLenres = (TcpHdrLen / 4 << 4 | 0)    TcpSeq = socket.htonl(int(time.time()))        Buffer = struct.pack("LLBBHHHLLBBHHH",IpSourceIP,IpDestIP,0,socket.IPPROTO_TCP,socket.htons(TcpHdrLen)                                         ,TcpSport,TcpDport,TcpSeq,0,TcpLenres,2,socket.htons(8192),0,0)    TcpChecksum = SetPacketCheckSum(Buffer)        Buffer = struct.pack("BBHHHBBHLL",IpVerlen,0,IpTotal_len,1,0x40,255,socket.IPPROTO_TCP,0,IpSourceIP,IpDestIP)    IpChecksum = SetPacketCheckSum(Buffer)        tcpcsp = struct.pack("L",TcpChecksum)    ipcsp = struct.pack("L",IpChecksum)        return struct.pack("BBHHHBBHLLHHLLBBHHH",IpVerlen,0,IpTotal_len,1,0x40,255,socket.IPPROTO_TCP                                            ,(ord(ipcsp[1]) << 8) + ord(ipcsp[0]),IpSourceIP,IpDestIP                                            ,TcpSport,TcpDport,TcpSeq,0,TcpLenres,2,socket.htons(8192)                                            ,(ord(tcpcsp[1]) << 8) + ord(tcpcsp[0]),0)def SynSendMain (DestIP = "127.0.0.1" , DestPort = 80) :    """SynSendMain"""        return SendPacketData(SynSendInit(DestIP , DestPort) , DestIP , DestPort)if __name__ == "__main__" :    SynSendMain()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.