44
55from collections import defaultdict
66from dataclasses import dataclass
7+ from io import BufferedReader
78from operator import itemgetter
89from pathlib import Path
9- from typing import BinaryIO , NamedTuple
10+ from typing import NamedTuple
1011
12+ import lief
1113from omegaconf import DictConfig
12- from peclasses .portable_executable import PortableExecutable
1314
14- from dfint64_patch .binio import read_section_data
1515from dfint64_patch .config import with_config
1616from dfint64_patch .cross_references .cross_references_relative import find_relative_cross_references
1717from dfint64_patch .extract_strings .from_raw_bytes import ExtractedStringInfo , extract_strings_from_raw_bytes
1818from dfint64_patch .extract_subroutines .from_raw_bytes import SubroutineInfo , extract_subroutines , which_subroutine
1919from dfint64_patch .type_aliases import Rva
2020
2121
22- def extract_strings_with_xrefs (pe_file : BinaryIO , pe : PortableExecutable ) -> dict [ExtractedStringInfo , list [Rva ]]:
23- sections = pe .section_table
24- code_section = sections [0 ]
25- string_section = sections [1 ]
22+ def extract_strings_with_xrefs (pe : lief .PE .Binary ) -> dict [ExtractedStringInfo , list [Rva ]]:
23+ code_section = pe .sections [0 ]
24+ string_section = pe .sections [1 ]
2625
2726 strings = list (
2827 extract_strings_from_raw_bytes (
29- read_section_data ( pe_file , string_section ) ,
30- base_address = string_section .virtual_address ,
28+ string_section . content ,
29+ base_address = Rva ( string_section .virtual_address ) ,
3130 ),
3231 )
3332
3433 cross_references = find_relative_cross_references (
35- read_section_data ( pe_file , code_section ) ,
36- base_address = code_section .virtual_address ,
34+ code_section . content ,
35+ base_address = Rva ( code_section .virtual_address ) ,
3736 addresses = map (itemgetter (0 ), strings ),
3837 )
3938
@@ -49,18 +48,18 @@ class StringCrossReference(NamedTuple):
4948 cross_reference : Rva
5049
5150
52- def extract_strings_grouped_by_subs (pe_file : BinaryIO ) -> dict [Rva , list [StringCrossReference ]]:
53- pe = PortableExecutable (pe_file )
54- sections = pe . section_table
55- code_section = sections [0 ]
51+ def extract_strings_grouped_by_subs (pe_file : BufferedReader ) -> dict [Rva , list [StringCrossReference ]]:
52+ pe = lief . PE . parse (pe_file )
53+ assert pe is not None
54+ code_section = pe . sections [0 ]
5655
57- image_base = pe .optional_header .image_base
56+ image_base = pe .optional_header .imagebase
5857
59- strings_with_xrefs = extract_strings_with_xrefs (pe_file , pe )
58+ strings_with_xrefs = extract_strings_with_xrefs (pe )
6059
6160 subroutines = list (
6261 extract_subroutines (
63- read_section_data ( pe_file , code_section ) ,
62+ code_section . content ,
6463 base_offset = code_section .virtual_address ,
6564 )
6665 )
0 commit comments