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
pe_buffer.h
Go to the documentation of this file.
1#pragma once
2
3#include <peconv.h>
5
6namespace pesieve {
7
8 class PeBuffer {
9 public:
10 PeBuffer(HANDLE _process_hndl, bool _is_refl)
11 : processHndl(_process_hndl), isRefl(_is_refl),
12 vBuf(nullptr), vBufSize(0),
14 {
15 }
16
18 {
19 freeBuffer();
20 }
21
22 bool isFilled()
23 {
24 return (vBuf && vBufSize > 0);
25 }
26
27 bool isValidPe()
28 {
29 if (!vBuf) return false;
30 if (peconv::get_nt_hdrs(vBuf, vBufSize)) {
31 return true;
32 }
33 return false;
34 }
35
36 bool isCode();
37
38 // Returns the size of the internal buffer
39 size_t getBufferSize() const
40 {
41 return vBufSize;
42 }
43
44 // Reads content from the remote process into a buffer. Automatically allocates sutiable buffer.
45 bool readRemote(ULONGLONG module_base, size_t pe_vsize);
46
47 // Fill the content from the cached buffer.
48 bool fillFromBuffer(ULONGLONG module_base, util::ByteBuffer& data_cache);
49
50 // Resizes internal buffer into a new size.
51 // The internal buffer must be non empty.
52 bool resizeBuffer(size_t new_size);
53
54 // Requires the internal buffer to contain a valid PE. Resizes the last section of the PE, to make it fit the new Image Size.
55 // The internal buffer must be non empty, and not smaller than the new Image Size.
56 bool resizeLastSection(size_t new_img_size);
57
58 // Requires the internal buffer to contain a valid PE.
59 // Dumps the PE into a file with a given name.
60 bool dumpPeToFile(IN std::string dumpFileName,
61 IN OUT peconv::t_pe_dump_mode &dumpMode,
62 IN OPTIONAL const peconv::ExportsMapper* exportsMap = NULL,
63 OUT OPTIONAL peconv::ImpsNotCovered *notCovered = NULL
64 );
65
66 bool dumpToFile(IN std::string dumpFileName);
67
68 ULONGLONG getModuleBase() const
69 {
70 return moduleBase;
71 }
72
73 ULONGLONG getRelocBase() const
74 {
75 return relocBase;
76 }
77
78 void setRelocBase(ULONGLONG reloc_base)
79 {
80 relocBase = reloc_base;
81 }
82
83 protected:
84 bool _readRemote(ULONGLONG module_base, size_t pe_vsize);
85
86 size_t calcRemoteImgSize(ULONGLONG module_base) const;
87
88 bool allocBuffer(const size_t pe_vsize)
89 {
90 freeBuffer();
91 vBuf = peconv::alloc_aligned(pe_vsize, PAGE_READWRITE);
92 if (!vBuf) {
93 return false;
94 }
95 vBufSize = pe_vsize;
96 return true;
97 }
98
100 {
101 peconv::free_aligned(vBuf);
102 vBuf = nullptr;
103 vBufSize = 0;
104 }
105
107 bool isRefl;
108 BYTE *vBuf;
109 size_t vBufSize;
110 ULONGLONG moduleBase;
111 ULONGLONG relocBase;
112
113 friend class ImpReconstructor;
114 friend class PeReconstructor;
115 };
116
117}; //namespace pesieve
void setRelocBase(ULONGLONG reloc_base)
Definition pe_buffer.h:78
bool fillFromBuffer(ULONGLONG module_base, util::ByteBuffer &data_cache)
Definition pe_buffer.cpp:44
size_t calcRemoteImgSize(ULONGLONG module_base) const
Definition pe_buffer.cpp:7
ULONGLONG moduleBase
Definition pe_buffer.h:110
size_t getBufferSize() const
Definition pe_buffer.h:39
bool allocBuffer(const size_t pe_vsize)
Definition pe_buffer.h:88
PeBuffer(HANDLE _process_hndl, bool _is_refl)
Definition pe_buffer.h:10
bool _readRemote(ULONGLONG module_base, size_t pe_vsize)
Definition pe_buffer.cpp:60
bool readRemote(ULONGLONG module_base, size_t pe_vsize)
Definition pe_buffer.cpp:27
bool dumpPeToFile(IN std::string dumpFileName, IN OUT peconv::t_pe_dump_mode &dumpMode, IN OPTIONAL const peconv::ExportsMapper *exportsMap=NULL, OUT OPTIONAL peconv::ImpsNotCovered *notCovered=NULL)
ULONGLONG getModuleBase() const
Definition pe_buffer.h:68
bool resizeLastSection(size_t new_img_size)
bool resizeBuffer(size_t new_size)
Definition pe_buffer.cpp:85
ULONGLONG getRelocBase() const
Definition pe_buffer.h:73
ULONGLONG relocBase
Definition pe_buffer.h:111
bool dumpToFile(IN std::string dumpFileName)