Skip to content

Commit b935e46

Browse files
committed
Migrate to lief
1 parent ba4a5e9 commit b935e46

File tree

7 files changed

+270
-203
lines changed

7 files changed

+270
-203
lines changed

dfint64_patch/binio.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from collections.abc import Iterable
22
from typing import BinaryIO
33

4-
from peclasses.section_table import Section
5-
64
from dfint64_patch.type_aliases import Offset
75

86

@@ -40,11 +38,3 @@ def write_string(
4038
bs = s.encode() if encoding is None else s.encode(encoding)
4139

4240
file_object.write(bs.ljust(new_len, b"\0"))
43-
44-
45-
def read_section_data(file: BinaryIO, section: Section) -> bytes:
46-
return read_bytes(
47-
file,
48-
Offset(section.pointer_to_raw_data),
49-
Offset(section.size_of_raw_data),
50-
)

dfint64_patch/extract_strings/cli.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
from pathlib import Path
44
from typing import BinaryIO, cast
55

6+
import lief
67
from omegaconf import DictConfig
7-
from peclasses.portable_executable import PortableExecutable
88

9-
from dfint64_patch.binio import read_section_data
109
from dfint64_patch.config import with_config
1110
from dfint64_patch.cross_references.cross_references_relative import (
1211
find_relative_cross_references,
@@ -20,23 +19,22 @@
2019

2120

2221
def extract_strings(pe_file: BinaryIO) -> list[ExtractedStringInfo]:
23-
pe = PortableExecutable(pe_file)
22+
pe: lief.PE.Binary = lief.PE.parse(pe_file)
2423

25-
sections = pe.section_table
26-
code_section = sections[0]
27-
string_section = sections[1]
24+
code_section = pe.sections[0]
25+
string_section = pe.sections[1]
2826

29-
image_base = pe.optional_header.image_base
27+
image_base = pe.optional_header.imagebase
3028

3129
strings = list(
3230
extract_strings_from_raw_bytes(
33-
read_section_data(pe_file, string_section),
31+
string_section.content,
3432
base_address=Rva(cast(int, string_section.virtual_address) + image_base),
3533
),
3634
)
3735

3836
cross_references = find_relative_cross_references(
39-
read_section_data(pe_file, code_section),
37+
code_section.content,
4038
base_address=Rva(cast(int, code_section.virtual_address) + image_base),
4139
addresses=map(operator.itemgetter(0), strings),
4240
)

dfint64_patch/extract_strings/from_raw_bytes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ExtractedStringInfo(NamedTuple):
3535

3636

3737
def extract_strings_from_raw_bytes(
38-
bytes_block: bytes,
38+
bytes_block: bytes | memoryview,
3939
base_address: Rva = RVA0,
4040
alignment: int = 4,
4141
encoding: str = "cp437",
@@ -48,6 +48,7 @@ def extract_strings_from_raw_bytes(
4848
:param encoding: string encoding
4949
:return: Iterator[ExtractedStringInfo]
5050
"""
51+
bytes_block = bytes(bytes_block)
5152
i = 0
5253
while i < len(bytes_block):
5354
if bytes_block[i] == b"\0":

dfint64_patch/patching/patch.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
from pathlib import Path
33
from typing import cast
44

5+
import lief
56
from loguru import logger
6-
from peclasses.portable_executable import PortableExecutable
77

8-
from dfint64_patch.binio import read_section_data
98
from dfint64_patch.cross_references.cross_references_relative import (
109
find_intersected_cross_references,
1110
find_relative_cross_references,
@@ -17,29 +16,28 @@
1716

1817
def patch(patched_file: str | Path, translation_table: list[tuple[str, str]], encoding: str) -> None:
1918
with Path(patched_file).open("r+b") as pe_file:
20-
pe = PortableExecutable(pe_file)
19+
pe = lief.PE.parse(pe_file)
2120

22-
sections = pe.section_table
23-
code_section = sections[0]
24-
data_section = sections[1]
21+
code_section = pe.sections[0]
22+
data_section = pe.sections[1]
2523

26-
cast(int, pe.optional_header.image_base)
24+
cast(int, pe.optional_header.imagebase)
2725

2826
logger.info("Extracting strings...")
2927
strings = {
3028
item.address: item.string
3129
for item in extract_strings_from_raw_bytes(
32-
read_section_data(pe_file, data_section),
33-
base_address=Rva(cast(int, data_section.virtual_address)),
30+
data_section.content,
31+
base_address=Rva(data_section.virtual_address),
3432
)
3533
}
3634

3735
logger.info(f"Found {len(strings)} string-like objects")
3836

3937
logger.info("Searching for cross references...")
4038
cross_references = find_relative_cross_references(
41-
read_section_data(pe_file, code_section),
42-
base_address=Rva(cast(int, code_section.virtual_address)),
39+
code_section.content,
40+
base_address=Rva(code_section.virtual_address),
4341
addresses=strings,
4442
)
4543

@@ -60,7 +58,7 @@ def patch(patched_file: str | Path, translation_table: list[tuple[str, str]], en
6058
if len(translation) <= len(string):
6159
# Shorter strings are padded with spaces
6260
encoded_translation = translation.encode(encoding).ljust(len(string) + 1) + b"\0"
63-
pe_file.seek(data_section.rva_to_offset(rva))
61+
pe_file.seek(pe.rva_to_offset(rva))
6462
pe_file.write(encoded_translation)
6563
else:
6664
# TODO: implement this case

dfint64_patch/strings_context/extract_strings_with_subs.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,29 @@
88
from pathlib import Path
99
from typing import BinaryIO, NamedTuple
1010

11+
import lief
1112
from omegaconf import DictConfig
12-
from peclasses.portable_executable import PortableExecutable
1313

14-
from dfint64_patch.binio import read_section_data
1514
from dfint64_patch.config import with_config
1615
from dfint64_patch.cross_references.cross_references_relative import find_relative_cross_references
1716
from dfint64_patch.extract_strings.from_raw_bytes import ExtractedStringInfo, extract_strings_from_raw_bytes
1817
from dfint64_patch.extract_subroutines.from_raw_bytes import SubroutineInfo, extract_subroutines, which_subroutine
1918
from dfint64_patch.type_aliases import Rva
2019

2120

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]
21+
def extract_strings_with_xrefs(pe: lief.PE.Binary) -> dict[ExtractedStringInfo, list[Rva]]:
22+
code_section = pe.sections[0]
23+
string_section = pe.sections[1]
2624

2725
strings = list(
2826
extract_strings_from_raw_bytes(
29-
read_section_data(pe_file, string_section),
27+
string_section.content,
3028
base_address=string_section.virtual_address,
3129
),
3230
)
3331

3432
cross_references = find_relative_cross_references(
35-
read_section_data(pe_file, code_section),
33+
code_section.content,
3634
base_address=code_section.virtual_address,
3735
addresses=map(itemgetter(0), strings),
3836
)
@@ -50,17 +48,16 @@ class StringCrossReference(NamedTuple):
5048

5149

5250
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+
pe = lief.PE.parse(pe_file)
52+
code_section = pe.sections[0]
5653

5754
image_base = pe.optional_header.image_base
5855

59-
strings_with_xrefs = extract_strings_with_xrefs(pe_file, pe)
56+
strings_with_xrefs = extract_strings_with_xrefs(pe)
6057

6158
subroutines = list(
6259
extract_subroutines(
63-
read_section_data(pe_file, code_section),
60+
code_section.content,
6461
base_offset=code_section.virtual_address,
6562
)
6663
)

0 commit comments

Comments
 (0)