-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathScanner.cpp
97 lines (83 loc) · 2.51 KB
/
Scanner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <QDebug>
#include <exception>
#include "Scanner.h"
using namespace std;
/*
*
*/
Scanner::Scanner ()
{
s_stop = false;
s_results = new std::vector<Result>;
}
/*
* the entry point of the thread;
*
*/
void Scanner::run ()
{
// We'll just start the event loop here and call all of the required methods via signals
exec();
}
/*
* first cache the entire Virtual Memory Area and then scan it so
* it doesn't hold up the target process while this is searching for the key
* it also drasticaly reduced the number of ptrace calls
*/
void Scanner::newScan (int pid, BYTE key, int type, int operation)
{
try {
//s_pid = pid; s_type = type;
Memory memory(pid);
unsigned long length = 4;//sizeof(key);
std::vector<VMA> areas = memory.vmaList(REGION_ALL);
// if no suitable virtual memory areas found, throw exception
if(areas.size() < 1) {
throw(QString("No suitable memory locations were located"));
}
// go over every virtual memory area
for(unsigned long i = 0; i < areas.size() ; i++) {
if(s_stop) {
break;
}
// no point in scanning a VMA too small to contain the key
if(areas.at(i).size() < length) {
continue;
}
unsigned char workSet[length];
VMA ee = areas.at(i);
ee = memory.readVMA(ee);
unsigned char *buffer = ee.vmaData();
// this loop incrementaly goes over every byte of the VMA and compares
// in sets of sizeof(key) bytes
for(unsigned long j = 0; j < ee.size() - length; j++ ) {
if(s_stop) {
break;
}
//copy sizeof(key) bytes from the vma data
memcpy(workSet,buffer + j,length);
//compare the key with the bytes copied from the vma
if(memcmp(workSet,key,length) == 0 ) {
Result found(static_cast<VMA>(areas.at(i)).baseAddr() + j, workSet, length);
s_results->push_back(found);
}
}
}
if(!s_stop)
emit setResults(*s_results);
//reset the stop switch
s_stop = 0;
} catch(bad_alloc&) {
qDebug() << "failed at memory alloc";
} catch(QString e) {
emit scanFailed("No suitable memory locations were located");
qDebug() << e;
}
}
void Scanner::scan (BYTE key, int operation)
{
}
void Scanner::stop ()
{
s_stop= 1;
}