PE-sieve
Scans all running processes. Recognizes and dumps a variety of potentially malicious implants (replaced/implanted PEs, shellcodes, hooks, in-memory patches).
Loading...
Searching...
No Matches
patch_list.cpp
Go to the documentation of this file.
1#include "patch_list.h"
2
3#include <iostream>
4#include <sstream>
5
7
9{
10 std::stringstream stream;
11
12 if (this->type == pesieve::PATCH_PADDING) {
13 stream << "padding:";
14 stream << std::hex << "0x" << (unsigned int)paddingVal;
15 return stream.str();
16 }
17 if (this->type == pesieve::PATCH_BREAKPOINT) {
18 stream << "breakpoint";
19 return stream.str();
20 }
21 if (this->hooked_func.length() > 0) {
22 stream << hooked_func;
23 } else {
24 switch (this->type) {
26 stream << "hook_"; break;
28 stream << "addr_replaced_"; break;
29 default:
30 stream << "patch_"; break;
31 }
32 stream << id;
33 }
34 if (this->type != pesieve::PATCH_UNKNOWN) {
35 stream << "->";
36 if (this->isDirect) {
37 stream << std::hex << hookTargetVA;
38 }
39 else {
40 stream << "via:" << std::hex << hookTargetVA;
41 }
42 }
43 if (this->hookTargetModule) {
44 ULONGLONG diff = hookTargetVA - hookTargetModule;
45 stream << "[";
46 stream << std::hex << hookTargetModule;
47 stream << "+" << diff << ":";
48 if (hookTargetModName.length() > 0) {
49 stream << hookTargetModName;
50 }
51 else {
52 stream << "(unnamed)";
53 }
54 stream << ":" << isTargetSuspicious;
55 stream << "]";
56 }
57 return stream.str();
58}
59
60const bool pesieve::PatchList::Patch::toTAG(std::ofstream &patch_report, const char delimiter)
61{
62 if (patch_report.is_open()) {
63 patch_report << std::hex << startRva;
64 patch_report << delimiter;
65 patch_report << getFormattedName();
66 patch_report << delimiter;
67 patch_report << (endRva - startRva);
68
69 patch_report << std::endl;
70 } else {
71 std::cout << std::hex << startRva << std::endl;
72 }
73 return true;
74}
75
76const bool pesieve::PatchList::Patch::toJSON(std::stringstream &outs, size_t level, bool short_info)
77{
78 OUT_PADDED(outs, level, "{\n");
79
80 OUT_PADDED(outs, (level + 1), "\"rva\" : ");
81 outs << "\"" << std::hex << (ULONGLONG)startRva << "\"" << ",\n";
82
83 OUT_PADDED(outs, (level + 1), "\"size\" : ");
84 outs << std::dec << (ULONGLONG)(endRva - startRva);
85
86 if (short_info) {
87 outs << ",\n";
88 OUT_PADDED(outs, (level + 1), "\"info\" : ");
89 outs << "\"" << getFormattedName() << "\"";
90 }
91 else {
92 outs << ",\n";
93 const bool isHook = (this->type == pesieve::HOOK_INLINE || this->type == pesieve::HOOK_ADDR_REPLACEMENT);
94 OUT_PADDED(outs, (level + 1), "\"is_hook\" : ");
95 outs << std::dec << isHook;
96
97 if (this->hooked_func.length() > 0) {
98 outs << ",\n";
99 OUT_PADDED(outs, (level + 1), "\"func_name\" : ");
100 outs << "\"" << hooked_func << "\"";
101 }
102 if (isHook) {
103 outs << ",\n";
104 OUT_PADDED(outs, (level + 1), "\"hook_target\" : {\n");
105 if (hookTargetModName.length() > 0) {
106 OUT_PADDED(outs, (level + 2), "\"module_name\" : ");
107 outs << "\"" << hookTargetModName << "\"" << ",\n";
108 }
109 OUT_PADDED(outs, (level + 2), "\"module\" : ");
110 outs << "\"" << std::hex << (ULONGLONG)hookTargetModule << "\"" << ",\n";
111 OUT_PADDED(outs, (level + 2), "\"rva\" : ");
112 outs << "\"" << std::hex << (ULONGLONG)(hookTargetVA - hookTargetModule) << "\"" << ",\n";
113 OUT_PADDED(outs, (level + 2), "\"status\" : ");
114 outs << std::dec << (ULONGLONG)this->isTargetSuspicious << "\n";
115 OUT_PADDED(outs, (level + 1), "}");
116 }
117 }
118
119 outs << "\n";
120 OUT_PADDED(outs, level, "}");
121 return true;
122}
123
124bool pesieve::PatchList::Patch::resolveHookedExport(peconv::ExportsMapper &expMap)
125{
126 ULONGLONG patch_va = (ULONGLONG) this->moduleBase + this->startRva;
127 const peconv::ExportedFunc *func = expMap.find_export_by_va(patch_va);
128 if (func == nullptr) {
129 return false; // not found
130 }
131 this->hooked_func = func->nameToString();
132 return true;
133}
134
135const size_t pesieve::PatchList::toTAGs(std::ofstream &patch_report, const char delimiter)
136{
137 std::vector<Patch*>::iterator itr;
138 for (itr = patches.begin(); itr != patches.end(); ++itr) {
139 Patch *patch = *itr;
140 patch->toTAG(patch_report, delimiter);
141 }
142 return patches.size();
143}
144
145const bool pesieve::PatchList::toJSON(std::stringstream &outs, size_t level, bool short_info)
146{
147 if (patches.size() == 0) {
148 return false;
149 }
150 bool is_first = true;
151 OUT_PADDED(outs, level, "\"patches_list\" : [\n");
152 std::vector<Patch*>::iterator itr;
153 size_t id = 0;
154 for (itr = patches.begin(); itr != patches.end(); ++itr, ++id) {
155 if (!is_first) {
156 outs << ",\n";
157 }
158 Patch *patch = *itr;
159 patch->toJSON(outs, level + 1, short_info);
160 is_first = false;
161 }
162 outs << "\n";
163 OUT_PADDED(outs, level, "]");
164 return true;
165}
166
167size_t pesieve::PatchList::checkForHookedExports(peconv::ExportsMapper &expMap)
168{
169 size_t hookes_exports = 0;
170 std::vector<Patch*>::iterator itr;
171 for (itr = patches.begin(); itr != patches.end(); ++itr) {
172 Patch *patch = *itr;
173 if (patch->resolveHookedExport(expMap)) {
174 hookes_exports++;
175 }
176 }
177 return hookes_exports;
178}
179
181{
182 std::vector<Patch*>::iterator itr;
183 for (itr = patches.begin(); itr != patches.end(); ++itr) {
184 Patch *patch = *itr;
185 delete patch;
186 }
187 this->patches.clear();
188}
std::string getFormattedName()
Definition patch_list.cpp:8
std::string hookTargetModName
Definition patch_list.h:101
bool resolveHookedExport(peconv::ExportsMapper &expMap)
const bool toTAG(std::ofstream &patch_report, const char delimiter)
const bool toJSON(std::stringstream &outs, size_t level, bool short_info)
size_t checkForHookedExports(peconv::ExportsMapper &expMap)
std::vector< Patch * > patches
Definition patch_list.h:147
const bool toJSON(std::stringstream &outs, size_t level, bool short_info)
const size_t toTAGs(std::ofstream &patch_report, const char delimiter)
#define OUT_PADDED(stream, field_size, str)
Definition format_util.h:12
@ PATCH_UNKNOWN
Definition patch_list.h:12
@ PATCH_BREAKPOINT
Definition patch_list.h:16
@ HOOK_INLINE
Definition patch_list.h:13
@ HOOK_ADDR_REPLACEMENT
Definition patch_list.h:14
@ PATCH_PADDING
Definition patch_list.h:15