|
15 | 15 | YEAR_IN_SECONDS = 365 * 60 * 60 * 24 |
16 | 16 |
|
17 | 17 |
|
18 | | -def _create_temp_binary(content: bytes) -> tempfile.NamedTemporaryFile: |
19 | | - binary = tempfile.NamedTemporaryFile() |
20 | | - |
21 | | - binary.write(content) |
22 | | - binary.flush() |
23 | | - |
24 | | - os.chmod(binary.name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) |
25 | | - |
26 | | - return binary |
| 18 | +def _create_temp_binary(content: bytes) -> str: |
| 19 | + binary = tempfile.NamedTemporaryFile(delete=False) |
| 20 | + try: |
| 21 | + binary.write(content) |
| 22 | + binary.flush() |
| 23 | + path = binary.name |
| 24 | + finally: |
| 25 | + binary.close() |
| 26 | + os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) |
| 27 | + return path |
27 | 28 |
|
28 | 29 |
|
29 | 30 | class EService(exploit_pb2_grpc.ExploitServiceServicer): |
30 | 31 | def Exploit(self, request, _): |
31 | | - temp_file = _create_temp_binary(request.binary) |
32 | | - |
33 | | - input_stream = ZeratoolInputStreams(request.input_stream) |
34 | | - overflow_only = request.overflow_only |
35 | | - format_only = request.format_only |
36 | | - win_funcs_used = request.serialized_win_funcs.split(",") |
37 | | - if len(win_funcs_used) == 1 and win_funcs_used[0] == '': |
38 | | - win_funcs_used = None |
39 | | - |
40 | | - print("temp_file.name: ", temp_file.name) |
41 | | - print("overflow_only: ", overflow_only) |
42 | | - print("format_only: ", format_only) |
43 | | - print("win_funcs_used: ", win_funcs_used) |
44 | | - |
45 | | - result = exploit( |
46 | | - temp_file.name, |
47 | | - input_stream, |
48 | | - overflow_only=overflow_only, |
49 | | - format_only=format_only, |
50 | | - win_funcs=win_funcs_used, |
51 | | - ) |
52 | | - |
53 | | - return exploit_pb2.exploit(pickledExploit=pickle.dumps(result)) |
| 32 | + temp_path = _create_temp_binary(request.binary) |
| 33 | + try: |
| 34 | + input_stream = ZeratoolInputStreams(request.input_stream) |
| 35 | + overflow_only = request.overflow_only |
| 36 | + format_only = request.format_only |
| 37 | + win_funcs_used = request.serialized_win_funcs.split(",") |
| 38 | + if len(win_funcs_used) == 1 and win_funcs_used[0] == '': |
| 39 | + win_funcs_used = None |
| 40 | + |
| 41 | + print("temp_path: ", temp_path) |
| 42 | + print("overflow_only: ", overflow_only) |
| 43 | + print("format_only: ", format_only) |
| 44 | + print("win_funcs_used: ", win_funcs_used) |
| 45 | + |
| 46 | + result = exploit( |
| 47 | + temp_path, |
| 48 | + input_stream, |
| 49 | + overflow_only=overflow_only, |
| 50 | + format_only=format_only, |
| 51 | + win_funcs=win_funcs_used, |
| 52 | + ) |
| 53 | + |
| 54 | + return exploit_pb2.exploit(pickledExploit=pickle.dumps(result)) |
| 55 | + finally: |
| 56 | + if os.path.exists(temp_path): |
| 57 | + try: |
| 58 | + os.unlink(temp_path) |
| 59 | + except OSError: |
| 60 | + pass |
54 | 61 |
|
55 | 62 |
|
56 | 63 | def serve() -> None: |
|
0 commit comments