|
| 1 | +from pathlib import Path |
| 2 | +import json |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ghidriff import get_parser, GhidraDiffEngine, SimpleDiff |
| 6 | + |
| 7 | +SYMBOLS_DIR = 'symbols' |
| 8 | +BINS_DIR = 'bins' |
| 9 | + |
| 10 | +@pytest.mark.forked |
| 11 | +def test_simple_diff_afd_cve_2023_21768(shared_datadir: Path): |
| 12 | + """ |
| 13 | + Tests end to end diff of CVE |
| 14 | + runs forked because each jpype jvm can only be initialized 1x |
| 15 | + """ |
| 16 | + |
| 17 | + test_name = 'cve-2023-21768-simple' |
| 18 | + output_path = shared_datadir / test_name |
| 19 | + output_path.mkdir(exist_ok=True, parents=True) |
| 20 | + symbols_path = shared_datadir / SYMBOLS_DIR |
| 21 | + bins_path = shared_datadir / BINS_DIR |
| 22 | + ghidra_project_path = output_path / 'ghidra_projects' |
| 23 | + ghidra_project_path.mkdir(exist_ok=True,parents=True) |
| 24 | + |
| 25 | + # setup bins |
| 26 | + old_bin_path = bins_path / 'afd.sys.x64.10.0.22621.1028' |
| 27 | + new_bin_path = bins_path / 'afd.sys.x64.10.0.22621.1415' |
| 28 | + |
| 29 | + # TODO figure out why these download are unreliable |
| 30 | + # for now just git clone ghidriff-test-data |
| 31 | + # old_bin_path = shared_datadir / 'afd.sys.x64.10.0.22621.1028' |
| 32 | + # old_url = 'https://msdl.microsoft.com/download/symbols/afd.sys/0C5C6994A8000/afd.sys' |
| 33 | + # new_bin_path = shared_datadir / 'afd.sys.x64.10.0.22621.1415' |
| 34 | + # new_url = 'https://msdl.microsoft.com/download/symbols/afd.sys/50989142A9000/afd.sys' |
| 35 | + |
| 36 | + # download binaries |
| 37 | + # download is unreliage |
| 38 | + # headers = get_chrome_headers() |
| 39 | + # old_bin_path.write_bytes(requests.get(old_url,headers=headers).content) |
| 40 | + # new_bin_path.write_bytes(requests.get(new_url,headers=headers).content) |
| 41 | + |
| 42 | + assert old_bin_path.exists() |
| 43 | + assert new_bin_path.exists() |
| 44 | + |
| 45 | + parser = get_parser() |
| 46 | + |
| 47 | + GhidraDiffEngine.add_ghidra_args_to_parser(parser) |
| 48 | + |
| 49 | + args = parser.parse_args([ |
| 50 | + '-s', |
| 51 | + str(symbols_path), |
| 52 | + str(old_bin_path.absolute()), |
| 53 | + str(new_bin_path.absolute()), |
| 54 | + '-p', |
| 55 | + str(ghidra_project_path.absolute()) |
| 56 | + ]) |
| 57 | + |
| 58 | + engine_log_path = output_path / parser.get_default('log_path') |
| 59 | + |
| 60 | + binary_paths = args.old + [bin for sublist in args.new for bin in sublist] |
| 61 | + |
| 62 | + binary_paths = [Path(path) for path in binary_paths] |
| 63 | + |
| 64 | + if any([not path.exists() for path in binary_paths]): |
| 65 | + missing_bins = [f'{path.name}' for path in binary_paths if not path.exists()] |
| 66 | + raise FileNotFoundError(f"Missing Bins: {' '.join(missing_bins)}") |
| 67 | + |
| 68 | + project_name = f'{args.project_name}-{binary_paths[0].name}-{binary_paths[-1].name}' |
| 69 | + |
| 70 | + DiffEngine: GhidraDiffEngine = SimpleDiff |
| 71 | + |
| 72 | + d: GhidraDiffEngine = DiffEngine(args=args, |
| 73 | + verbose=True, |
| 74 | + threaded=args.threaded, |
| 75 | + max_ram_percent=args.max_ram_percent, |
| 76 | + print_jvm_flags=args.print_flags, |
| 77 | + jvm_args=args.jvm_args, |
| 78 | + force_analysis=args.force_analysis, |
| 79 | + force_diff=args.force_diff, |
| 80 | + verbose_analysis=args.va, |
| 81 | + no_symbols=args.no_symbols, |
| 82 | + engine_log_path=engine_log_path, |
| 83 | + engine_log_level=args.log_level, |
| 84 | + engine_file_log_level=args.file_log_level, |
| 85 | + ) |
| 86 | + |
| 87 | + d.setup_project(binary_paths, args.project_location, project_name, args.symbols_path) |
| 88 | + |
| 89 | + d.analyze_project() |
| 90 | + |
| 91 | + pdiff = d.diff_bins(old_bin_path, new_bin_path) |
| 92 | + pdiff_json = json.dumps(pdiff) |
| 93 | + |
| 94 | + d.validate_diff_json(pdiff_json) |
| 95 | + |
| 96 | + diff_name = f"{old_bin_path.name}-{new_bin_path.name}_diff" |
| 97 | + |
| 98 | + d.dump_pdiff_to_path(diff_name, |
| 99 | + pdiff, |
| 100 | + output_path, |
| 101 | + side_by_side=args.side_by_side, |
| 102 | + max_section_funcs=args.max_section_funcs, |
| 103 | + md_title=args.md_title) |
| 104 | + |
| 105 | + assert len(pdiff['functions']['modified']) == 14 |
| 106 | + assert len(pdiff['functions']['added']) == 28 |
| 107 | + assert len(pdiff['functions']['deleted']) == 0 |
| 108 | + |
| 109 | + func_name = "AfdNotifyRemoveIoCompletion" |
| 110 | + assert any([func_name in func['old']['name'] or func_name in func['new']['name'] |
| 111 | + for func in pdiff['functions']['modified']]) is True |
0 commit comments