-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbasefind.cpp
233 lines (213 loc) · 5.36 KB
/
basefind.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifdef _OPENMP
# include <omp.h>
//OpenMP support borrowed directly from the documentation
struct MutexType
{
public:
MutexType() { omp_init_lock(&lock); }
~MutexType() { omp_destroy_lock(&lock); }
void Lock() { omp_set_lock(&lock); }
void Unlock() { omp_unset_lock(&lock); }
MutexType(const MutexType& ) { omp_init_lock(&lock); }
MutexType& operator= (const MutexType& ) { return *this; }
private:
omp_lock_t lock;
};
#else
/* A dummy mutex that doesn't actually exclude anything,
* but as there is no parallelism either, no worries. */
struct MutexType
{
void Lock() {}
void Unlock() {}
};
#endif
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <unordered_set>
static const size_t WORD_LEN = 4;//32-bit assumed in a few places below
typedef uint32_t offset_t;
typedef std::vector<std::pair<offset_t,unsigned>> ptable;//pointer table
typedef std::unordered_set<offset_t> stable; //string table
typedef std::vector<std::pair<offset_t,unsigned>> scorecard;
static void high_scores( const scorecard & scores )
{
static const unsigned max = 20;
unsigned i = 0;
printf("\nTop %i base address candidates:\n", max);
for( scorecard::const_reverse_iterator it = scores.rbegin(); i < max && it != scores.rend(); ++it, ++i)
{
printf( "0x%x\t%d\n", it->second, it->first );
}
exit(0);
}
ptable get_pointers( const std::vector<uint8_t> buf )
{
const uint8_t * ptr = &buf[0];
const uint8_t * endptr = ptr + buf.size();
std::map<offset_t,unsigned> temp_table;
for( ; ptr < endptr; ptr += WORD_LEN )
{
offset_t offset;
//implicit assumption that host endian and target endian match
memcpy( &offset, ptr, WORD_LEN );
std::map<offset_t,unsigned>::iterator it = temp_table.find(offset);
if(it == temp_table.end())
{
temp_table[offset]=1;
}
else
{
temp_table[offset]++;
}
}
ptable table;
for(std::map<offset_t,unsigned>::iterator iter = temp_table.begin(); iter != temp_table.end(); ++iter)
{
table.push_back(std::make_pair(iter->first,iter->second));
}
return table;
}
#include <climits>
#include <cstring>
#include <vector>
class stringScanner
{
private:
bool filter[1<<CHAR_BIT];
void set( unsigned char idx )
{
filter[idx] = true;
}
void setRange( unsigned char start, unsigned char end )
{
for( unsigned char i = start; i <= end; ++i ) set( i );
}
public:
stringScanner()
{
memset( filter, 0x00, sizeof(filter) );
setRange('A','Z');
setRange('a','z');
setRange('0','9');
static const char onesies[]="/\\-:.,_$%'\"()[]<> ";
for( size_t i = 0; i < strlen(onesies); ++i )
set( onesies[i] );
}
~stringScanner()
{
}
static const size_t minLength = 10;
bool scan( const uint8_t * ptr )
{
for( const uint8_t * endptr = ptr + minLength; ptr < endptr; ++ptr )
{
if( !filter[*ptr] ) return false;
}
return true;
}
bool scanChar( uint8_t ch )
{
return filter[ch];
}
};
static stable get_strings( const std::vector< uint8_t > & buf )
{
stable results;
stringScanner scanner;
if( buf.size() < stringScanner::minLength )
return results;
for( size_t i = 1; i < ( buf.size() - stringScanner::minLength ); ++i )
{
const uint8_t * ptr = &buf[i];
if( !scanner.scanChar( *( ptr - 1 ) ) )
{
if( scanner.scan( ptr ) )
{
results.insert(i);
}
}
}
return results;
}
#include <fstream>
#include <ios>
int main( int nArgs, const char * const args[] )
{
if( nArgs != 2 )
{
printf("%s: <image>\n", args[0] );
return 1;
}
const char * infile = args[1];
std::vector<uint8_t> fbuf;
{
std::ifstream f( infile, std::ios::binary | std::ios::ate );
if( !f.is_open() )
{
printf("Unable to open %s\n",infile);
return 2;
}
fbuf.resize( f.tellg() );
f.seekg(0);
f.read( (char*)&fbuf[0], fbuf.size() );
if( f.gcount() != fbuf.size() )
{
printf("Unable to fully read %s\n",infile);
return 3;
}
}
printf( "Scanning binary for strings...\n" );
stable str_table = get_strings( fbuf );
printf( "Total strings found: %d\n", str_table.size() );
printf( "Scanning binary for pointers...\n" );
ptable ptr_table = get_pointers( fbuf );
printf( "Total pointers found: %d\n", ptr_table.size() );
// signal.signal(signal.SIGINT, high_scores)
MutexType mutex; //protects next few variables
unsigned top_score = 0;
scorecard scores;
#ifdef _OPENMP
#pragma omp parallel for
#endif
for( offset_t base = 0; base < 0xf0000000UL; base += 0x1000 )
{
#ifndef _OPENMP
//Turn off status display when using OpenMP
//otherwise the numbers will be scrabled
if( base % 0x10000 == 0 )
printf( "Trying base address 0x%x\n", base );
#endif
unsigned score = 0;
for(ptable::iterator iter = ptr_table.begin(); iter != ptr_table.end(); ++iter)
{
offset_t ptr = iter->first;
if( ptr < base )
continue;
if( ptr >= (base + fbuf.size()) )
continue;
unsigned offset = ptr - base;
if( str_table.find(offset) != str_table.end() )
score += iter->second;
}
if( score )
{
mutex.Lock();
scores.push_back( std::make_pair( score, base ) );
if( score > top_score )
{
top_score = score;
printf( "New highest score, 0x%x: %d\n", base, score );
}
mutex.Unlock();
}
}
std::sort(scores.begin(),scores.end() );
high_scores( scores );
}