[英]Porting Python2 script to Python3 - struct library
I'm working on a socks5 proxy server and now I'm implementing a ICMP tunneling method to to tunnel SSH traffic through the ICMP protocol to bypass firewall restrictions.我正在使用socks5代理服务器,现在我正在实施一种ICMP隧道方法,以通过ICMP协议对SSH流量进行隧道传输以绕过防火墙限制。
This is the open source I used: https://github.com/sanecz/pingtunnel这是我使用的开源: 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)
Spesificly this part of the code gives me trouble and i MUST run this code in python3 in order to match my Socks5 proxy and reverse port forwarding.特别是这部分代码给我带来了麻烦,我必须在 python3 中运行此代码以匹配我的 Socks5 代理和反向端口转发。
self.checksum = self._checksum(struct.pack(pack_str, *pack_args))
This line gives me an error that "Expected value is not int".这一行给了我一个“预期值不是整数”的错误。
also here:也在这里:
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
I removed the L ending at the csum = csum & 0xffffffff
lines.我删除了以csum = csum & 0xffffffff
行结尾的csum = csum & 0xffffffff
Can anyone help me port this ICMP.py script and explain?谁能帮我移植这个 ICMP.py 脚本并解释一下?
Hey guys i mannged to solve it my self after a few tries!嘿伙计们,我尝试了几次后自己解决了这个问题!
this is the edited code:这是编辑后的代码:
#!/usr/bin/python3
import socket
import struct
ICMP_ECHO = 0
ICMP_ECHO_REQUEST = 8
class ICMPPacket(object):
def __init__(self, type, code, checksum, id,
sequence, data, source_ip, dest=(None, None)):
self.type, self.code, self.checksum = type, code, checksum
self.id, self.sequence, self.data = id, sequence, data
self.dest = dest
self.source_ip = source_ip
self.length = len(self.data)
def __repr__(self):
return "<ICMP packet: type = {s.type}, code = {s.code}, " \
"data length = {length}".format(s=self, length=len(self.data))
def __str__(self):
return "Type of message: {s.type}, Code {s.code},"\
"Checksum: {s.checksum}, ID: {s.id}, Sequence: {s.sequence}, " \
"Data: {s.data}, Data length: {length}".format(s=self, length=len(self.data))
def create(self):
#print("\nEntering CREATE!!\n\n")
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)
#print("PACK STR: " + pack_str)
pack_args.append(self.data)
#print("Pack ARGS: \n", pack_args, "\n")
self.checksum = self._checksum(struct.pack(pack_str, *pack_args))
#print("CHECKSUM: ", self.checksum)
pack_args[2] = self.checksum
return struct.pack(pack_str, *pack_args)
@classmethod
def parse(cls, packet):
ip_pack_str = "BBHHHBBH4s4s"
icmp_pack_str = "!BBHHH4sH"
data = ""
ip_packet, icmp_packet = packet[:20], packet[20:] # split ip header
ip_packet = struct.unpack(ip_pack_str, ip_packet)
source_ip = ip_packet[8]
icmp_pack_len = struct.calcsize(icmp_pack_str)
packet_len = len(icmp_packet) - icmp_pack_len
if packet_len > 0:
icmp_data_str = "{}s".format(packet_len)
data = struct.unpack(icmp_data_str, icmp_packet[icmp_pack_len:])[0]
type, code, checksum, id, sequence, dest_ip, \
dest_port = struct.unpack(icmp_pack_str, icmp_packet[:icmp_pack_len])
return cls(type, code, checksum, id, sequence, data,
socket.inet_ntoa(source_ip),
(socket.inet_ntoa(dest_ip), dest_port))
@staticmethod
def _checksum(packet):
#print("Argument for checksum: !!\n",packet)
packet = packet.decode('ISO-8859-1') # edited to match python3
csum = 0
countTo = (len(packet) / 2) * 2
count = 0
while count < countTo:
thisVal = ord(packet[count+1]) * 256 + ord(packet[count])
#print("THISVAL: ", thisVal)
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 >> 16) + (csum & 0xffff)
csum = csum + (csum >> 16)
checksum = ~csum
checksum = checksum & 0xffff
checksum = checksum >> 8 | (checksum << 8 & 0xff00)
return checksum
I rencoded the packet to match the the bytes using packet = packet.decode('ISO-8859-1')
我使用packet = packet.decode('ISO-8859-1')
重新编码数据包以匹配字节
in order to match this line thisVal = ord(packet[count+1]) * 256 + ord(packet[count])
为了匹配这一行thisVal = ord(packet[count+1]) * 256 + ord(packet[count])
cause it needed a string and it received an INT instead.因为它需要一个字符串,而它却收到了一个 INT。
therefore decoding it into a string solved this issue!因此将其解码为字符串解决了这个问题!
EDIT: if you have any suggestions about a better encoding to deal with binary data of packets please do tell me.编辑:如果您对更好的编码处理数据包的二进制数据有任何建议,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.