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
code_scanner.h
Go to the documentation of this file.
1#pragma once
2#include <windows.h>
3#include <vector>
4#include <fstream>
5
6#include "module_scanner.h"
7#include "pe_section.h"
8#include "patch_list.h"
9
10namespace pesieve {
11
14 {
15 public:
22
27
29 {
30 size_t counter = 0;
31 std::map<DWORD, t_section_status>::iterator itr;
32 for (itr = sectionToResult.begin(); itr != sectionToResult.end(); ++itr) {
33 const t_section_status status = itr->second;
34 if (status == neededStatus) {
35 counter++;
36 }
37 }
38 return counter;
39 }
40
41 const virtual void fieldsToJSON(std::stringstream &outs, size_t level, const pesieve::t_json_level &jdetails)
42 {
44 const size_t scannedCount = sectionToResult.size() - inaccessibleCount;
46 if (sectionToResult.size() > 0) {
47 outs << ",\n";
48 OUT_PADDED(outs, level, "\"scanned_sections\" : ");
49 outs << std::dec << scannedCount;
50 }
51 if (inaccessibleCount > 0) {
52 outs << ",\n";
53 OUT_PADDED(outs, level, "\"inaccessible_sections\" : ");
54 outs << std::dec << inaccessibleCount;
55 }
56 const size_t unpacked = countUnpackedSections();
57 if (unpacked > 0) {
58 outs << ",\n";
59 OUT_PADDED(outs, level, "\"unpacked_sections\" : ");
60 outs << std::dec << unpacked;
61 }
62 if (patchesList.size() > 0) {
63 outs << ",\n";
64 OUT_PADDED(outs, level, "\"patches\" : ");
65 outs << std::dec << patchesList.size();
66
67 if (jdetails >= JSON_DETAILS) {
68 outs << ",\n";
69 const bool is_short = (jdetails < JSON_DETAILS2) ? true : false;
71 }
72 }
73 }
74
75 const virtual bool toJSON(std::stringstream &outs, size_t level, const pesieve::t_json_level &jdetails)
76 {
77 OUT_PADDED(outs, level, "\"code_scan\" : {\n");
79 outs << "\n";
80 OUT_PADDED(outs, level, "}");
81 return true;
82 }
83
85 {
86 return this->relocBase;
87 }
88
93
98
99 size_t generateTags(std::string reportPath);
100
102 std::map<DWORD, t_section_status> sectionToResult;
104 };
105
106
108 class CodeScanner : public ModuleScanner {
109 public:
110
116
117 virtual CodeScanReport* scanRemote();
118
119 void setScanData(bool enable) { this->isScanData = enable; }
120 void setScanInaccessible(bool enable) { this->isScanInaccessible = enable; }
121
122 private:
123
124 size_t collectExecutableSections(RemoteModuleData &remoteModData, std::map<size_t, PeSection*> &sections, CodeScanReport &my_report);
125
126 void freeExecutableSections(std::map<size_t, PeSection*> &sections);
127
128 bool postProcessScan(IN OUT CodeScanReport &report);
129
130 t_scan_status scanUsingBase(IN ULONGLONG load_base, IN std::map<size_t, PeSection*> &remote_code, OUT std::map<DWORD, CodeScanReport::t_section_status> &sectionToResult, OUT PatchList &patchesList);
131
133
134 bool clearIAT(PeSection &originalSec, PeSection &remoteSec);
135
136 bool clearExports(PeSection &originalSec, PeSection &remoteSec);
137
138 bool clearLoadConfig(PeSection &originalSec, PeSection &remoteSec);
139
140 size_t collectPatches(DWORD section_rva, PBYTE orig_code, PBYTE patched_code, size_t code_size, OUT PatchList &patchesList);
141
142 bool isScanData;
143 bool isScanInaccessible;
144 };
145
146}; //namespace pesieve
147
A report from the code scan, generated by CodeScanner.
size_t countSectionsWithStatus(const t_section_status neededStatus)
size_t generateTags(std::string reportPath)
virtual ULONGLONG getRelocBase()
size_t countInaccessibleSections()
virtual const void fieldsToJSON(std::stringstream &outs, size_t level, const pesieve::t_json_level &jdetails)
virtual const bool toJSON(std::stringstream &outs, size_t level, const pesieve::t_json_level &jdetails)
CodeScanReport(HMODULE _module, size_t _moduleSize)
std::map< DWORD, t_section_status > sectionToResult
enum pesieve::CodeScanReport::section_status t_section_status
A scanner for detection of patches in the code.
void setScanInaccessible(bool enable)
void setScanData(bool enable)
virtual CodeScanReport * scanRemote()
CodeScanner(HANDLE hProc, ModuleData &moduleData, RemoteModuleData &remoteModData)
Loads a module from the disk, corresponding to the module in the scanned process' memory.
Definition module_data.h:15
A base class of all the reports detailing on the output of the performed module's scan.
virtual const bool _toJSON(std::stringstream &outs, size_t level=JSON_LEVEL, const pesieve::t_json_level &jdetails=JSON_BASIC)
A base class for all the scanners operating on module data.
RemoteModuleData & remoteModData
const bool toJSON(std::stringstream &outs, size_t level, bool short_info)
Buffers the defined PE section belonging to the module loaded in the scanned process into the local m...
Definition pe_section.h:12
Buffers the data from the module loaded in the scanned process into the local memory.
#define OUT_PADDED(stream, field_size, str)
Definition format_util.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
enum pesieve::module_scan_status t_scan_status
@ JSON_DETAILS
include the basic list patches in the main JSON report
@ JSON_DETAILS2
include the extended list patches in the main JSON report
Final summary about the scanned process.