Better termination logic for early game exiting

This commit is contained in:
Timothy Baldridge 2020-02-07 05:38:20 -07:00
parent e4551115e5
commit 5782bac4a9

View File

@ -7,16 +7,18 @@ import frida
import sys import sys
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
import psutil, time, json import psutil, time, json
from pathlib import Path
known_headers = {} known_headers = {}
shutdown = False shutdown = False
def on_message(message, data): def on_message(message, data):
msg_type, msg_data = message["payload"] msg_type, msg_data = message["payload"]
if msg_type == "header": if msg_type == "header":
header, value = msg_data.split(": "); header, value = msg_data.split(": ")
if header not in known_headers: if header not in known_headers:
known_headers[header] = value; known_headers[header] = value
if msg_type == "data": if msg_type == "data":
try: try:
data = json.loads(msg_data) data = json.loads(msg_data)
@ -25,6 +27,7 @@ def on_message(message, data):
except: except:
return return
def main(target_process): def main(target_process):
session = frida.attach(target_process) session = frida.attach(target_process)
@ -64,40 +67,57 @@ def main(target_process):
""") """)
script.on('message', on_message) script.on('message', on_message)
script.load() script.load()
while not shutdown: while not shutdown and psutil.pid_exists(target_process):
time.sleep(0.5); time.sleep(0.5)
session.detach() session.detach()
sys.exit(1)
def wait_for_game(name):
def wait_for_game(started, name):
no_exe = 0
parent_path = Path(started).parent
while True: while True:
time.sleep(1); found = False
time.sleep(1)
for proc in psutil.process_iter(): for proc in psutil.process_iter():
try:
if Path(proc.exe()).parent == parent_path:
no_exe = 0
found = True
except:
pass
if proc.name() == name: if proc.name() == name:
return proc.pid; return proc.pid
if not found:
print("Not Found " + str(no_exe))
no_exe += 1
if no_exe == 3:
sys.exit(1)
def shutdown_and_print(data): def shutdown_and_print(data):
global shutdown global shutdown
output = {"body": json.dumps(data), "headers": known_headers} output = {"body": json.dumps(data), "headers": known_headers}
print(json.dumps(output)) print(json.dumps(output))
for proc in psutil.process_iter(): for proc in psutil.process_iter():
if proc.pid == pid: if proc.pid == pid:
proc.kill(); proc.kill()
break break
shutdown = True; shutdown = True
if __name__ == '__main__': if __name__ == '__main__':
start = """C:\Steam\steamapps\common\Skyrim Special Edition\SkyrimSE.exe""" start = """C:\Steam\steamapps\common\Skyrim Special Edition\SkyrimSE.exe"""
wait_for = "SkyrimSE.exe" wait_for = "SkyrimSE.exe"
if len(sys.argv) == 3: if len(sys.argv) == 3:
start = sys.argv[1]; start = sys.argv[1]
wait_for = sys.argv[2] wait_for = sys.argv[2]
target_process = Popen([start]) target_process = Popen([start])
pid = wait_for_game(wait_for); pid = wait_for_game(start, wait_for)
main(pid) main(pid)