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