(script_file, is_spawn=False)
| 1089 | |
| 1090 | |
| 1091 | def execute_script(script_file, is_spawn=False): |
| 1092 | if not os.path.isfile(f"{current_identifier}/{script_file}"): |
| 1093 | warn(f"{current_identifier}/{script_file} File Not found") |
| 1094 | return |
| 1095 | online_session = None |
| 1096 | online_script = None |
| 1097 | log_fh = None |
| 1098 | saved_fd = None |
| 1099 | pipe_r = None |
| 1100 | pipe_w = None |
| 1101 | tee_thread = None |
| 1102 | use_v8 = "just_trust_me.js" in script_file |
| 1103 | try: |
| 1104 | log_filename = script_file.rsplit('.', 1)[0] + '.log' |
| 1105 | log_filepath = f"{current_identifier}/{log_filename}" |
| 1106 | log_fh = open(log_filepath, 'w', encoding='utf-8') |
| 1107 | # fd 级别重定向,捕获所有 C 层写 stdout 的输出 |
| 1108 | pipe_r, pipe_w = os.pipe() |
| 1109 | saved_fd = os.dup(1) |
| 1110 | os.dup2(pipe_w, 1) |
| 1111 | os.close(pipe_w) |
| 1112 | pipe_w = None |
| 1113 | stop_event = threading.Event() |
| 1114 | def tee_reader(): |
| 1115 | while not stop_event.is_set(): |
| 1116 | try: |
| 1117 | data = os.read(pipe_r, 8192) |
| 1118 | if not data: |
| 1119 | break |
| 1120 | os.write(saved_fd, data) |
| 1121 | log_fh.write(data.decode('utf-8', errors='replace')) |
| 1122 | log_fh.flush() |
| 1123 | except Exception: |
| 1124 | break |
| 1125 | tee_thread = threading.Thread(target=tee_reader, daemon=True) |
| 1126 | tee_thread.start() |
| 1127 | if is_spawn: |
| 1128 | online_session, online_script = spawn(f"{current_identifier}/{script_file}", use_v8) |
| 1129 | else: |
| 1130 | online_session, online_script = attach(f"{current_identifier}/{script_file}", use_v8) |
| 1131 | info(f"Frida output logging -> {log_filepath}") |
| 1132 | while online_session != None: |
| 1133 | try: |
| 1134 | with patch_stdout(): |
| 1135 | text = cmd_session.prompt("CTRL + C to stop > ", handle_sigint=True) |
| 1136 | except KeyboardInterrupt: |
| 1137 | info(f"Interrupting {script_file}") |
| 1138 | break |
| 1139 | except EOFError: |
| 1140 | warn("Exiting...") |
| 1141 | break |
| 1142 | except Exception: |
| 1143 | print(traceback.format_exc()) |
| 1144 | finally: |
| 1145 | detach(online_session, online_script) |
| 1146 | # 恢复 fd 1 |
| 1147 | if saved_fd is not None: |
| 1148 | os.dup2(saved_fd, 1) |
no test coverage detected