1 : // Copyright 2013 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Defines the PEHackerApp class, which implements the command-line
16 : // "pehacker" tool.
17 :
18 : #include "syzygy/pehacker/pehacker_app.h"
19 :
20 : #include "base/files/file_util.h"
21 : #include "base/json/json_reader.h"
22 : #include "base/strings/string_split.h"
23 : #include "base/strings/stringprintf.h"
24 : #include "base/strings/utf_string_conversions.h"
25 : #include "syzygy/block_graph/orderers/original_orderer.h"
26 : #include "syzygy/pdb/pdb_reader.h"
27 : #include "syzygy/pdb/pdb_writer.h"
28 : #include "syzygy/pe/decomposer.h"
29 : #include "syzygy/pe/pe_file_writer.h"
30 : #include "syzygy/pe/pe_relinker_util.h"
31 : #include "syzygy/pehacker/operation.h"
32 : #include "syzygy/pehacker/variables.h"
33 : #include "syzygy/pehacker/operations/add_imports_operation.h"
34 : #include "syzygy/pehacker/operations/redirect_imports_operation.h"
35 :
36 : namespace pehacker {
37 :
38 : namespace {
39 :
40 : using block_graph::BlockGraph;
41 :
42 : static const char kUsageFormatStr[] = "Usage: %ls [options]\n"
43 : " Required Options:\n"
44 : " --config-file=<path> Path to the configuration file to be used.\n"
45 : " Options:\n"
46 : " -Dvar=val Defines variable 'var' with value 'val'.\n"
47 : " Variable names defined on the command-line\n"
48 : " will be normalized to all lowercase. Values\n"
49 : " will be parsed as JSON.\n"
50 : " --overwrite Allow output files to be overwritten.\n"
51 : " --verbose Log verbosely.\n"
52 : "\n";
53 :
54 : // Gets the value under key |name| in |dictionary|, performing variable
55 : // expansion using |variables|, and finally converting it to a normalized path
56 : // in |path|. If |optional| this will return true if the key doesn't exist
57 : // and leave |path| unchanged. Returns true on success, false otherwise.
58 : bool GetFilePath(bool optional,
59 : const base::DictionaryValue& dictionary,
60 : const base::DictionaryValue& variables,
61 : const std::string& name,
62 E : base::FilePath* path) {
63 E : DCHECK_NE(reinterpret_cast<base::FilePath*>(NULL), path);
64 :
65 : const base::Value* value;
66 E : if (!dictionary.Get(name, &value)) {
67 E : if (optional)
68 E : return true;
69 :
70 i : LOG(ERROR) << "Dictionary does not contain key \"" << name << "\".";
71 i : return false;
72 : }
73 :
74 E : std::string s;
75 E : if (!ConvertVariableToString(*value, &s))
76 i : return false;
77 :
78 E : if (!ExpandVariables(variables, s, &s))
79 i : return false;
80 :
81 E : *path = base::FilePath(base::UTF8ToWide(s)).NormalizePathSeparators();
82 E : VLOG(1) << "Parsed \"" << name << "\" as \"" << path->value() << "\".";
83 E : return true;
84 E : }
85 :
86 E : void RemovePaddingBlocks(BlockGraph* block_graph) {
87 E : DCHECK_NE(reinterpret_cast<BlockGraph*>(NULL), block_graph);
88 E : BlockGraph::BlockMap::iterator it = block_graph->blocks_mutable().begin();
89 E : while (it != block_graph->blocks_mutable().end()) {
90 E : BlockGraph::BlockMap::iterator it_next = it;
91 E : ++it_next;
92 :
93 E : BlockGraph::Block* block = &it->second;
94 E : if (block->attributes() & BlockGraph::PADDING_BLOCK)
95 E : block_graph->RemoveBlock(block);
96 :
97 E : it = it_next;
98 E : }
99 E : }
100 :
101 : } // namespace
102 :
103 i : bool PEHackerApp::ImageId::operator<(const ImageId& rhs) const {
104 i : if (input_module.value() < rhs.input_module.value())
105 i : return true;
106 i : if (input_module.value() > rhs.input_module.value())
107 i : return false;
108 i : return output_module.value() < rhs.output_module.value();
109 i : }
110 :
111 E : bool PEHackerApp::ParseCommandLine(const base::CommandLine* cmd_line) {
112 E : DCHECK_NE(reinterpret_cast<const base::CommandLine*>(NULL), cmd_line);
113 :
114 E : if (cmd_line->HasSwitch("help"))
115 E : return Usage(cmd_line, "");
116 :
117 E : if (cmd_line->HasSwitch("verbose")) {
118 i : logging::SetMinLogLevel(logging::LOG_VERBOSE);
119 i : VLOG(1) << "Parsed --verbose switch.";
120 : }
121 :
122 : config_file_ = cmd_line->GetSwitchValuePath("config-file").
123 E : NormalizePathSeparators();
124 E : if (config_file_.empty()) {
125 E : LOG(ERROR) << "Must specify --config-file!";
126 E : return false;
127 : }
128 :
129 E : overwrite_ = cmd_line->HasSwitch("overwrite");
130 E : if (overwrite_) {
131 E : VLOG(1) << "Parsed --overwrite switch.";
132 : }
133 :
134 : // Set built-in variables.
135 E : if (!SetBuiltInVariables())
136 i : return false;
137 :
138 : // Parse any variables defined as arguments.
139 E : VLOG(1) << "Parsing command-line variables.";
140 E : const base::CommandLine::SwitchMap& switches = cmd_line->GetSwitches();
141 E : base::CommandLine::SwitchMap::const_iterator it = switches.begin();
142 E : for (; it != switches.end(); ++it) {
143 E : if (it->first[0] != 'd')
144 E : continue;
145 E : const std::wstring wname(it->first.begin() + 1, it->first.end());
146 E : std::string name = base::WideToUTF8(wname);
147 E : std::string value = base::WideToUTF8(it->second);
148 E : if (!ParseVariable(name, value, &variables_))
149 E : return false;
150 E : }
151 :
152 E : return true;
153 E : }
154 :
155 E : int PEHackerApp::Run() {
156 E : if (!LoadAndValidateConfigurationFile())
157 i : return 1;
158 :
159 E : if (!ProcessConfigurationFile(false))
160 i : return 1;
161 :
162 E : if (!WriteImages())
163 i : return 1;
164 :
165 E : return 0;
166 E : }
167 :
168 : bool PEHackerApp::Usage(const base::CommandLine* cmd_line,
169 E : const base::StringPiece& message) const {
170 E : if (!message.empty()) {
171 i : ::fwrite(message.data(), 1, message.length(), err());
172 i : ::fprintf(err(), "\n\n");
173 : }
174 :
175 : ::fprintf(err(),
176 : kUsageFormatStr,
177 E : cmd_line->GetProgram().BaseName().value().c_str());
178 :
179 E : return false;
180 E : }
181 :
182 E : bool PEHackerApp::SetBuiltInVariables() {
183 E : VLOG(1) << "Setting built-in variables.";
184 E : std::wstring wroot = config_file_.DirName().value();
185 E : std::string root = base::WideToUTF8(wroot);
186 E : variables_.Set("ROOT", new base::StringValue(root));
187 E : return true;
188 E : }
189 :
190 E : bool PEHackerApp::LoadAndValidateConfigurationFile() {
191 : // Parse the configuration file.
192 E : if (!ParseConfigFile())
193 E : return false;
194 :
195 : // Build the variables dictionary.
196 E : if (!UpdateVariablesFromConfig())
197 i : return false;
198 :
199 : // If we're logging verbosely then dump the variables for debugging.
200 E : if (logging::LOG_VERBOSE >= logging::GetMinLogLevel()) {
201 i : base::DictionaryValue::Iterator it(variables_);
202 i : for (; !it.IsAtEnd(); it.Advance()) {
203 i : std::string value;
204 i : ConvertVariableToJson(it.value(), &value);
205 i : VLOG(1) << "Have variable \"" << it.key() << "\" with value "
206 : << value << ".";
207 i : }
208 i : }
209 :
210 : // Process the configuration in dry-run mode. This doesn't do any work, but
211 : // validates that the configuration makes sense and can be run.
212 E : if (!ProcessConfigurationFile(true))
213 E : return false;
214 :
215 E : return true;
216 E : }
217 :
218 E : bool PEHackerApp::ParseConfigFile() {
219 E : LOG(INFO) << "Loading configuration file \"" << config_file_.value()
220 : << "\".";
221 :
222 E : VLOG(1) << "Reading configuration file from disk.";
223 E : std::string json;
224 E : if (!base::ReadFileToString(config_file_, &json)) {
225 E : LOG(ERROR) << "Unable to read configuration file \""
226 : << config_file_.value() << "\".";
227 E : return false;
228 : }
229 :
230 E : VLOG(1) << "Parsing configuration file contents.";
231 E : int error_code = 0;
232 E : std::string error_message;
233 : scoped_ptr<base::Value> config(base::JSONReader::ReadAndReturnError(
234 : json, base::JSON_ALLOW_TRAILING_COMMAS, &error_code,
235 E : &error_message));
236 E : if (config.get() == NULL) {
237 E : LOG(ERROR) << "Failed to parse configuration file: "
238 : << error_message << "(" << error_code << ").";
239 E : return false;
240 : }
241 :
242 : // Ensure the configuration is a dictionary, and transfer ownership to
243 : // config_ if it is.
244 E : base::DictionaryValue* dict = NULL;
245 E : if (!config->GetAsDictionary(&dict)) {
246 E : LOG(ERROR) << "Configuration must be a dictionary.";
247 E : return false;
248 : }
249 E : config_.reset(dict);
250 E : config.release();
251 :
252 E : return true;
253 E : }
254 :
255 E : bool PEHackerApp::UpdateVariablesFromConfig() {
256 E : base::Value* value = NULL;
257 E : if (!config_->Get("variables", &value))
258 E : return true;
259 :
260 E : base::DictionaryValue* variables = NULL;
261 E : if (!value->GetAsDictionary(&variables)) {
262 i : LOG(ERROR) << "Expect a dictionary for \"variables\".";
263 i : return false;
264 : }
265 :
266 E : VLOG(1) << "Merging configuration variables with command-line variables.";
267 E : if (!MergeVariables(*variables, &variables_))
268 i : return false;
269 E : return true;
270 E : }
271 :
272 E : bool PEHackerApp::ProcessConfigurationFile(bool dry_run) {
273 E : if (dry_run) {
274 E : VLOG(1) << "Validating configuration file.";
275 : }
276 :
277 E : base::ListValue* targets = NULL;
278 E : if (!config_->GetList("targets", &targets)) {
279 i : LOG(ERROR) << "Configuration must contain a \"targets\" list.";
280 i : return false;
281 : }
282 :
283 E : if (!ProcessTargets(dry_run, targets))
284 E : return false;
285 :
286 E : return true;
287 E : }
288 :
289 E : bool PEHackerApp::ProcessTargets(bool dry_run, base::ListValue* targets) {
290 E : DCHECK_NE(reinterpret_cast<base::ListValue*>(NULL), targets);
291 :
292 E : if (targets->GetSize() == 0) {
293 i : LOG(ERROR) << "No targets to process.";
294 i : return false;
295 : }
296 :
297 : // Process the targets in order.
298 E : for (size_t i = 0; i < targets->GetSize(); ++i) {
299 E : base::DictionaryValue* target = NULL;
300 E : if (!targets->GetDictionary(i, &target)) {
301 i : LOG(ERROR) << "Each target must be a dictionary.";
302 i : return false;
303 : }
304 :
305 E : if (!ProcessTarget(dry_run, target))
306 E : return false;
307 E : }
308 :
309 E : return true;
310 E : }
311 :
312 E : bool PEHackerApp::ProcessTarget(bool dry_run, base::DictionaryValue* target) {
313 E : DCHECK_NE(reinterpret_cast<base::DictionaryValue*>(NULL), target);
314 :
315 E : base::FilePath input_module;
316 E : base::FilePath output_module;
317 E : base::FilePath input_pdb;
318 E : base::FilePath output_pdb;
319 E : bool opt = false;
320 E : if (!GetFilePath(opt, *target, variables_, "input_module", &input_module))
321 i : return false;
322 E : if (!GetFilePath(opt, *target, variables_, "output_module", &output_module))
323 i : return false;
324 E : opt = true;
325 E : if (!GetFilePath(opt, *target, variables_, "input_pdb", &input_pdb))
326 i : return false;
327 E : if (!GetFilePath(opt, *target, variables_, "output_pdb", &output_pdb))
328 i : return false;
329 :
330 E : base::ListValue* operations = NULL;
331 E : if (!target->GetList("operations", &operations)) {
332 i : LOG(ERROR) << "Each target must specify an \"operations\" list.";
333 i : return false;
334 : }
335 :
336 : // Validate and infer module-related paths.
337 : if (!pe::ValidateAndInferPaths(
338 E : input_module, output_module, overwrite_, &input_pdb, &output_pdb)) {
339 E : return false;
340 : }
341 :
342 E : ImageInfo* image_info = NULL;
343 E : if (!dry_run) {
344 : // Get the decomposed image.
345 : image_info = GetImageInfo(
346 E : input_module, output_module, input_pdb, output_pdb);
347 E : if (image_info == NULL)
348 i : return false;
349 : }
350 :
351 E : VLOG(1) << "Processing operations for module \"" << input_module.value()
352 : << "\".";
353 E : if (!ProcessOperations(dry_run, operations, image_info))
354 i : return false;
355 :
356 E : return true;
357 E : }
358 :
359 : bool PEHackerApp::ProcessOperations(bool dry_run,
360 : base::ListValue* operations,
361 E : ImageInfo* image_info) {
362 E : DCHECK_NE(reinterpret_cast<base::ListValue*>(NULL), operations);
363 E : if (!dry_run)
364 E : DCHECK_NE(reinterpret_cast<ImageInfo*>(NULL), image_info);
365 :
366 E : for (size_t i = 0; i < operations->GetSize(); ++i) {
367 E : base::DictionaryValue* operation = NULL;
368 E : if (!operations->GetDictionary(i, &operation)) {
369 i : LOG(ERROR) << "Each operation must be a dictionary.";
370 i : return false;
371 : }
372 :
373 E : if (!ProcessOperation(dry_run, operation, image_info))
374 i : return false;
375 E : }
376 :
377 E : return true;
378 E : }
379 :
380 : bool PEHackerApp::ProcessOperation(bool dry_run,
381 : base::DictionaryValue* operation,
382 E : ImageInfo* image_info) {
383 E : DCHECK_NE(reinterpret_cast<base::DictionaryValue*>(NULL), operation);
384 E : if (!dry_run)
385 E : DCHECK_NE(reinterpret_cast<ImageInfo*>(NULL), image_info);
386 :
387 E : std::string type;
388 E : if (!operation->GetString("type", &type)) {
389 i : LOG(ERROR) << "Each operation must specify a \"type\".";
390 i : return false;
391 : }
392 :
393 : // Dispatch to the appropriate operation implementation.
394 E : scoped_ptr<OperationInterface> operation_impl;
395 E : if (type == "none") {
396 : // The 'none' operation is always defined, and does nothing. This is
397 : // mainly there for simple unittesting of configuration files.
398 E : return true;
399 i : } else if (type == "add_imports") {
400 i : operation_impl.reset(new operations::AddImportsOperation());
401 i : } else if (type == "redirect_imports") {
402 i : operation_impl.reset(new operations::RedirectImportsOperation());
403 i : } else {
404 i : LOG(ERROR) << "Unrecognized operation type \"" << type << "\".";
405 i : return false;
406 : }
407 :
408 : // Initialize the operation.
409 i : DCHECK_NE(reinterpret_cast<OperationInterface*>(NULL), operation_impl.get());
410 i : if (!operation_impl->Init(&policy_, operation)) {
411 i : LOG(ERROR) << "Failed to initialize \"" << operation_impl->name()
412 : << "\".";
413 i : return false;
414 : }
415 :
416 : // If not in a dry-run then apply the operation.
417 i : if (!dry_run) {
418 i : LOG(INFO) << "Applying operation \"" << type << "\" to \""
419 : << image_info->input_module.value() << "\".";
420 : if (!operation_impl->Apply(&policy_,
421 : &image_info->block_graph,
422 i : image_info->header_block)) {
423 i : LOG(ERROR) << "Failed to apply \"" << operation_impl->name() << "\".";
424 i : return false;
425 : }
426 : }
427 :
428 i : return true;
429 E : }
430 :
431 : PEHackerApp::ImageInfo* PEHackerApp::GetImageInfo(
432 : const base::FilePath& input_module,
433 : const base::FilePath& output_module,
434 : const base::FilePath& input_pdb,
435 E : const base::FilePath& output_pdb) {
436 E : DCHECK(!input_module.empty());
437 E : DCHECK(!output_module.empty());
438 E : DCHECK(!input_pdb.empty());
439 E : DCHECK(!output_pdb.empty());
440 :
441 : // Return the existing module if it exists.
442 E : ImageId image_id = { input_module, output_module };
443 E : ImageInfoMap::iterator it = image_info_map_.find(image_id);
444 E : if (it != image_info_map_.end())
445 i : return it->second;
446 :
447 : // Initialize a new ImageInfo struct.
448 E : scoped_ptr<ImageInfo> image_info(new ImageInfo());
449 E : image_info->input_module = input_module;
450 E : image_info->output_module = output_module;
451 E : image_info->input_pdb = input_pdb;
452 E : image_info->output_pdb = output_pdb;
453 E : if (!image_info->pe_file.Init(input_module)) {
454 i : LOG(ERROR) << "Failed to read image: " << input_module.value();
455 i : return NULL;
456 : }
457 :
458 : // Decompose the image.
459 E : pe::ImageLayout image_layout(&image_info->block_graph);
460 E : pe::Decomposer decomposer(image_info->pe_file);
461 E : if (!decomposer.Decompose(&image_layout)) {
462 i : LOG(ERROR) << "Failed to decompose image: " << input_module.value();
463 i : return NULL;
464 : }
465 :
466 : // Lookup the header block.
467 : image_info->header_block = image_layout.blocks.GetBlockByAddress(
468 E : BlockGraph::RelativeAddress(0));
469 : DCHECK_NE(reinterpret_cast<BlockGraph::Block*>(NULL),
470 E : image_info->header_block);
471 :
472 : // Remove padding blocks. No need to carry these through the pipeline.
473 E : VLOG(1) << "Removing padding blocks.";
474 E : RemovePaddingBlocks(&image_info->block_graph);
475 :
476 : // Get the input range to use in generating OMAP information. This is required
477 : // when finalizing the PDB.
478 E : pe::GetOmapRange(image_layout.sections, &image_info->input_omap_range);
479 :
480 : // Decomposition was successful. Add it to the map, transfer the image info to
481 : // the scoped array and return it.
482 E : it = image_info_map_.insert(std::make_pair(image_id, image_info.get())).first;
483 E : image_infos_.push_back(image_info.release());
484 E : return it->second;
485 E : }
486 :
487 E : bool PEHackerApp::WriteImages() {
488 E : ImageInfoMap::iterator it = image_info_map_.begin();
489 E : for (; it != image_info_map_.end(); ++it) {
490 E : ImageInfo* image_info = it->second;
491 :
492 E : LOG(INFO) << "Finalizing and writing image \""
493 : << image_info->output_module.value() << "\".";
494 :
495 : // Create a GUID for the output PDB.
496 E : GUID pdb_guid = {};
497 E : if (FAILED(::CoCreateGuid(&pdb_guid))) {
498 i : LOG(ERROR) << "Failed to create new GUID for output PDB.";
499 i : return false;
500 : }
501 :
502 : // Finalize the block-graph.
503 E : VLOG(1) << "Finalizing the block-graph.";
504 : if (!pe::FinalizeBlockGraph(image_info->input_module,
505 : image_info->output_pdb,
506 : pdb_guid,
507 : true,
508 : &policy_,
509 : &image_info->block_graph,
510 E : image_info->header_block)) {
511 i : return false;
512 : }
513 :
514 : // Build the ordered block-graph.
515 : block_graph::OrderedBlockGraph ordered_block_graph(
516 E : &image_info->block_graph);
517 E : block_graph::orderers::OriginalOrderer orderer;
518 E : VLOG(1) << "Ordering the block-graph.";
519 : if (!orderer.OrderBlockGraph(&ordered_block_graph,
520 E : image_info->header_block)) {
521 i : return false;
522 : }
523 :
524 : // Finalize the ordered block-graph.
525 E : VLOG(1) << "Finalizing the ordered block-graph.";
526 : if (!pe::FinalizeOrderedBlockGraph(&ordered_block_graph,
527 E : image_info->header_block)) {
528 i : return false;
529 : }
530 :
531 : // Build the image layout.
532 E : pe::ImageLayout image_layout(&image_info->block_graph);
533 E : VLOG(1) << "Building the image layout.";
534 : if (!pe::BuildImageLayout(0, 1, ordered_block_graph,
535 E : image_info->header_block, &image_layout)) {
536 i : return false;
537 : }
538 :
539 : // Write the image.
540 E : pe::PEFileWriter pe_writer(image_layout);
541 E : VLOG(1) << "Writing image to disk.";
542 E : if (!pe_writer.WriteImage(image_info->output_module))
543 i : return false;
544 :
545 E : LOG(INFO) << "Finalizing and writing PDB file \""
546 : << image_info->output_pdb.value() << "\".";
547 :
548 : // Parse the original PDB.
549 E : pdb::PdbFile pdb_file;
550 E : pdb::PdbReader pdb_reader;
551 E : VLOG(1) << "Reading original PDB.";
552 E : if (!pdb_reader.Read(image_info->input_pdb, &pdb_file))
553 i : return false;
554 :
555 : // Finalize the PDB to reflect the transformed image.
556 E : VLOG(1) << "Finalizing PDB.";
557 : if (!pe::FinalizePdbFile(image_info->input_module,
558 : image_info->output_module,
559 : image_info->input_omap_range,
560 : image_layout,
561 : pdb_guid,
562 : false,
563 : false,
564 : false,
565 E : &pdb_file)) {
566 i : return false;
567 : }
568 :
569 : // Write the PDB.
570 E : pdb::PdbWriter pdb_writer;
571 E : VLOG(1) << "Writing transformed PDB.";
572 E : if (!pdb_writer.Write(image_info->output_pdb, pdb_file))
573 i : return false;
574 E : }
575 :
576 E : return true;
577 E : }
578 :
579 : } // namespace pehacker
|