将Python2脚本移植到Python3 - struct库

我正在开发一个socks5代理服务器,现在我正在实现一种ICMP隧道方法,通过ICMP协议来传输SSH流量以绕过防火墙限制。

这是我使用的开源: https: //github.com/sanecz/pingtunnel

    def create(self):

        pack_str = "!BBHHH4sH"

        pack_args = [self.type, self.code, 0, self.id, self.sequence,

                     socket.inet_aton(self.dest[0]), self.dest[1]]


        if self.length:

            pack_str += "{}s".format(self.length)

            pack_args.append(self.data)


        self.checksum = self._checksum(struct.pack(pack_str, *pack_args)) 

        pack_args[2] = self.checksum

        return struct.pack(pack_str, *pack_args)

具体来说,这部分代码给我带来了麻烦,我必须在 python3 中运行此代码才能匹配我的 Socks5 代理和反向端口转发。


self.checksum = self._checksum(struct.pack(pack_str, *pack_args))


此行给我一个错误“预期值不是 int”。


也在这里:


        while count < countTo:

            thisVal = ord(packet[count+1]) * 256 + ord(packet[count])

            csum = csum + thisVal

            csum = csum & 0xffffffff

            count = count + 2


        if countTo < len(packet):

            csum = csum + ord(packet[len(packet) - 1])

            csum = csum & 0xffffffff

csum = csum & 0xffffffff我删除了行尾的 L。


谁能帮我移植这个 ICMP.py 脚本并解释一下?


Qyouu
浏览 86回答 1
1回答

开心每一天1111

嘿伙计们,经过几次尝试,我终于自己解决了这个问题!这是编辑后的代码:#!/usr/bin/python3import socketimport structICMP_ECHO = 0ICMP_ECHO_REQUEST = 8class ICMPPacket(object):&nbsp; &nbsp; def __init__(self, type, code, checksum, id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sequence, data, source_ip, dest=(None, None)):&nbsp; &nbsp; &nbsp; &nbsp; self.type, self.code, self.checksum = type, code, checksum&nbsp; &nbsp; &nbsp; &nbsp; self.id, self.sequence, self.data = id, sequence, data&nbsp; &nbsp; &nbsp; &nbsp; self.dest = dest&nbsp; &nbsp; &nbsp; &nbsp; self.source_ip = source_ip&nbsp; &nbsp; &nbsp; &nbsp; self.length = len(self.data)&nbsp; &nbsp; def __repr__(self):&nbsp; &nbsp; &nbsp; &nbsp; return "<ICMP packet: type = {s.type}, code = {s.code}, " \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data length = {length}".format(s=self, length=len(self.data))&nbsp; &nbsp; def __str__(self):&nbsp; &nbsp; &nbsp; &nbsp; return "Type of message: {s.type}, Code {s.code},"\&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Checksum: {s.checksum}, ID: {s.id}, Sequence: {s.sequence}, " \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Data: {s.data}, Data length: {length}".format(s=self, length=len(self.data))&nbsp; &nbsp; def create(self):&nbsp; &nbsp; &nbsp; &nbsp; #print("\nEntering CREATE!!\n\n")&nbsp; &nbsp; &nbsp; &nbsp; pack_str = "!BBHHH4sH"&nbsp; &nbsp; &nbsp; &nbsp; pack_args = [self.type, self.code, 0, self.id, self.sequence,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;socket.inet_aton(self.dest[0]), self.dest[1]]&nbsp; &nbsp; &nbsp; &nbsp; if self.length:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pack_str += "{}s".format(self.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #print("PACK STR: " + pack_str)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pack_args.append(self.data)&nbsp; &nbsp; &nbsp; &nbsp; #print("Pack ARGS: \n", pack_args, "\n")&nbsp; &nbsp; &nbsp; &nbsp; self.checksum = self._checksum(struct.pack(pack_str, *pack_args))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; #print("CHECKSUM: ", self.checksum)&nbsp; &nbsp; &nbsp; &nbsp; pack_args[2] = self.checksum&nbsp; &nbsp; &nbsp; &nbsp; return struct.pack(pack_str, *pack_args)&nbsp; &nbsp; @classmethod&nbsp; &nbsp; def parse(cls, packet):&nbsp; &nbsp; &nbsp; &nbsp; ip_pack_str = "BBHHHBBH4s4s"&nbsp; &nbsp; &nbsp; &nbsp; icmp_pack_str = "!BBHHH4sH"&nbsp; &nbsp; &nbsp; &nbsp; data = ""&nbsp; &nbsp; &nbsp; &nbsp; ip_packet, icmp_packet = packet[:20], packet[20:] # split ip header&nbsp; &nbsp; &nbsp; &nbsp; ip_packet = struct.unpack(ip_pack_str, ip_packet)&nbsp; &nbsp; &nbsp; &nbsp; source_ip = ip_packet[8]&nbsp; &nbsp; &nbsp; &nbsp; icmp_pack_len = struct.calcsize(icmp_pack_str)&nbsp; &nbsp; &nbsp; &nbsp; packet_len = len(icmp_packet) - icmp_pack_len&nbsp; &nbsp; &nbsp; &nbsp; if packet_len > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; icmp_data_str = "{}s".format(packet_len)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = struct.unpack(icmp_data_str, icmp_packet[icmp_pack_len:])[0]&nbsp; &nbsp; &nbsp; &nbsp; type, code, checksum, id, sequence, dest_ip, \&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dest_port = struct.unpack(icmp_pack_str, icmp_packet[:icmp_pack_len])&nbsp; &nbsp; &nbsp; &nbsp; return cls(type, code, checksum, id, sequence, data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;socket.inet_ntoa(source_ip),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(socket.inet_ntoa(dest_ip), dest_port))&nbsp; &nbsp; @staticmethod&nbsp; &nbsp; def _checksum(packet):&nbsp; &nbsp; &nbsp; &nbsp; #print("Argument for checksum: !!\n",packet)&nbsp; &nbsp; &nbsp; &nbsp; packet = packet.decode('ISO-8859-1') # edited to match python3&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; csum = 0&nbsp; &nbsp; &nbsp; &nbsp; countTo = (len(packet) / 2) * 2&nbsp; &nbsp; &nbsp; &nbsp; count = 0&nbsp; &nbsp; &nbsp; &nbsp; while count < countTo:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; thisVal = ord(packet[count+1]) * 256 + ord(packet[count])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #print("THISVAL: ", thisVal)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csum = csum + thisVal&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csum = csum & 0xffffffff&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = count + 2&nbsp; &nbsp; &nbsp; &nbsp; if countTo < len(packet):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csum = csum + ord(packet[len(packet) - 1])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; csum = csum & 0xffffffff&nbsp; &nbsp; &nbsp; &nbsp; csum = (csum >> 16) + (csum & 0xffff)&nbsp; &nbsp; &nbsp; &nbsp; csum = csum + (csum >> 16)&nbsp; &nbsp; &nbsp; &nbsp; checksum = ~csum&nbsp; &nbsp; &nbsp; &nbsp; checksum = checksum & 0xffff&nbsp; &nbsp; &nbsp; &nbsp; checksum = checksum >> 8 | (checksum << 8 & 0xff00)&nbsp; &nbsp; &nbsp; &nbsp; return checksum我对数据包进行了重新编码以匹配字节使用packet = packet.decode('ISO-8859-1')为了匹配这一行thisVal = ord(packet[count+1]) * 256 + ord(packet[count])因为它需要一个字符串,但它收到了一个 INT。因此将其解码为字符串解决了这个问题!编辑:如果您对更好的编码来处理数据包的二进制数据有任何建议,请告诉我。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python