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.
This is the open source I used: 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.
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.
Can anyone help me port this ICMP.py script and explain?
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')
in order to match this line thisVal = ord(packet[count+1]) * 256 + ord(packet[count])
cause it needed a string and it received an INT instead.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.