第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將Python2腳本移植到Python3 - struct庫(kù)

將Python2腳本移植到Python3 - struct庫(kù)

Qyouu 2023-09-05 15:21:56
我正在開(kāi)發(fā)一個(gè)socks5代理服務(wù)器,現(xiàn)在我正在實(shí)現(xiàn)一種ICMP隧道方法,通過(guò)ICMP協(xié)議來(lái)傳輸SSH流量以繞過(guò)防火墻限制。這是我使用的開(kāi)源: 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)具體來(lái)說(shuō),這部分代碼給我?guī)?lái)了麻煩,我必須在 python3 中運(yùn)行此代碼才能匹配我的 Socks5 代理和反向端口轉(zhuǎn)發(fā)。self.checksum = self._checksum(struct.pack(pack_str, *pack_args))此行給我一個(gè)錯(cuò)誤“預(yù)期值不是 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 & 0xffffffffcsum = csum & 0xffffffff我刪除了行尾的 L。誰(shuí)能幫我移植這個(gè) ICMP.py 腳本并解釋一下?
查看完整描述

1 回答

?
開(kāi)心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊

嘿伙計(jì)們,經(jīng)過(guò)幾次嘗試,我終于自己解決了這個(gè)問(wèn)題!


這是編輯后的代碼:


#!/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

我對(duì)數(shù)據(jù)包進(jìn)行了重新編碼以匹配字節(jié)使用packet = packet.decode('ISO-8859-1')


為了匹配這一行thisVal = ord(packet[count+1]) * 256 + ord(packet[count])


因?yàn)樗枰粋€(gè)字符串,但它收到了一個(gè) INT。


因此將其解碼為字符串解決了這個(gè)問(wèn)題!


編輯:如果您對(duì)更好的編碼來(lái)處理數(shù)據(jù)包的二進(jìn)制數(shù)據(jù)有任何建議,請(qǐng)告訴我。


查看完整回答
反對(duì) 回復(fù) 2023-09-05
  • 1 回答
  • 0 關(guān)注
  • 112 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)