-
Notifications
You must be signed in to change notification settings - Fork 8
/
mtpass.cpp
348 lines (315 loc) · 8.44 KB
/
mtpass.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
mtpass.cpp
This tool decodes user passwords from MikroTik RouterOS user.dat file
license: GPL v2.0
(c) by Mariusz 'manio' Bialonczyk; manio@skyboo.net
v0.1 [2008-01-03]: initial release
v0.2 [2008-01-23]: rewritten in C++
ability to show other users besides admin
added decrypt keys and key prediction
v0.3 [2008-12-08]: figured out xor key generation algorithm, so no more collecting keys
and all users passwords should be decrypted ok :)
v0.4 [2009-03-17]: a lot of fixes
able to decrypt passwords from mikrotik backup files and full flash-dump files
v0.5 [2009-03-26]: fixed decrypting passwords longer then 16 chars
v0.6 [2010-10-12]: fixed compilation problems with newer g++
v0.7 [2011-08-15]: kocour_easy: fix for decoding files from RouterOS 5.5
v0.8 [2012-01-29]: another fixes for decoding new RouterOS files (thanks to NetworkPro)
v0.9 [2012-11-28]: fix compilation with gcc 4.7.2 (thanks to mthqwork)
*/
#include <iostream>
#include <cstdio>
#include <list>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <openssl/md5.h>
using namespace std;
const char* szVerInfo = "mtpass v0.9 - MikroTik RouterOS password recovery tool, (c) 2008-2012 by manio";
const char* szURLInfo = "http://manio.skyboo.net/mikrotik/";
const char* szFormatHdr = "%-4s | %-15s | %-18s | %-14s | %-35s";
const char* szFormatData = "%-4d | %-15s | %-18s | %-14s | %-35s";
const int iFormatLineLength = 92;
int iDebug = 0;
int gRecNumber = 1;
void debug(const char *fmt, ...)
{
if (iDebug == 0)
return;
va_list ap;
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
fflush(stdout);
}
void ASCIIonly(char *text)
{
for (int i=0; i<strlen(text); i++)
{
if ((unsigned char)text[i]<32 || (unsigned char)text[i]>126) //not printable ASCII
{
//terminating the string
text[i] = 0;
break;
}
}
}
class cUserRecord
{
private:
bool bDisabled;
int iRecNumber;
int iPassLen;
char* szPass;
char* szUserName;
char* szComment;
int iPrefKey;
public:
cUserRecord()
{
iPassLen=0;
szPass=NULL;
szUserName=NULL;
szComment=NULL;
bDisabled=false;
iRecNumber=-1;
}
cUserRecord(const cUserRecord &t)
{
bDisabled=t.bDisabled;
iRecNumber=t.iRecNumber;
if (t.szPass==NULL)
szPass=NULL;
else
{
szPass=new char[t.iPassLen];
iPassLen = t.iPassLen;
memcpy(szPass, t.szPass, t.iPassLen);
}
if (t.szUserName==NULL)
szUserName=NULL;
else
{
szUserName=new char[strlen(t.szUserName)+1];
strcpy(szUserName, t.szUserName);
}
if (t.szComment==NULL)
szComment=NULL;
else
{
szComment=new char[strlen(t.szComment)+1];
strcpy(szComment, t.szComment);
}
}
~cUserRecord()
{
if (szPass) delete []szPass;
if (szUserName) delete []szUserName;
if (szComment) delete []szComment;
}
void SetPass(unsigned char* pPass)
{
iPassLen=pPass[0]+1;
szPass=new char[iPassLen];
memcpy(szPass, pPass+1, iPassLen-1);
szPass[iPassLen-1]=0; //terminating
}
void SetDisableFlag(bool bFlag)
{
bDisabled=bFlag;
}
void SetRecNumber(int iRecNo)
{
iRecNumber=iRecNo;
}
void SetUserName(char* NewUserName)
{
if (szUserName) delete []szUserName;
szUserName=new char[strlen(NewUserName)+1];
strcpy(szUserName, NewUserName);
ASCIIonly(szUserName);
if (strcmp(szUserName,"")==0)
{
delete szUserName;
szUserName=NULL;
}
}
void SetComment(char* NewComment)
{
if (szComment) delete []szComment;
szComment=new char[strlen(NewComment)+1];
strcpy(szComment, NewComment);
ASCIIonly(szComment);
}
void DecryptAndShowRecord()
{
if (szUserName==NULL)
return;
static const char magic_string [] = "283i4jfkai3389"; // :)
unsigned char key[MD5_DIGEST_LENGTH];
char user_magic[200];
strncpy(user_magic, szUserName, 199-strlen(magic_string));
strcat(user_magic, magic_string);
MD5((unsigned char*)user_magic, strlen(user_magic), key);
if (szPass != NULL)
{
for (int i=0; i<(iPassLen-1); i++)
szPass[i] = szPass[i] ^ key[i%MD5_DIGEST_LENGTH]; //decoding (xor)
ASCIIonly(szPass);
}
fprintf(stdout, szFormatData, gRecNumber++, szUserName, szPass==NULL ? "<BLANK PASSWORD>" : szPass, bDisabled ? "USER DISABLED" : "", szComment==NULL ? "" : szComment);
fprintf(stdout, "\n");
}
};
int main(int argc, char **argv)
{
unsigned char *buff;
int fd;
list<cUserRecord> tabUser;
int i, bytes, iKeys;
fprintf(stdout, "%s\n%s\n\n", szVerInfo, szURLInfo);
if (argc <= 1)
{
fprintf(stdout, "usage: %s [-d] input_file\n", argv[0]);
fprintf(stdout, "input_file: RouterOS userdata file from /nova/store/user.dat\n");
return -1;
}
if (strcmp(argv[1],"-d")==0)
iDebug = 1;
fd = open(argv[1+iDebug], O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "Error: could not open file: %s\n", argv[1+iDebug]);
return -2;
}
bytes = lseek(fd, 0, SEEK_END);
fprintf(stdout, "Reading file %s, %d bytes long\n", argv[1+iDebug], bytes);
buff = new unsigned char[bytes];
if (buff==NULL)
{
fprintf(stderr, "Error: cannot allocate buffer\n");
return -3;
}
cUserRecord *ptr=NULL;
lseek(fd, 0, SEEK_SET);
if (read(fd, buff, bytes) == bytes)
{
for (i=0; i<bytes; i++)
{
//searching for StartOfRecord
if (i+2>=bytes) break;
if ((buff[i]==0x4d) && (buff[i+1]==0x32) && (buff[i+2]==0x0a || buff[i+2]==0x10))
{
debug("Probably user record at offset 0x%.5x\n",i);
ptr=new cUserRecord;
//some bytes ahead is enable/disable flag
i += (buff[i+2] - 5);
if (i>=bytes) break;
if (buff[i-1] == 0xfe)
ptr->SetDisableFlag(bool(buff[i]));
//cout << (int)buff[i] << endl;
//searching for StartOfRecNumber
if (i+3>=bytes) break;
while (!( (buff[i]==0x01) && ((buff[i+1]==0x00)||(buff[i+1]==0x20)) && (buff[i+3]==0x09||buff[i+3]==0x20)))
{
i++;
if (i+3>=bytes) break;
}
i+=4;
if (i>=bytes) break;
debug("SORn: 0x%X\n", i);
//cout << (int)buff[i] << endl;
ptr->SetRecNumber(buff[i]);
//is there a comment?
i+=18;
if ((i+4)>=bytes) break;
if ((!((buff[i+1]==0x11) && (buff[i+2]==0x20) && (buff[i+3]==0x20) && (buff[i+4]==0x21))) && (buff[i-5]==0x03 && (buff[i]!=0x00))) //there is comment
{
if ((i+1)+buff[i]>=bytes) break;
debug("SOC: 0x%X\n", i+1);
char *tmp=new char[buff[i]+1];
memcpy(tmp,(void*)&buff[i+1],buff[i]);
//terminating the string
tmp[buff[i]]=0;
ptr->SetComment(tmp);
//cout <<tmp<<endl;
delete tmp;
i+=buff[i];
}
else //there is no comment
i-=18;
//searching for StartOfPassword
if (i+4>=bytes) break;
while (!((buff[i]==0x11) && (buff[i+3]==0x21) && ((buff[i+4] % MD5_DIGEST_LENGTH)==0)))
{
i++;
if (i+4>=bytes) break;
}
i+=5;
if ((i+3)>=bytes) break;
debug("SOP: 0x%X\n", i);
if ((buff[i-1]!=0x00) && !((buff[i]==0x01) && ((buff[i+1]==0x20 && buff[i+2]==0x20)||(buff[i+1]==0x00 && buff[i+2]==0x00)) && (buff[i+3]==0x21)))
{
//copying pass
ptr->SetPass(&buff[i-1]);
i+=buff[i-1];
}
//searching for StartOfUsername
if (i+3>=bytes) break;
while (!((buff[i]==0x01) && (buff[i+3]==0x21)))
{
i++;
if (i+3>=bytes) break;
}
i+=4;
if (i>=bytes) break;
if (buff[i]!=0x00)
{
if (i+buff[i]>=bytes) break;
debug("SOU: 0x%X\n", i);
char *tmp=new char[buff[i]+1];
memcpy(tmp,(void*)&buff[i+1],buff[i]);
//terminating the string
tmp[buff[i]]=0;
//cout <<tmp<<endl;
ptr->SetUserName(tmp);
delete tmp;
i+=buff[i];
}
tabUser.push_back(*ptr);
}
//clean if we need to
if (ptr)
{
delete ptr;
ptr=NULL;
}
}
delete buff;
}
else
{
fprintf(stderr, "Error: can't read file\n");
return -4;
}
close(fd);
//show the results
list<cUserRecord>::iterator iter1;
list<cUserRecord>::iterator iter2;
iter1 = tabUser.begin();
iter2 = tabUser.end();
//print header
fprintf(stdout, "\n");
fprintf(stdout, szFormatHdr, "Rec#", "Username", "Password", "Disable flag", "User comment");
fprintf(stdout, "\n");
for (int i=0; i<iFormatLineLength; i++) fprintf(stdout, "-");
fprintf(stdout, "\n");
//print data
for (; iter1!=iter2; ++iter1)
iter1->DecryptAndShowRecord();
if (gRecNumber == 1)
fprintf(stdout, "Sorry - no passwords were found in the file\n");
fprintf(stdout, "\n");
return 0;
}