0

If i send the command dir, it returns just a part of the output that i get on the server side (dir on C:\windows\system32), its like the client.recv(4096)in the client code doest receive all the data the server sent as an output

Client where i send to the server the command dir:

import socket
import subprocess
import sys

REMOTE_HOST = 'xxx.xx.xx.xx.xx' # '192.168.43.82'
REMOTE_PORT = 8081 # 2222
client = socket.socket()
print("[-] Connection Initiating...")
client.connect((REMOTE_HOST, REMOTE_PORT))
client.settimeout(3)
print("[-] Connection initiated!")


while True:
    command = input()
    if command != "exit":
        sus = command.encode()
        client.send(sus)
        print("[-] command sent...")
        output = client.recv(4096)
        output2 = client.recv(4096)
        output3 = output.decode()
        output1 = output2.decode()
        print(output3)
        print(output1, end='')
    else:
        sus = command.encode()
        client.send(sus)
        client.close()
        sys.exit()

Server side, where it executes the command "dir" and sends back the whole output:

from __future__ import print_function

import sys
import os
import re
import socket

HOST = 'xxx.xx.xx.xx.xx' # '192.168.43.82'
PORT = 8081 # 2222
server = socket.socket()
server.bind((HOST, PORT))
print('[+] Server Started')
print('[+] Listening For Client Connection ...')
server.listen(1)
client, client_addr = server.accept()
print(f'[+] {client_addr} Client connected to the server')

here = os.path.dirname(os.path.abspath(__file__))
wexpectPath = os.path.dirname(here)

import wexpect

# Path of cmd executable:
cmd_exe = 'cmd'
# The prompt should be more sophisticated than just a '>'.
cmdPrompt = re.compile('[A-Z]\:.+>')

# Start the child process
p = wexpect.spawn(cmd_exe)

# Wait for prompt
p.expect(cmdPrompt, timeout = 100)

# print the texts
#print(p.before, end='')
#print(p.match.group(0), end='')
# while True:
#     

while True:
    command = client.recv(4096)
    command1 = command.decode()
    # Wait and run a command.
    p.sendline(command1)
    
    try:
        # Wait for prompt
        p.expect(cmdPrompt,timeout = 100)
        
        # print the texts
        b = p.before
        client.send(b.encode())
        v = p.match.group(0)
        client.send(v.encode())
    
    except wexpect.EOF:
        # The program has exited
        #print('The program has exied... BY!')
        client.close()
        sys.exit()
        break
Narrow
  • 5
  • 3
  • 2
    You have to call `recv()` in a loop. TCP is a stream protocol, it doesn't maintain message boundaries. – Barmar Oct 04 '21 at 20:42
  • but how am i supposed to know when to stop the loop? – Narrow Oct 05 '21 at 14:54
  • You need to design that into your application protocol. E.g. in HTTP, the header may contain a `Content-Length` header, and you stop when you read that many bytes. Other protocols use a sentinel, e.g. SMTP ends the message with a line containing just `.`. In some protocols you keep reading until the connection is closed. – Barmar Oct 05 '21 at 14:58

0 Answers0