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->hooked_func.length() > 0) {
13 stream << hooked_func;
14 } else {
15 switch (this->type) {
17 stream << "hook_"; break;
19 stream << "addr_replaced_"; break;
20 default:
21 stream << "patch_"; break;
22 }
23 stream << id;
24 }
25 if (this->type != pesieve::HOOK_NONE) {
26 stream << "->";
27 if (this->isDirect) {
28 stream << std::hex << hookTargetVA;
29 }
30 else {
31 stream << "via:" << std::hex << hookTargetVA;
32 }
33 }
34 if (this->hookTargetModule) {
36 stream << "[";
37 stream << std::hex << hookTargetModule;
38 stream << "+" << diff << ":";
39 if (hookTargetModName.length() > 0) {
41 }
42 else {
43 stream << "(unnamed)";
44 }
45 stream << ":" << isTargetSuspicious;
46 stream << "]";
47 }
48 return stream.str();
49}
50
51const bool pesieve::PatchList::Patch::toTAG(std::ofstream &patch_report, const char delimiter)
52{
53 if (patch_report.is_open()) {
54 patch_report << std::hex << startRva;
56 patch_report << getFormattedName();
58 patch_report << (endRva - startRva);
59
60 patch_report << std::endl;
61 } else {
62 std::cout << std::hex << startRva << std::endl;
63 }
64 return true;
65}
66
67const bool pesieve::PatchList::Patch::toJSON(std::stringstream &outs, size_t level, bool short_info)
68{
69 OUT_PADDED(outs, level, "{\n");
70
71 OUT_PADDED(outs, (level + 1), "\"rva\" : ");
72 outs << "\"" << std::hex << (ULONGLONG)startRva << "\"" << ",\n";
73
74 OUT_PADDED(outs, (level + 1), "\"size\" : ");
75 outs << std::dec << (ULONGLONG)(endRva - startRva);
76
77 if (short_info) {
78 outs << ",\n";
79 OUT_PADDED(outs, (level + 1), "\"info\" : ");
80 outs << "\"" << getFormattedName() << "\"";
81 }
82 else {
83 outs << ",\n";
84 const bool isHook = (this->type != pesieve::HOOK_NONE);
85 OUT_PADDED(outs, (level + 1), "\"is_hook\" : ");
86 outs << std::dec << isHook;
87
88 if (this->hooked_func.length() > 0) {
89 outs << ",\n";
90 OUT_PADDED(outs, (level + 1), "\"func_name\" : ");
91 outs << "\"" << hooked_func << "\"";
92 }
93 if (isHook) {
94 outs << ",\n";
95 OUT_PADDED(outs, (level + 1), "\"hook_target\" : {\n");
96 if (hookTargetModName.length() > 0) {
97 OUT_PADDED(outs, (level + 2), "\"module_name\" : ");
98 outs << "\"" << hookTargetModName << "\"" << ",\n";
99 }
100 OUT_PADDED(outs, (level + 2), "\"module\" : ");
101 outs << "\"" << std::hex << (ULONGLONG)hookTargetModule << "\"" << ",\n";
102 OUT_PADDED(outs, (level + 2), "\"rva\" : ");
103 outs << "\"" << std::hex << (ULONGLONG)(hookTargetVA - hookTargetModule) << "\"" << ",\n";
104 OUT_PADDED(outs, (level + 2), "\"status\" : ");
105 outs << std::dec << (ULONGLONG)this->isTargetSuspicious << "\n";
106 OUT_PADDED(outs, (level + 1), "}");
107 }
108 }
109
110 outs << "\n";
111 OUT_PADDED(outs, level, "}");
112 return true;
113}
114
116{
117 ULONGLONG patch_va = (ULONGLONG) this->moduleBase + this->startRva;
118 const peconv::ExportedFunc *func = expMap.find_export_by_va(patch_va);
119 if (func == nullptr) {
120 return false; // not found
121 }
122 this->hooked_func = func->nameToString();
123 return true;
124}
125
126const size_t pesieve::PatchList::toTAGs(std::ofstream &patch_report, const char delimiter)
127{
128 std::vector<Patch*>::iterator itr;
129 for (itr = patches.begin(); itr != patches.end(); ++itr) {
130 Patch *patch = *itr;
132 }
133 return patches.size();
134}
135
136const bool pesieve::PatchList::toJSON(std::stringstream &outs, size_t level, bool short_info)
137{
138 if (patches.size() == 0) {
139 return false;
140 }
141 bool is_first = true;
142 OUT_PADDED(outs, level, "\"patches_list\" : [\n");
143 std::vector<Patch*>::iterator itr;
144 size_t id = 0;
145 for (itr = patches.begin(); itr != patches.end(); ++itr, ++id) {
146 if (!is_first) {
147 outs << ",\n";
148 }
149 Patch *patch = *itr;
151 is_first = false;
152 }
153 outs << "\n";
154 OUT_PADDED(outs, level, "]");
155 return true;
156}
157
159{
160 size_t hookes_exports = 0;
161 std::vector<Patch*>::iterator itr;
162 for (itr = patches.begin(); itr != patches.end(); ++itr) {
163 Patch *patch = *itr;
164 if (patch->resolveHookedExport(expMap)) {
166 }
167 }
168 return hookes_exports;
169}
170
172{
173 std::vector<Patch*>::iterator itr;
174 for (itr = patches.begin(); itr != patches.end(); ++itr) {
175 Patch *patch = *itr;
176 delete patch;
177 }
178 this->patches.clear();
179}
std::string getFormattedName()
Definition patch_list.cpp:8
std::string hookTargetModName
Definition patch_list.h:96
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:142
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
@ HOOK_INLINE
Definition patch_list.h:13
@ HOOK_ADDR_REPLACEMENT
Definition patch_list.h:14
@ HOOK_NONE
Definition patch_list.h:12
size_t fill_iat(BYTE *vBuf, size_t vBufSize, IN const peconv::ExportsMapper *exportsMap, IN OUT IATBlock &iat, IN ThunkFoundCallback *callback)
Definition iat_finder.h:31