Running Flask as a subprocess under Windows

Viewed 148

I'm writing a tool in python to interact with a remote PowerShell agent (basically PowerShell Empire but for PS v1). I want to use HTTP(S) as the transport medium for commands and output between the agent and the user interface.

To that end, I've got Flask running as a subprocess and then present a simple command line in the main thread to the user. This works fine on Linux but under Windows I encounter the following error when calling start on the Process:

TypeError("can't pickle _thread.lock objects")

I believe this is related to how Windows handles threads within Processes. Can anyone suggest a way to get this working on Windows? My code is below:

from flask import Flask, request
from multiprocessing import Process, Queue
import logging

port = 8080 # TODO: set this from cmdline args

cmds = Queue()
quitting = Queue()

app = Flask(__name__)
@app.route('/', methods=['GET'])
def get_command():
    if cmds.empty():
        return ''
    else:
        return cmds.get()

@app.route('/', methods=['POST'])
def get_output():
    if request.data.decode() == "quitting":
        quitting.put(True)
    print("{} ".format(request.data.decode()),end='\n> ')
    return ''

if __name__ == "__main__":
    log = logging.getLogger('werkzeug')
    log.setLevel(logging.ERROR)

    t = Process(target=app.run, kwargs={'host': '0.0.0.0'})
    t.start()

    running = True
    while running:
        cmd = input("> ")
        if cmd == "kill" or cmd =="quit":
            print("Waiting for agent to close...")
            cmds.put("quit")
            while quitting.empty():
                pass
            t.terminate()
            t.join()
            running = False
        else:
            cmds.put(cmd)
0 Answers
Related