strindex 3.3.0__tar.gz → 3.3.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {strindex-3.3.0/strindex.egg-info → strindex-3.3.2}/PKG-INFO +1 -1
- {strindex-3.3.0 → strindex-3.3.2}/setup.py +2 -2
- strindex-3.3.2/strindex/filetypes/datawin.py +119 -0
- strindex-3.3.2/strindex/filetypes/pefile.py +340 -0
- {strindex-3.3.0 → strindex-3.3.2}/strindex/strindex.py +7 -13
- {strindex-3.3.0 → strindex-3.3.2}/strindex/utils.py +1 -2
- {strindex-3.3.0 → strindex-3.3.2/strindex.egg-info}/PKG-INFO +1 -1
- {strindex-3.3.0 → strindex-3.3.2}/strindex.egg-info/SOURCES.txt +3 -1
- {strindex-3.3.0 → strindex-3.3.2}/LICENSE +0 -0
- {strindex-3.3.0 → strindex-3.3.2}/README.md +0 -0
- {strindex-3.3.0 → strindex-3.3.2}/setup.cfg +0 -0
- {strindex-3.3.0 → strindex-3.3.2}/strindex/__init__.py +0 -0
- {strindex-3.3.0 → strindex-3.3.2}/strindex/__main__.py +0 -0
- {strindex-3.3.0 → strindex-3.3.2}/strindex.egg-info/dependency_links.txt +0 -0
- {strindex-3.3.0 → strindex-3.3.2}/strindex.egg-info/entry_points.txt +0 -0
- {strindex-3.3.0 → strindex-3.3.2}/strindex.egg-info/requires.txt +0 -0
- {strindex-3.3.0 → strindex-3.3.2}/strindex.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: strindex
|
|
3
|
-
Version: 3.3.
|
|
3
|
+
Version: 3.3.2
|
|
4
4
|
Summary: A command line utility to extract and patch strings of various filetypes, with a focus on compatibility and translation.
|
|
5
5
|
Home-page: https://github.com/zWolfrost/strindex
|
|
6
6
|
Author: zWolfrost
|
|
@@ -3,7 +3,7 @@ from setuptools import setup
|
|
|
3
3
|
|
|
4
4
|
setup(
|
|
5
5
|
name = "strindex",
|
|
6
|
-
version = "3.3.
|
|
6
|
+
version = "3.3.2",
|
|
7
7
|
author = "zWolfrost",
|
|
8
8
|
author_email = "zwolfrost@gmail.com",
|
|
9
9
|
description = "A command line utility to extract and patch strings of various filetypes, with a focus on compatibility and translation.",
|
|
@@ -12,7 +12,7 @@ setup(
|
|
|
12
12
|
license = "MIT",
|
|
13
13
|
keywords = "PE strings patching translation",
|
|
14
14
|
url = "https://github.com/zWolfrost/strindex",
|
|
15
|
-
packages = ['strindex'],
|
|
15
|
+
packages = ['strindex', 'strindex.filetypes'],
|
|
16
16
|
install_requires=[
|
|
17
17
|
'pefile>=2024.8.26',
|
|
18
18
|
],
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
from strindex.utils import Strindex, FileBytearray
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def get_last_chunk_pointer(data: FileBytearray) -> int:
|
|
5
|
+
offset = 12
|
|
6
|
+
while offset < len(data):
|
|
7
|
+
size = int.from_bytes(data[offset:offset+4], 'little')
|
|
8
|
+
if size != 0:
|
|
9
|
+
prev_offset = offset
|
|
10
|
+
offset += size + 8
|
|
11
|
+
return prev_offset
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def is_valid(data: FileBytearray) -> bool:
|
|
15
|
+
return data[0:4] == b"FORM"
|
|
16
|
+
|
|
17
|
+
def create(data: FileBytearray, min_length: int, prefixes: list[bytes]) -> Strindex:
|
|
18
|
+
BYTE_LENGTH = 4
|
|
19
|
+
|
|
20
|
+
temp_strindex = {
|
|
21
|
+
"original": [],
|
|
22
|
+
"offsets": [],
|
|
23
|
+
"offset_bytes": [],
|
|
24
|
+
"pointers": []
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
for string, offset in data.yield_strings():
|
|
28
|
+
if len(string) >= min_length:
|
|
29
|
+
offset -= BYTE_LENGTH
|
|
30
|
+
temp_strindex["original"].append(string)
|
|
31
|
+
temp_strindex["offsets"].append(offset)
|
|
32
|
+
temp_strindex["offset_bytes"].append(offset.to_bytes(BYTE_LENGTH, 'little'))
|
|
33
|
+
|
|
34
|
+
if not temp_strindex["original"]:
|
|
35
|
+
raise ValueError("No strings found in the file.")
|
|
36
|
+
print(f"(1/2) Created search dictionary with {len(temp_strindex['original'])} strings.")
|
|
37
|
+
|
|
38
|
+
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["offset_bytes"], prefixes)
|
|
39
|
+
print(f"(2/2) Found pointers for {len([p for p in temp_strindex['pointers'] if p])} / {len(temp_strindex['original'])} strings.")
|
|
40
|
+
|
|
41
|
+
STRINDEX = Strindex()
|
|
42
|
+
for original, offset, _, pointers in zip(*temp_strindex.values()):
|
|
43
|
+
if pointers:
|
|
44
|
+
STRINDEX.overwrite.append(original)
|
|
45
|
+
STRINDEX.offsets.append(offset)
|
|
46
|
+
STRINDEX.pointers.append(pointers)
|
|
47
|
+
|
|
48
|
+
return STRINDEX
|
|
49
|
+
|
|
50
|
+
def patch(data: FileBytearray, strindex: Strindex) -> FileBytearray:
|
|
51
|
+
"""
|
|
52
|
+
The patching is done by increasing both
|
|
53
|
+
the "FORM" chunk size and the last chunk size to fit the new data.
|
|
54
|
+
The data is thus contained in the last chunk of the file.
|
|
55
|
+
The pointers are changed to reference the new data.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
BYTE_LENGTH = 4
|
|
59
|
+
DATA_LEN = len(data)
|
|
60
|
+
|
|
61
|
+
new_section_data = bytearray()
|
|
62
|
+
|
|
63
|
+
def increase_bytes(pointer: int, increase: int):
|
|
64
|
+
data[pointer:pointer+BYTE_LENGTH] = (int.from_bytes(data[pointer:pointer+BYTE_LENGTH], 'little') + increase).to_bytes(BYTE_LENGTH, 'little')
|
|
65
|
+
def get_replaced_offset() -> bytes:
|
|
66
|
+
return (DATA_LEN + len(new_section_data)).to_bytes(BYTE_LENGTH, 'little')
|
|
67
|
+
def new_section_string(string: str) -> bytes:
|
|
68
|
+
return len(string).to_bytes(BYTE_LENGTH, 'little') + bytearray(strindex.patch_replace_string(string), 'utf-8') + b'\x00'
|
|
69
|
+
|
|
70
|
+
temp_strindex = {
|
|
71
|
+
"original_offset": [],
|
|
72
|
+
"replaced_offset": [],
|
|
73
|
+
"pointers": [],
|
|
74
|
+
"pointers_switches": []
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
# Deal with compatible strings
|
|
78
|
+
for strindex_index, offset in enumerate(data.get_indices_ordered(strindex.original, b"\x00", b"\x00")):
|
|
79
|
+
if offset is None:
|
|
80
|
+
print(f'String not found: "{strindex.original[strindex_index]}"')
|
|
81
|
+
continue
|
|
82
|
+
|
|
83
|
+
offset -= BYTE_LENGTH
|
|
84
|
+
|
|
85
|
+
temp_strindex["original_offset"].append(offset.to_bytes(BYTE_LENGTH, 'little'))
|
|
86
|
+
temp_strindex["replaced_offset"].append(get_replaced_offset())
|
|
87
|
+
temp_strindex["pointers_switches"].append(strindex.pointers_switches[strindex_index])
|
|
88
|
+
|
|
89
|
+
new_section_data += new_section_string(strindex.replace[strindex_index])
|
|
90
|
+
|
|
91
|
+
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["original_offset"], strindex.settings["prefix_bytes"], strindex.settings["suffix_bytes"])
|
|
92
|
+
|
|
93
|
+
for original_offset, replaced_offset, pointers, pointers_switches in zip(*temp_strindex.values()):
|
|
94
|
+
if pointers:
|
|
95
|
+
for pointer, switch in zip(pointers, pointers_switches):
|
|
96
|
+
if switch:
|
|
97
|
+
data[pointer:pointer+BYTE_LENGTH] = replaced_offset
|
|
98
|
+
else:
|
|
99
|
+
print("No pointers found for rva: " + original_offset.hex())
|
|
100
|
+
|
|
101
|
+
# Deal with overwrite strings
|
|
102
|
+
for strindex_index in range(len(strindex.overwrite)):
|
|
103
|
+
replaced_offset = get_replaced_offset()
|
|
104
|
+
new_section_data += new_section_string(strindex.overwrite[strindex_index])
|
|
105
|
+
|
|
106
|
+
for pointer in strindex.pointers[strindex_index]:
|
|
107
|
+
if pointer:
|
|
108
|
+
data[pointer:pointer+BYTE_LENGTH] = replaced_offset
|
|
109
|
+
else:
|
|
110
|
+
print("No pointers found for string: " + strindex.overwrite[strindex_index])
|
|
111
|
+
print("(1/1) Created chunk data & relocated pointers.")
|
|
112
|
+
|
|
113
|
+
# Increase the "FORM" chunk size and the last chunk size
|
|
114
|
+
increase_bytes(4, len(new_section_data))
|
|
115
|
+
increase_bytes(get_last_chunk_pointer(data), len(new_section_data))
|
|
116
|
+
|
|
117
|
+
data += new_section_data
|
|
118
|
+
|
|
119
|
+
return data
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
from strindex.utils import Strindex, FileBytearray
|
|
2
|
+
import pefile
|
|
3
|
+
pefile.fast_load = True
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def pe_adjust_optional_header(pe: pefile.PE) -> pefile.PE:
|
|
7
|
+
"""
|
|
8
|
+
Recalculates the SizeOfImage, SizeOfCode, SizeOfInitializedData and
|
|
9
|
+
SizeOfUninitializedData of the optional header.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
# SizeOfImage = ((VirtualAddress + VirtualSize) of the new last section)
|
|
13
|
+
pe.OPTIONAL_HEADER.SizeOfImage = pe.sections[-1].VirtualAddress + pe.sections[-1].Misc_VirtualSize
|
|
14
|
+
|
|
15
|
+
pe.OPTIONAL_HEADER.SizeOfCode = 0
|
|
16
|
+
pe.OPTIONAL_HEADER.SizeOfInitializedData = 0
|
|
17
|
+
pe.OPTIONAL_HEADER.SizeOfUninitializedData = 0
|
|
18
|
+
|
|
19
|
+
# Recalculating the sizes by iterating over every section and checking if
|
|
20
|
+
# the appropriate characteristics are set.
|
|
21
|
+
for section in pe.sections:
|
|
22
|
+
if section.Characteristics & 0x00000020:
|
|
23
|
+
# Section contains code.
|
|
24
|
+
pe.OPTIONAL_HEADER.SizeOfCode += section.SizeOfRawData
|
|
25
|
+
if section.Characteristics & 0x00000040:
|
|
26
|
+
# Section contains initialized data.
|
|
27
|
+
pe.OPTIONAL_HEADER.SizeOfInitializedData += section.SizeOfRawData
|
|
28
|
+
if section.Characteristics & 0x00000080:
|
|
29
|
+
# Section contains uninitialized data.
|
|
30
|
+
pe.OPTIONAL_HEADER.SizeOfUninitializedData += section.SizeOfRawData
|
|
31
|
+
|
|
32
|
+
return pe
|
|
33
|
+
|
|
34
|
+
def pe_add_header_space(pe: pefile.PE) -> pefile.PE:
|
|
35
|
+
"""
|
|
36
|
+
To make space for a new section header a buffer filled with nulls is added at the
|
|
37
|
+
end of the headers. The buffer has the size of one file alignment.
|
|
38
|
+
The data between the last section header and the end of the headers is copied to
|
|
39
|
+
the new space (everything moved by the size of one file alignment).
|
|
40
|
+
If any data directory entry points to the moved data the pointer is adjusted.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
FileAlignment = pe.OPTIONAL_HEADER.FileAlignment
|
|
44
|
+
SizeOfHeaders = pe.OPTIONAL_HEADER.SizeOfHeaders
|
|
45
|
+
|
|
46
|
+
data = b'\x00' * FileAlignment
|
|
47
|
+
|
|
48
|
+
# Adding the null buffer.
|
|
49
|
+
pe.__data__ = pe.__data__[:SizeOfHeaders] + data + pe.__data__[SizeOfHeaders:]
|
|
50
|
+
|
|
51
|
+
section_table_offset = (
|
|
52
|
+
pe.DOS_HEADER.e_lfanew + 4 + pe.FILE_HEADER.sizeof() + pe.FILE_HEADER.SizeOfOptionalHeader
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
# Copying the data between the last section header and SizeOfHeaders to the newly allocated
|
|
56
|
+
# space.
|
|
57
|
+
new_section_offset = section_table_offset + pe.FILE_HEADER.NumberOfSections*0x28
|
|
58
|
+
size = SizeOfHeaders - new_section_offset
|
|
59
|
+
data = pe.get_data(new_section_offset, size)
|
|
60
|
+
pe.set_bytes_at_offset(new_section_offset + FileAlignment, data)
|
|
61
|
+
|
|
62
|
+
# Filling the space, from which the data was copied from, with NULLs.
|
|
63
|
+
pe.set_bytes_at_offset(new_section_offset, b'\x00' * FileAlignment)
|
|
64
|
+
|
|
65
|
+
data_directory_offset = section_table_offset - pe.OPTIONAL_HEADER.NumberOfRvaAndSizes * 0x8
|
|
66
|
+
|
|
67
|
+
# Checking data directories if anything points to the space between the last section header
|
|
68
|
+
# and the former SizeOfHeaders. If that's the case the pointer is increased by FileAlignment.
|
|
69
|
+
for data_offset in range(data_directory_offset, section_table_offset, 0x8):
|
|
70
|
+
data_rva = pe.get_dword_from_offset(data_offset)
|
|
71
|
+
|
|
72
|
+
if new_section_offset <= data_rva and data_rva < SizeOfHeaders:
|
|
73
|
+
pe.set_dword_at_offset(data_offset, data_rva + FileAlignment)
|
|
74
|
+
|
|
75
|
+
SizeOfHeaders_offset = pe.DOS_HEADER.e_lfanew + 4 + pe.FILE_HEADER.sizeof() + 0x3C
|
|
76
|
+
|
|
77
|
+
# Adjusting the SizeOfHeaders value.
|
|
78
|
+
pe.set_dword_at_offset(SizeOfHeaders_offset, SizeOfHeaders + FileAlignment)
|
|
79
|
+
|
|
80
|
+
section_raw_address_offset = section_table_offset + 0x14
|
|
81
|
+
|
|
82
|
+
# The raw addresses of the sections are adjusted.
|
|
83
|
+
for section in pe.sections:
|
|
84
|
+
if section.PointerToRawData != 0:
|
|
85
|
+
pe.set_dword_at_offset(section_raw_address_offset, section.PointerToRawData+FileAlignment)
|
|
86
|
+
|
|
87
|
+
section_raw_address_offset += 0x28
|
|
88
|
+
|
|
89
|
+
# All changes in this method were made to the raw data (__data__). To make these changes
|
|
90
|
+
# accessbile in pe __data__ has to be parsed again. Since a new pefile is parsed during
|
|
91
|
+
# the init method, the easiest way is to replace pe with a new pefile based on __data__
|
|
92
|
+
# of the old pe.
|
|
93
|
+
pe = pefile.PE(data=pe.__data__)
|
|
94
|
+
|
|
95
|
+
return pe
|
|
96
|
+
|
|
97
|
+
def pe_add_section(pe: pefile.PE, Name: str, Data: str, Characteristics=0xE00000E0, VirtualSize=0x00000000, VirtualAddress=0x00000000, RawSize=0x00000000, RawAddress=0x00000000) -> pefile.PE:
|
|
98
|
+
"""
|
|
99
|
+
Tested with pefile 1.2.10-123 on 32bit PE executable files.
|
|
100
|
+
An implementation to push a section header to the section table of a PE file.
|
|
101
|
+
by n0p
|
|
102
|
+
|
|
103
|
+
Adds the section, specified by the functions parameters, at the end of the section table.
|
|
104
|
+
If the space to add an additional section header is insufficient, a buffer is inserted
|
|
105
|
+
after SizeOfHeaders. Data between the last section header and the end of SizeOfHeaders
|
|
106
|
+
is copied to +1 FileAlignment. Data directory entries pointing to this data are fixed.
|
|
107
|
+
|
|
108
|
+
If a RawSize > 0 is set or Data is given the data gets aligned to the FileAlignment and
|
|
109
|
+
is attached at the end of the file.
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
if pe.FILE_HEADER.NumberOfSections == len(pe.sections):
|
|
113
|
+
|
|
114
|
+
FileAlignment = pe.OPTIONAL_HEADER.FileAlignment
|
|
115
|
+
SectionAlignment = pe.OPTIONAL_HEADER.SectionAlignment
|
|
116
|
+
|
|
117
|
+
if len(Name) > 8:
|
|
118
|
+
raise ValueError("The name is too long for a section.")
|
|
119
|
+
|
|
120
|
+
if (
|
|
121
|
+
VirtualAddress < (pe.sections[-1].Misc_VirtualSize + pe.sections[-1].VirtualAddress)
|
|
122
|
+
or VirtualAddress % SectionAlignment != 0
|
|
123
|
+
):
|
|
124
|
+
if (pe.sections[-1].Misc_VirtualSize % SectionAlignment) != 0:
|
|
125
|
+
VirtualAddress = (
|
|
126
|
+
pe.sections[-1].VirtualAddress + pe.sections[-1].Misc_VirtualSize -
|
|
127
|
+
(pe.sections[-1].Misc_VirtualSize % SectionAlignment) + SectionAlignment
|
|
128
|
+
)
|
|
129
|
+
else:
|
|
130
|
+
VirtualAddress = pe.sections[-1].VirtualAddress + pe.sections[-1].Misc_VirtualSize
|
|
131
|
+
|
|
132
|
+
if VirtualSize < len(Data):
|
|
133
|
+
VirtualSize = len(Data)
|
|
134
|
+
|
|
135
|
+
if (len(Data) % FileAlignment) != 0:
|
|
136
|
+
# Padding the data of the section.
|
|
137
|
+
Data += b'\x00' * (FileAlignment - (len(Data) % FileAlignment))
|
|
138
|
+
|
|
139
|
+
if RawSize != len(Data):
|
|
140
|
+
if RawSize > len(Data) and (RawSize % FileAlignment) == 0:
|
|
141
|
+
Data += b'\x00' * (RawSize - (len(Data) % RawSize))
|
|
142
|
+
else:
|
|
143
|
+
RawSize = len(Data)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
section_table_offset = (
|
|
147
|
+
pe.DOS_HEADER.e_lfanew + 4 + pe.FILE_HEADER.sizeof() + pe.FILE_HEADER.SizeOfOptionalHeader
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
# If the new section header exceeds the SizeOfHeaders there won't be enough space
|
|
151
|
+
# for an additional section header. Besides that it's checked if the 0x28 bytes
|
|
152
|
+
# (size of one section header) after the last current section header are filled
|
|
153
|
+
# with nulls/ are free to use.
|
|
154
|
+
if (
|
|
155
|
+
pe.OPTIONAL_HEADER.SizeOfHeaders < section_table_offset + (pe.FILE_HEADER.NumberOfSections+1)*0x28
|
|
156
|
+
or not all(char == b'\x00' for char in pe.get_data(section_table_offset + (pe.FILE_HEADER.NumberOfSections)*0x28, 0x28))
|
|
157
|
+
):
|
|
158
|
+
# Checking if more space can be added.
|
|
159
|
+
if pe.OPTIONAL_HEADER.SizeOfHeaders < pe.sections[0].VirtualAddress:
|
|
160
|
+
pe = pe_add_header_space(pe)
|
|
161
|
+
# print("Additional space to add a new section header was allocated.")
|
|
162
|
+
else:
|
|
163
|
+
raise ValueError("No more space can be added for the section header.")
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
# The validity check of RawAddress is done after space for a new section header may
|
|
167
|
+
# have been added because if space had been added the PointerToRawData of the previous
|
|
168
|
+
# section would have changed.
|
|
169
|
+
if RawAddress != pe.sections[-1].PointerToRawData + pe.sections[-1].SizeOfRawData:
|
|
170
|
+
RawAddress = pe.sections[-1].PointerToRawData + pe.sections[-1].SizeOfRawData
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
# Appending the data of the new section to the file.
|
|
174
|
+
if len(Data) > 0:
|
|
175
|
+
pe.__data__ = pe.__data__[:RawAddress] + Data + pe.__data__[RawAddress:]
|
|
176
|
+
|
|
177
|
+
section_offset = section_table_offset + pe.FILE_HEADER.NumberOfSections*0x28
|
|
178
|
+
|
|
179
|
+
# Manually writing the data of the section header to the file.
|
|
180
|
+
pe.set_bytes_at_offset(section_offset, Name)
|
|
181
|
+
pe.set_dword_at_offset(section_offset+0x08, VirtualSize)
|
|
182
|
+
pe.set_dword_at_offset(section_offset+0x0C, VirtualAddress)
|
|
183
|
+
pe.set_dword_at_offset(section_offset+0x10, RawSize)
|
|
184
|
+
pe.set_dword_at_offset(section_offset+0x14, RawAddress)
|
|
185
|
+
pe.set_dword_at_offset(section_offset+0x18, 0x00000000)
|
|
186
|
+
pe.set_dword_at_offset(section_offset+0x1C, 0x00000000)
|
|
187
|
+
pe.set_word_at_offset(section_offset+0x20, 0x0000)
|
|
188
|
+
pe.set_word_at_offset(section_offset+0x22, 0x0000)
|
|
189
|
+
pe.set_dword_at_offset(section_offset+0x24, Characteristics)
|
|
190
|
+
|
|
191
|
+
pe.FILE_HEADER.NumberOfSections += 1
|
|
192
|
+
|
|
193
|
+
# Parsing the section table of the file again to add the new section to the sections
|
|
194
|
+
# list of pefile.
|
|
195
|
+
pe.parse_sections(section_table_offset)
|
|
196
|
+
|
|
197
|
+
pe = pe_adjust_optional_header(pe)
|
|
198
|
+
else:
|
|
199
|
+
raise ValueError(
|
|
200
|
+
"The NumberOfSections specified in the file header and the size of the sections list of pefile don't match."
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
return pe
|
|
204
|
+
|
|
205
|
+
def pe_get_new_section_base_rva(pe: pefile.PE) -> int:
|
|
206
|
+
""" Returns the base rva for a possibly new PE section. """
|
|
207
|
+
new_section_base_rva = pe.sections[-1].VirtualAddress + pe.sections[-1].Misc_VirtualSize
|
|
208
|
+
if new_section_base_rva % pe.OPTIONAL_HEADER.SectionAlignment:
|
|
209
|
+
new_section_base_rva += pe.OPTIONAL_HEADER.SectionAlignment - (new_section_base_rva % pe.OPTIONAL_HEADER.SectionAlignment)
|
|
210
|
+
new_section_base_rva += pe.OPTIONAL_HEADER.ImageBase
|
|
211
|
+
return new_section_base_rva
|
|
212
|
+
|
|
213
|
+
def pe_section_exists(pe: pefile.PE, section_name: str) -> bool:
|
|
214
|
+
""" Checks if a section with the specified name exists. """
|
|
215
|
+
return any(sect.Name == section_name.ljust(8, b'\x00') for sect in pe.sections)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
SECTION_NAME = b".strdex"
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def is_valid(data: FileBytearray) -> bool:
|
|
222
|
+
""" Checks if the file is a valid PE file. """
|
|
223
|
+
try:
|
|
224
|
+
pe = pefile.PE(data=bytes(data))
|
|
225
|
+
except pefile.PEFormatError:
|
|
226
|
+
return False
|
|
227
|
+
return pe.DOS_HEADER.e_magic == 0x5A4D
|
|
228
|
+
|
|
229
|
+
def create(data: FileBytearray, min_length: int, prefixes: list[bytes]) -> Strindex:
|
|
230
|
+
pe = pefile.PE(data=bytes(data))
|
|
231
|
+
|
|
232
|
+
if pe_section_exists(pe, SECTION_NAME):
|
|
233
|
+
print(f"This file contains a '{SECTION_NAME.decode('utf-8')}' section. You might not want this.")
|
|
234
|
+
|
|
235
|
+
BYTE_LENGTH = 4 if pe.OPTIONAL_HEADER.Magic == 0x10b else 8
|
|
236
|
+
|
|
237
|
+
temp_strindex = {
|
|
238
|
+
"original": [],
|
|
239
|
+
"offsets": [],
|
|
240
|
+
"rva": [],
|
|
241
|
+
"rva_bytes": [],
|
|
242
|
+
"pointers": []
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
for string, offset in data.yield_strings():
|
|
246
|
+
if len(string) >= min_length:
|
|
247
|
+
rva = pe.get_rva_from_offset(offset)
|
|
248
|
+
if rva:
|
|
249
|
+
rva += pe.OPTIONAL_HEADER.ImageBase
|
|
250
|
+
temp_strindex["original"].append(string)
|
|
251
|
+
temp_strindex["offsets"].append(offset)
|
|
252
|
+
temp_strindex["rva"].append(rva)
|
|
253
|
+
temp_strindex["rva_bytes"].append(rva.to_bytes(BYTE_LENGTH, 'little'))
|
|
254
|
+
|
|
255
|
+
if not temp_strindex["original"]:
|
|
256
|
+
raise ValueError("No strings found in the file.")
|
|
257
|
+
print(f"(1/2) Created search dictionary with {len(temp_strindex['original'])} strings.")
|
|
258
|
+
|
|
259
|
+
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["rva_bytes"], prefixes)
|
|
260
|
+
print(f"(2/2) Found pointers for {len([p for p in temp_strindex['pointers'] if p])} / {len(temp_strindex['original'])} strings.")
|
|
261
|
+
|
|
262
|
+
STRINDEX = Strindex()
|
|
263
|
+
for string, offset, rva, _, pointers in zip(*temp_strindex.values()):
|
|
264
|
+
if pointers:
|
|
265
|
+
STRINDEX.overwrite.append(string)
|
|
266
|
+
STRINDEX.offsets.append(offset)
|
|
267
|
+
STRINDEX.pointers.append(pointers)
|
|
268
|
+
|
|
269
|
+
return STRINDEX
|
|
270
|
+
|
|
271
|
+
def patch(data: FileBytearray, strindex: Strindex) -> FileBytearray:
|
|
272
|
+
"""
|
|
273
|
+
The patching is done by adding a new section to the PE file, containing the new data.
|
|
274
|
+
The pointers are changed to reference the new data.
|
|
275
|
+
"""
|
|
276
|
+
|
|
277
|
+
pe = pefile.PE(data=bytes(data))
|
|
278
|
+
|
|
279
|
+
if pe_section_exists(pe, SECTION_NAME):
|
|
280
|
+
raise ValueError(f"This file already contains a '{SECTION_NAME.decode('utf-8')}' section.")
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
BYTE_LENGTH = 4 if pe.OPTIONAL_HEADER.Magic == 0x10b else 8
|
|
284
|
+
STRDEX_SECTION_BASE_RVA = pe_get_new_section_base_rva(pe)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
new_section_data = bytearray()
|
|
288
|
+
|
|
289
|
+
def get_replaced_rva() -> bytes:
|
|
290
|
+
return (STRDEX_SECTION_BASE_RVA + len(new_section_data)).to_bytes(BYTE_LENGTH, 'little')
|
|
291
|
+
def new_section_string(string: str) -> bytes:
|
|
292
|
+
return bytearray(strindex.patch_replace_string(string), 'utf-8') + b'\x00'
|
|
293
|
+
|
|
294
|
+
temp_strindex = {
|
|
295
|
+
"original_rva": [],
|
|
296
|
+
"replaced_rva": [],
|
|
297
|
+
"pointers": [],
|
|
298
|
+
"pointers_switches": []
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
# Deal with compatible strings
|
|
302
|
+
for strindex_index, offset in enumerate(data.get_indices_ordered(strindex.original, b"\x00", b"\x00")):
|
|
303
|
+
if offset is None:
|
|
304
|
+
print(f'String not found: "{strindex.original[strindex_index]}"')
|
|
305
|
+
continue
|
|
306
|
+
|
|
307
|
+
temp_strindex["original_rva"].append((pe.get_rva_from_offset(offset) + pe.OPTIONAL_HEADER.ImageBase).to_bytes(BYTE_LENGTH, 'little'))
|
|
308
|
+
temp_strindex["replaced_rva"].append(get_replaced_rva())
|
|
309
|
+
temp_strindex["pointers_switches"].append(strindex.pointers_switches[strindex_index])
|
|
310
|
+
|
|
311
|
+
new_section_data += new_section_string(strindex.replace[strindex_index])
|
|
312
|
+
|
|
313
|
+
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["original_rva"], strindex.settings["prefix_bytes"], strindex.settings["suffix_bytes"])
|
|
314
|
+
|
|
315
|
+
for original_rva, replaced_rva, pointers, pointers_switches in zip(*temp_strindex.values()):
|
|
316
|
+
if pointers:
|
|
317
|
+
for pointer, switch in zip(pointers, pointers_switches):
|
|
318
|
+
if switch:
|
|
319
|
+
pe.set_bytes_at_offset(pointer, replaced_rva)
|
|
320
|
+
else:
|
|
321
|
+
print("No pointers found for rva: " + original_rva.hex())
|
|
322
|
+
|
|
323
|
+
# Deal with overwrite strings
|
|
324
|
+
for strindex_index in range(len(strindex.overwrite)):
|
|
325
|
+
replaced_rva = get_replaced_rva()
|
|
326
|
+
new_section_data += new_section_string(strindex.overwrite[strindex_index])
|
|
327
|
+
|
|
328
|
+
for pointer in strindex.pointers[strindex_index]:
|
|
329
|
+
if pointer:
|
|
330
|
+
pe.set_bytes_at_offset(pointer, replaced_rva)
|
|
331
|
+
else:
|
|
332
|
+
print("No pointers found for string: " + strindex.overwrite[strindex_index])
|
|
333
|
+
print("(1/2) Created section data & relocated pointers.")
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
pe = pe_add_section(pe, Name=SECTION_NAME, Data=new_section_data, Characteristics=0xF0000040)
|
|
337
|
+
print(f"(2/2) Added '{SECTION_NAME.decode('utf-8')}' section.")
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
return bytearray(pe.write())
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import os, sys, argparse, re
|
|
2
2
|
from hashlib import md5
|
|
3
|
-
from
|
|
4
|
-
from .
|
|
3
|
+
from strindex.utils import PrintProgress, Strindex, FileBytearray, truncate_prev_char
|
|
4
|
+
from strindex.filetypes import pefile, datawin
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
def get_module_methods(data: FileBytearray, action: str) -> dict:
|
|
@@ -9,17 +9,11 @@ def get_module_methods(data: FileBytearray, action: str) -> dict:
|
|
|
9
9
|
Returns the methods of the module associated with the file type.
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
|
-
for
|
|
13
|
-
if
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
continue
|
|
18
|
-
|
|
19
|
-
if module.is_valid(data):
|
|
20
|
-
print(f'Detected filetype: "{file.rstrip(".py")}"')
|
|
21
|
-
assert action in module.__dict__, f"Action '{action}' is not available for this file type."
|
|
22
|
-
return module.__dict__[action]
|
|
12
|
+
for module in [pefile, datawin]:
|
|
13
|
+
if module.is_valid(data):
|
|
14
|
+
print(f'Detected filetype: "{module.__name__.split(".")[-1]}".')
|
|
15
|
+
assert action in module.__dict__, f"Action '{action}' is not available for this file type."
|
|
16
|
+
return module.__dict__[action]
|
|
23
17
|
|
|
24
18
|
raise ValueError("This file type has no associated module, or the required libraries to handle it are not installed.")
|
|
25
19
|
|
|
@@ -50,7 +50,7 @@ class Strindex():
|
|
|
50
50
|
|
|
51
51
|
DELIMITERS = (f"{'=' * 80}", f"{'-' * 80}", f'/', f'-')
|
|
52
52
|
HEADER = f"You can freely delete informational lines in the header like this one.\n\n{{}}\n\n"
|
|
53
|
-
INFO = f"#{'=' * 79}/ offset /
|
|
53
|
+
INFO = f"#{'=' * 79}/ offset / offset(s)-of-rva-pointer(s) /\n"
|
|
54
54
|
COMPATIBLE_INFO = f"#{'=' * 79}[reallocate pointer(s) if 1]\n# replace this string...\n#{'-' * 79}\n# ...with this string!\n"
|
|
55
55
|
|
|
56
56
|
full_header: str
|
|
@@ -60,7 +60,6 @@ class Strindex():
|
|
|
60
60
|
|
|
61
61
|
overwrite: list[str]
|
|
62
62
|
pointers: list[list[int]]
|
|
63
|
-
rvas: list[int]
|
|
64
63
|
offsets: list[int]
|
|
65
64
|
|
|
66
65
|
original: list[str]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: strindex
|
|
3
|
-
Version: 3.3.
|
|
3
|
+
Version: 3.3.2
|
|
4
4
|
Summary: A command line utility to extract and patch strings of various filetypes, with a focus on compatibility and translation.
|
|
5
5
|
Home-page: https://github.com/zWolfrost/strindex
|
|
6
6
|
Author: zWolfrost
|
|
@@ -10,4 +10,6 @@ strindex.egg-info/SOURCES.txt
|
|
|
10
10
|
strindex.egg-info/dependency_links.txt
|
|
11
11
|
strindex.egg-info/entry_points.txt
|
|
12
12
|
strindex.egg-info/requires.txt
|
|
13
|
-
strindex.egg-info/top_level.txt
|
|
13
|
+
strindex.egg-info/top_level.txt
|
|
14
|
+
strindex/filetypes/datawin.py
|
|
15
|
+
strindex/filetypes/pefile.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|