Skip to content

Commit

Permalink
Added option -l --list to retdec-bin2pat.
Browse files Browse the repository at this point in the history
The added option -l --list is being used to pass the list of objects
from retdec-signature-from-library-creator.py as a text file.
Resolves #472
  • Loading branch information
astrelsky committed Feb 6, 2019
1 parent 1d9bda5 commit 157ded0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# dev

* Fix: Added option to `retdec-bin2pat` to have the objects list passed through a text file.
Modified `retdec-signature-from-library-creator.py` to pass objects list as a text file. ([#472](https://github.com/avast-tl/retdec/issues/472)).
* New Feature: Added presentation of imported types and TypeRef hashes for .NET binaries ([#363](https://github.com/avast-tl/retdec/issues/363), [#364](https://github.com/avast-tl/retdec/issues/364), [#428](https://github.com/avast-tl/retdec/issues/428)).
* New Feature: Added computation and presentation of icon hashes for exact and also similarity matching in PE files ([#339](https://github.com/avast-tl/retdec/issues/339)).
* Enhancement: Added support for build and run on FreeBSD and potentially on other BSD OSes ([#476](https://github.com/avast-tl/retdec/pull/476)).
Expand Down
6 changes: 5 additions & 1 deletion scripts/retdec-signature-from-library-creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def _check_arguments(self):

dir_name = os.path.dirname(os.path.abspath(self.args.output))
self.tmp_dir_path = tempfile.mkdtemp(dir=dir_name)
self.object_list_path = os.path.join(self.tmp_dir_path, 'object-list.txt')

if self.args.ignore_nops:
self.ignore_nop = '--ignore-nops'
Expand Down Expand Up @@ -126,7 +127,10 @@ def run(self):
# Extract patterns from library.
pattern_file = os.path.join(self.tmp_dir_path, lib_name) + '.pat'
pattern_files.append(pattern_file)
_, result, _ = cmd.run_cmd([config.BIN2PAT, '-o', pattern_file] + objects, discard_stdout=True, discard_stderr=True)
with open(self.object_list_path, 'w') as object_list:
for item in objects:
object_list.write(item+'\n')
_, result, _ = cmd.run_cmd([config.BIN2PAT, '-o', pattern_file, '-l', self.object_list_path], discard_stdout=True, discard_stderr=True)

if result != 0:
self.print_error_and_cleanup('utility bin2pat failed when processing %s' % lib_path)
Expand Down
38 changes: 36 additions & 2 deletions src/bin2pat/bin2pat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ void printUsage(
std::ostream &outputStream)
{
outputStream << "Usage: bin2pat [-o OUTPUT_FILE] [-n NOTE]"
<< " INPUT_FILE [INPUT_FILE...]\n\n"
<< " <INPUT_FILE [INPUT_FILE...] | -l LIST_FILE>\n\n"
<< "-o --output OUTPUT_FILE\n"
<< " Output file path (if not given, stdout is used).\n"
<< " If multiple paths are given, only last one is used.\n\n"
<< "-n --note NOTE\n"
<< " Optional note that will be added to all rules.\n"
<< " If multiple notes are given, only last one is used.\n\n";
<< " If multiple notes are given, only last one is used.\n\n"
<< "-l --list LIST_FILE\n"
<< " Optionally pass the list of input files as a text file.\n"
<< " This is useful for a large number of input files.\n\n";
}

void printErrorAndDie(
Expand Down Expand Up @@ -81,6 +84,37 @@ void processArgs(
return;
}
}
else if (args[i] == "-l" || args[i] == "--list") {
// Ensure -l --list is not the last thing in args
if (&args[i] == &args.back()) {
printErrorAndDie("input file missing");
return;
}

std::ifstream inputObjects(args[++i]);
std::string object;

if (inputObjects.is_open()) {
// Read LIST_FILE until EOF
while (inputObjects >> object) {
// Ensure file exists before proceeding
if(!FilesystemPath(object).isFile()) {
printErrorAndDie("argument '" + args[i]
+ "' contains the filename '" + object
+ "' which is not a valid file");
return;
}
else {
inPaths.push_back(object);
}
}
}
else {
printErrorAndDie("LIST_FILE '" + args[i]
+ "' is not a valid file");
return;
}
}
else {
// Input file. Check file on system level.
if(!FilesystemPath(args[i]).isFile()) {
Expand Down

0 comments on commit 157ded0

Please sign in to comment.