-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathresults.py
307 lines (241 loc) · 9.76 KB
/
results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Copyright (C) 2021 Mandiant, Inc. All Rights Reserved.
import re
import json
import datetime
from enum import Enum
from typing import Dict, List, Optional
from pathlib import Path
from dataclasses import field
from pydantic import TypeAdapter, ValidationError
# we use pydantic for dataclasses so that we can
# easily load and validate JSON reports.
#
# pydantic checks all the JSON fields look as they should
# while using the nice and familiar dataclass syntax.
#
# really, you should just pretend we're using stock dataclasses.
from pydantic.dataclasses import dataclass
import floss.logging_
from floss.render import Verbosity
from floss.version import __version__
from floss.render.sanitize import sanitize
logger = floss.logging_.getLogger(__name__)
class InvalidResultsFile(Exception):
pass
class InvalidLoadConfig(Exception):
pass
class StringEncoding(str, Enum):
ASCII = "ASCII"
UTF16LE = "UTF-16LE"
UTF8 = "UTF-8"
@dataclass(frozen=True)
class StackString:
"""
here's what the following members represent:
[smaller addresses]
+---------------+ <- stack_pointer (top of stack)
| | \
+---------------+ | offset
| | /
+---------------+
| "abc" | \
+---------------+ |
| | |
+---------------+ | frame_offset
| | |
+---------------+ |
| | /
+---------------+ <- original_stack_pointer (bottom of stack, probably bp)
[bigger addresses]
Attributes:
function: the address of the function from which the stackstring was extracted
string: the extracted string
encoding: string encoding
program_counter: the program counter at the moment the string was extracted
stack_pointer: the stack counter at the moment the string was extracted
original_stack_pointer: the initial stack counter when the function was entered
offset: the offset into the stack from at which the stack string was found
frame_offset: the offset from the function frame at which the stack string was found
"""
function: int
string: str
encoding: StringEncoding
program_counter: int
stack_pointer: int
original_stack_pointer: int
offset: int
frame_offset: int
class TightString(StackString):
pass
class AddressType(str, Enum):
STACK = "STACK"
GLOBAL = "GLOBAL"
HEAP = "HEAP"
@dataclass(frozen=True)
class DecodedString:
"""
A decoding string and details about where it was found.
Attributes:
address: address of the string in memory
address_type: type of the address of the string in memory
string: the decoded string
encoding: the string encoding, like ASCII or unicode
decoded_at: the address at which the decoding routine is called
decoding_routine: the address of the decoding routine
"""
address: int
address_type: AddressType
string: str
encoding: StringEncoding
decoded_at: int
decoding_routine: int
@dataclass(frozen=True)
class StaticString:
"""
A string extracted from the raw bytes of the input.
Attributes:
string: the string
offset: the offset into the input where the string is found
encoding: the string encoding, like ASCII or unicode
"""
string: str
offset: int
encoding: StringEncoding
@classmethod
def from_utf8(cls, buf, addr, min_length):
try:
decoded_string = buf.decode("utf-8")
except UnicodeDecodeError:
raise ValueError("not utf-8")
if not re.sub(r"[\r\n\t]", "", decoded_string).isprintable():
raise ValueError("not printable")
if len(decoded_string) < min_length:
raise ValueError("too short")
return cls(string=decoded_string, offset=addr, encoding=StringEncoding.UTF8)
@dataclass
class Runtime:
start_date: datetime.datetime = datetime.datetime.now()
total: float = 0
vivisect: float = 0
find_features: float = 0
static_strings: float = 0
language_strings: float = 0
stack_strings: float = 0
decoded_strings: float = 0
tight_strings: float = 0
@dataclass
class Functions:
discovered: int = 0
library: int = 0
analyzed_stack_strings: int = 0
analyzed_tight_strings: int = 0
analyzed_decoded_strings: int = 0
decoding_function_scores: Dict[int, Dict[str, float]] = field(default_factory=dict)
@dataclass
class Analysis:
enable_static_strings: bool = True
enable_stack_strings: bool = True
enable_tight_strings: bool = True
enable_decoded_strings: bool = True
functions: Functions = field(default_factory=Functions)
STRING_TYPE_FIELDS = set([field for field in Analysis.__annotations__ if field.startswith("enable_")])
@dataclass
class Metadata:
file_path: str
version: str = __version__
imagebase: int = 0
min_length: int = 0
runtime: Runtime = field(default_factory=Runtime)
language: str = ""
language_version: str = ""
language_selected: str = "" # configured by user
file_offset_in_rdata: Optional[int] = None
@dataclass
class Strings:
stack_strings: List[StackString] = field(default_factory=list)
tight_strings: List[TightString] = field(default_factory=list)
decoded_strings: List[DecodedString] = field(default_factory=list)
static_strings: List[StaticString] = field(default_factory=list)
language_strings: List[StaticString] = field(default_factory=list)
language_strings_missed: List[StaticString] = field(default_factory=list)
@dataclass
class ResultDocument:
metadata: Metadata
analysis: Analysis = field(default_factory=Analysis)
strings: Strings = field(default_factory=Strings)
@classmethod
def parse_file(cls, path: Path) -> "ResultDocument":
return TypeAdapter(cls).validate_json(path.read_text(encoding="utf-8"))
def log_result(decoded_string, verbosity):
string = sanitize(decoded_string.string)
if verbosity < Verbosity.VERBOSE:
logger.info("%s", string)
else:
if type(decoded_string) == DecodedString:
logger.info(
"%s [%s] decoded by 0x%x called at 0x%x",
string,
decoded_string.encoding,
decoded_string.decoding_routine,
decoded_string.decoded_at,
)
elif type(decoded_string) in (StackString, TightString):
logger.info(
"%s [%s] in 0x%x at address 0x%x",
string,
decoded_string.encoding,
decoded_string.function,
decoded_string.program_counter,
)
else:
ValueError("unknown decoded or extracted string type: %s", type(decoded_string))
def load(sample: Path, analysis: Analysis, functions: List[int], min_length: int) -> ResultDocument:
logger.debug("loading results document: %s", str(sample))
results = read(sample)
results.metadata.file_path = f"{sample}\n{results.metadata.file_path}"
check_set_string_types(results, analysis)
if functions:
filter_functions(results, functions)
if min_length:
filter_string_len(results, min_length)
results.metadata.min_length = min_length
return results
def read(sample: Path) -> ResultDocument:
try:
with sample.open("rb") as f:
results = json.loads(f.read().decode("utf-8"))
except (json.decoder.JSONDecodeError, UnicodeDecodeError) as e:
raise InvalidResultsFile(f"{e}")
try:
results = ResultDocument(**results)
except (TypeError, ValidationError) as e:
raise InvalidResultsFile(f"{str(sample)} is not a valid FLOSS result document: {e}")
return results
def check_set_string_types(results: ResultDocument, wanted_analysis: Analysis) -> None:
for string_type in STRING_TYPE_FIELDS:
if getattr(wanted_analysis, string_type) and not getattr(results.analysis, string_type):
logger.warning(f"{string_type} not in loaded data, use --only/--no to enable/disable type(s)")
setattr(results.analysis, string_type, getattr(wanted_analysis, string_type))
def filter_functions(results: ResultDocument, functions: List[int]) -> None:
filtered_scores = dict()
for fva in functions:
try:
filtered_scores[fva] = results.analysis.functions.decoding_function_scores[fva]
except KeyError:
raise InvalidLoadConfig(f"function 0x{fva:x} not found in loaded data")
results.analysis.functions.decoding_function_scores = filtered_scores
results.strings.stack_strings = list(filter(lambda f: f.function in functions, results.strings.stack_strings))
results.strings.tight_strings = list(filter(lambda f: f.function in functions, results.strings.tight_strings))
results.strings.decoded_strings = list(
filter(lambda f: f.decoding_routine in functions, results.strings.decoded_strings)
)
results.analysis.functions.analyzed_stack_strings = len(results.strings.stack_strings)
results.analysis.functions.analyzed_tight_strings = len(results.strings.tight_strings)
results.analysis.functions.analyzed_decoded_strings = len(results.strings.decoded_strings)
def filter_string_len(results: ResultDocument, min_length: int) -> None:
results.strings.static_strings = list(filter(lambda s: len(s.string) >= min_length, results.strings.static_strings))
results.strings.stack_strings = list(filter(lambda s: len(s.string) >= min_length, results.strings.stack_strings))
results.strings.tight_strings = list(filter(lambda s: len(s.string) >= min_length, results.strings.tight_strings))
results.strings.decoded_strings = list(
filter(lambda s: len(s.string) >= min_length, results.strings.decoded_strings)
)