cobf
PE imports obfuscator
cli.cpp
Go to the documentation of this file.
1 
7 // Includes.
8 #include "ini.h"
9 #include <iostream>
10 #include <cobf.hpp>
11 
12 // Processed line handler.
13 void ini_line_handler(void* data, unsigned int line, const char* sec, const char* nam, const char* val)
14 {
15  // Cast the needed pointer.
16  cobf* p_obf_file = (cobf*)data;
17  cobf_error ret_err;
18  char error[MAX_PATH];
19 
20  // Saving the old section.
21  static string s_sec = "";
22  if (sec)
23  {
24  // Get the dll name.
25  s_sec = sec;
26  return;
27  };
28 
29  // Name/ordinals checking.
30  if (*nam != '#' && *val != '#') ret_err = p_obf_file->obf_sym(s_sec.c_str(), nam, val);
31  else if (*nam != '#' && *val == '#') ret_err = p_obf_file->obf_sym(s_sec.c_str(), nam, atoi(val + 1));
32  else if (*nam == '#' && *val != '#') ret_err = p_obf_file->obf_sym(s_sec.c_str(), atoi(nam + 1), val);
33  else ret_err = p_obf_file->obf_sym(s_sec.c_str(), atoi(nam + 1), atoi(val + 1));
34 
35  // Check the error.
36  if (ret_err != cobf_error::COBF_NO_ERROR)
37  {
38  // Error at processing.
39  cobf_format_message(ret_err, error, sizeof(error));
40  cout << "[-] Error at the config at line " << line << ": " << error << endl;
41  };
42 };
43 
44 // The main entry.
45 int main(int argc, char** argv)
46 {
47  // Expecting the input file, out file and the config file.
48  if (argc < 3 || argc > 4)
49  {
50  // Print help.
51  cout << *argv << " <input file> <out file> [config file]" << endl;
52  return 1;
53  };
54 
55  // Getting the data.
56  char* input_file = argv[1];
57  char* out_file = argv[2];
58  char* config_file = argc == 4 ? argv[3] : (char*)"config.ini";
59 
60  // Needed locals.
61  cobf_error ret_err;
62  char error[MAX_PATH];
63  try
64  {
65  // Loading the input file.
66  cobf obf_file = cobf(input_file);
67  if ((ret_err = obf_file.load_pe()) != cobf_error::COBF_NO_ERROR)
68  {
69  // Error at loading.
70  cobf_format_message(ret_err, error, sizeof(error));
71  cout << "[-] Error at loading the file: " << error << endl;
72  return 1;
73  };
74 
75  // Log.
76  cout << "[+] File loaded successfully." << endl;
77 
78  // Process the config file.
79  if (!parse_ini_file(config_file, ini_line_handler, &obf_file))
80  {
81  // Error at processing the config.
82  cout << "[-] Error occurred while processing the config file." << endl;
83  return 1;
84  };
85 
86  // Obfuscating it.
87  if ((ret_err = obf_file.generate(out_file)) != cobf_error::COBF_NO_ERROR)
88  {
89  // Error at obfuscating.
90  cobf_format_message(ret_err, error, sizeof(error));
91  cout << "[-] Error at obfuscating the file: " << error << endl;
92  return 1;
93  };
94 
95  // Log.
96  cout << "[+] File obfuscated successfully." << endl;
97 
98  // Unloading it.
99  if ((ret_err = obf_file.unload_pe()) != cobf_error::COBF_NO_ERROR)
100  {
101  // Error at unloading.
102  cobf_format_message(ret_err, error, sizeof(error));
103  cout << "[-] Error at unloading the file: " << error << endl;
104  return 1;
105  };
106 
107  // Log.
108  cout << "[+] File unloaded successfully." << endl;
109 
110  }
111  catch (...)
112  {
113  // Log.
114  cout << "[!] Unhandled exception occurred." << endl;
115  return 1;
116  };
117 
118  // Done.
119  return 0;
120 };
cobf
Definition: cobf.hpp:26
cobf::unload_pe
cobf_error unload_pe()
Unload the specified PE from memory.
Definition: load.cpp:138
cobf_format_message
void cobf_format_message(cobf_error err_msg, char *buffer, unsigned int size)
Format the error to a message.
Definition: utils.cpp:15
cobf::load_pe
cobf_error load_pe()
Load the specified PE from disk.
Definition: load.cpp:17
cobf::generate
cobf_error generate(string out_file)
Generate the obfuscated PE.
Definition: obfuscate.cpp:78
main
int main(int argc, char **argv)
Definition: cli.cpp:45
ini.h
parse_ini_file
int parse_ini_file(const char *f_path, ini_line_cb handler, void *data)
Parse ini file, supply the data to a callback.
Definition: ini.c:22
cobf.hpp
cobf_error
cobf_error
Definition: utils.hpp:15
ini_line_handler
void ini_line_handler(void *data, unsigned int line, const char *sec, const char *nam, const char *val)
Definition: cli.cpp:13
cobf::obf_sym
cobf_error obf_sym(string dll_name, string sym_name, string obf_name)
Obfuscate one symbol (name) with another (name).
Definition: obfuscate.cpp:32