44
55from collections import defaultdict
66from dataclasses import dataclass
7- from io import BufferedReader
87from operator import itemgetter
98from pathlib import Path
109from typing import NamedTuple
@@ -68,13 +67,8 @@ def group_by_subroutines(
6867 return raw_result
6968
7069
71- def extract_strings_grouped_by_subs (pe_file : BufferedReader ) -> dict [Rva , list [StringCrossReference ]]:
72- pe = lief .PE .parse (pe_file )
73- assert pe is not None
70+ def extract_strings_grouped_by_subs (pe : lief .PE .Binary ) -> dict [SubroutineInfo , list [StringCrossReference ]]:
7471 code_section = pe .sections [0 ]
75-
76- image_base = pe .optional_header .imagebase
77-
7872 strings_with_xrefs = extract_strings_with_xrefs (pe )
7973
8074 subroutines = list (
@@ -89,10 +83,10 @@ def extract_strings_grouped_by_subs(pe_file: BufferedReader) -> dict[Rva, list[S
8983
9084 raw_result = group_by_subroutines (strings_with_xrefs , subroutines )
9185
92- result : dict [Rva , list [StringCrossReference ]] = {}
86+ result : dict [SubroutineInfo , list [StringCrossReference ]] = {}
9387 for subroutine , string_xrefs in sorted (raw_result .items (), key = itemgetter (0 )):
9488 sorted_xrefs = sorted (string_xrefs , key = lambda x : x .cross_reference )
95- result [Rva ( image_base + subroutine . start ) ] = sorted_xrefs
89+ result [subroutine ] = sorted_xrefs
9690
9791 return result
9892
@@ -106,8 +100,12 @@ class ExtractConfig(DictConfig):
106100@with_config (ExtractConfig , ".extract.yaml" )
107101def main (conf : ExtractConfig ) -> None :
108102 with Path (conf .file_name ).open ("rb" ) as pe_file :
109- for subroutine , strings in extract_strings_grouped_by_subs (pe_file ).items ():
110- print (f"[sub_{ subroutine :x} ]" )
103+ pe = lief .PE .parse (pe_file )
104+ assert pe is not None
105+
106+ image_base = pe .optional_header .imagebase
107+ for subroutine , strings in extract_strings_grouped_by_subs (pe ).items ():
108+ print (f"[sub_{ image_base + subroutine .start :x} ]" )
111109 for string in strings :
112110 print (string .string )
113111
0 commit comments