PE-sieve
Scans all running processes. Recognizes and dumps a variety of potentially malicious implants (replaced/implanted PEs, shellcodes, hooks, in-memory patches).
Toggle main menu visibility
Loading...
Searching...
No Matches
scanners
patch_list.cpp
Go to the documentation of this file.
1
#include "
patch_list.h
"
2
3
#include <iostream>
4
#include <sstream>
5
6
#include "
../utils/format_util.h
"
7
8
std::string
pesieve::PatchList::Patch::getFormattedName
()
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
) {
25
case
pesieve::HOOK_INLINE
:
26
stream <<
"hook_"
;
break
;
27
case
pesieve::HOOK_ADDR_REPLACEMENT
:
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
60
const
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
76
const
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
124
bool
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
135
const
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
145
const
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
167
size_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
180
void
pesieve::PatchList::deletePatches
()
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
}
pesieve::PatchList::Patch
Definition
patch_list.h:23
pesieve::PatchList::Patch::getFormattedName
std::string getFormattedName()
Definition
patch_list.cpp:8
pesieve::PatchList::Patch::moduleBase
HMODULE moduleBase
Definition
patch_list.h:91
pesieve::PatchList::Patch::hooked_func
std::string hooked_func
Definition
patch_list.h:97
pesieve::PatchList::Patch::hookTargetModName
std::string hookTargetModName
Definition
patch_list.h:101
pesieve::PatchList::Patch::type
t_patch_type type
Definition
patch_list.h:93
pesieve::PatchList::Patch::resolveHookedExport
bool resolveHookedExport(peconv::ExportsMapper &expMap)
Definition
patch_list.cpp:124
pesieve::PatchList::Patch::id
size_t id
Definition
patch_list.h:88
pesieve::PatchList::Patch::endRva
DWORD endRva
Definition
patch_list.h:90
pesieve::PatchList::Patch::paddingVal
BYTE paddingVal
Definition
patch_list.h:96
pesieve::PatchList::Patch::startRva
DWORD startRva
Definition
patch_list.h:89
pesieve::PatchList::Patch::hookTargetVA
ULONGLONG hookTargetVA
Definition
patch_list.h:95
pesieve::PatchList::Patch::isDirect
bool isDirect
Definition
patch_list.h:94
pesieve::PatchList::Patch::toTAG
const bool toTAG(std::ofstream &patch_report, const char delimiter)
Definition
patch_list.cpp:60
pesieve::PatchList::Patch::isTargetSuspicious
bool isTargetSuspicious
Definition
patch_list.h:100
pesieve::PatchList::Patch::toJSON
const bool toJSON(std::stringstream &outs, size_t level, bool short_info)
Definition
patch_list.cpp:76
pesieve::PatchList::Patch::hookTargetModule
ULONGLONG hookTargetModule
Definition
patch_list.h:99
pesieve::PatchList::checkForHookedExports
size_t checkForHookedExports(peconv::ExportsMapper &expMap)
Definition
patch_list.cpp:167
pesieve::PatchList::patches
std::vector< Patch * > patches
Definition
patch_list.h:147
pesieve::PatchList::deletePatches
void deletePatches()
Definition
patch_list.cpp:180
pesieve::PatchList::toJSON
const bool toJSON(std::stringstream &outs, size_t level, bool short_info)
Definition
patch_list.cpp:145
pesieve::PatchList::toTAGs
const size_t toTAGs(std::ofstream &patch_report, const char delimiter)
Definition
patch_list.cpp:135
format_util.h
OUT_PADDED
#define OUT_PADDED(stream, field_size, str)
Definition
format_util.h:12
pesieve::PATCH_UNKNOWN
@ PATCH_UNKNOWN
Definition
patch_list.h:12
pesieve::PATCH_BREAKPOINT
@ PATCH_BREAKPOINT
Definition
patch_list.h:16
pesieve::HOOK_INLINE
@ HOOK_INLINE
Definition
patch_list.h:13
pesieve::HOOK_ADDR_REPLACEMENT
@ HOOK_ADDR_REPLACEMENT
Definition
patch_list.h:14
pesieve::PATCH_PADDING
@ PATCH_PADDING
Definition
patch_list.h:15
patch_list.h
Generated by
1.17.0