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
|
#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <iomanip>
#include <string.h>
#include <openssl/sha.h>
using namespace std;
int v = 0;
string magic_comp = "fc1c";
static const string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
void escapeOutput(string &input, string &outstr)
{
outstr.clear();
int idx = 0; int lastnl = 0;
for (string::iterator it=input.begin(); it!=input.end(); ++it) {
stringstream conv;
lastnl = 0;
if (isprint(*it)) {
if (idx > 0) {
outstr += ' ';
idx++;
}
outstr += *it;
idx++;
if(idx > 74) {
outstr += '\n';
lastnl = 1;
}
} else {
conv.str(string());
conv << "\\x" << hex << setw(2) << ((unsigned int)*it & 0xff);
if (idx > 0) {
outstr += ' ';
idx++;
}
idx+=5;
outstr += conv.str();
if(idx > 74) {
outstr += '\n';
lastnl = 1;
}
}
}
if(lastnl)
if (outstr.size () > 0) outstr.resize (outstr.size () - 1);
}
void binTo64(const unsigned char *data, unsigned int datalen, string &str, unsigned int strLen)
{
unsigned int idx = 0;
int sex;
unsigned char incoming[3];
int last;
str.clear();
(v == 1) && cout << "binTo64 datalen: " << datalen << endl;
(v == 1) && cout << "binTo64 strLen: " << strLen << endl;
for(unsigned int i=0; i<strLen; i+=4) {
if(idx >= datalen)
break;
if (idx + 3 > datalen) {
memset(incoming,0,3);
memcpy(incoming,data+idx,datalen-idx);
last = datalen - idx - 1;
} else {
memcpy(incoming,data+idx,sizeof incoming);
last = 4;
}
idx += 3;
(v == 1) && cout << hex << "in[0]: " << (int)incoming[0] << " in[1]: " << (int)incoming[1]<< " in[2] " << (int)incoming[2] << endl;
sex = (incoming[0] >> 2);
str += base64_chars[sex];
(v == 1) && cout << "1st, 4th inp char: " << hex << sex << " to: " << str << endl;
sex = (incoming[1] >> 4);
(v == 1) && cout << "2nd, 5th inp char: " << hex << sex << endl;
sex += (incoming[0] << 4) & 0x30;
str += base64_chars[sex];
(v == 1) && cout << "3rd, 6th inp char: " << hex << sex << " to: " << str << endl;
(v == 1) && cout << "last: " << last << endl;
if (last == 0)
continue;
sex = (incoming[2] >> 6);
sex += ((incoming[1] << 2) & 0x3c);
str += base64_chars[sex];
if(last == 1)
continue;
sex = (incoming[2] & 0x3f);
str += base64_chars[sex];
(v == 1) && cout << "Converted " << idx << "chars: " << str << endl;
}
while (str.length() < strLen)
str += '=';
return;
}
int badPassword(string &passwd)
{
unsigned char a[3];
int i = 0;
int lower=0, upper=0, numeric=0, special=0;
a[0] = a[1] = a[2] = 0;
for (string::iterator it=passwd.begin(); it!=passwd.end(); ++it) {
a[0] = *it;
if (i > 1) {
if ((a[0] == a[1]) && (a[1] == a[2]))
return 1;
if ((a[0] == a[1]+1) && (a[1] == a[2]+1))
return 1;
if ((a[0] == a[1]-1) && (a[1] == a[2]-1))
return 1;
}
a[2] = a[1];
a[1] = a[0];
if(islower(*it))
lower=1;
if(isupper(*it))
upper=1;
if(isdigit(*it))
numeric=1;
if((*it == '/') || (*it == '+'))
special=1;
i++;
}
if (lower + upper + numeric + special < 3)
return 1;
return 0;
}
void usage(void)
{
cout << "usage:" << endl <<
" mts-hashpwd [-v] [[-d did] [-m mac] | [-p password]] salt" << endl <<
" -v verbose" << endl <<
" -d did Device ID (serial #)" << endl <<
" -m mac Ethernet mac address" << endl <<
" -p password" << endl <<
"Either password must be supplied, or Device-ID and Ethernet Mac address" << endl <<
"salt is required." <<endl;
exit(1);
}
int main(int argc, char **argv) {
int opt = 0, p = 0, d = 0, m = 0;
unsigned long long fudge = 0;
short unsigned int prefix;
string did, mac, pwd, salt;
while ((opt = getopt(argc,argv,"d:m:p:v")) != EOF)
switch(opt)
{
case 'v': v = 1; cout << " verbose" <<endl; break;
case 'd': d = 1; did = optarg ; (v==1) && cout << "device-id is " << did << endl; break;
case 'm': m = 1; mac = optarg ; (v==1) && cout << "Ethernet mac is " << mac << endl; break;
case 'p': p = 1; pwd = optarg ; (v==1) && cout << "User defined password is \"" << pwd << "\"" << endl; break;
case '?': usage(); break;
default: cout<<endl; abort();
}
(v == 1) && cout << "optind is " << optind << " and argc is " << argc << endl;
if (v == 1) {
cout << "arguments:" << endl;
for (int i = 0; i < argc; i++)
cout << " argv[" << i << "]=\"" << argv[i] << "\"" << endl;
}
if ((p && d) || (p && m)) {
cout << "Must use either a supplied password or Device ID and Ethernet MAC address, but not all three." << endl;
usage();
}
if ((d && !m) || (m && !d)) {
cout << "Must have both a Device-ID and an Ethernet MAC address." << endl;
usage();
}
if (optind != argc-1) {
cout << "Must have exactly one salt argument." << endl;
usage();
}
salt = argv[optind];
(v == 1) && cout << "Salt is \"" << salt << "\"" << endl;
prefix = strtol(magic_comp.c_str(),NULL,16);
prefix = ~prefix;
stringstream prefixStream;
prefixStream << setw(4) << setfill('0') << hex << prefix;
(v == 1) && cout << "prefix is " << prefixStream.str() << endl;
string passwd_str;
string passwd0, passwdnew;
SHA256_CTX sha256;
unsigned char hash[SHA256_DIGEST_LENGTH];
if(!p) {
passwd0 = did + "|" + mac;
passwdnew = passwd0;
while (1) {
unsigned char append[9];
(v == 1) && cout << "pwdinput: " << passwdnew << endl;
SHA256_Init(&sha256);
SHA256_Update(&sha256,passwdnew.c_str(),passwdnew.length());
SHA256_Final(hash, &sha256);
if (v == 1) {
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
cout << hex << setw(2) << setfill('0') << (int)hash[i];
cout << endl;
}
pwd = "";
binTo64(hash,SHA256_DIGEST_LENGTH,pwd,8);
(v == 1) && cout << "passwd_str: " << pwd << endl;
if(!badPassword(pwd))
break;
fudge++;
memcpy(append,&fudge,sizeof fudge);
append[8]=0;
string b64Appendstr;
binTo64(append,8,b64Appendstr,12);
unsigned int found = b64Appendstr.find_first_of('=');
if(found != string::npos)
b64Appendstr = b64Appendstr.substr(0,found);
passwdnew = passwd0 + b64Appendstr;
}
#ifdef Runtest
// Test
string testencode = "aX2t^%~Q\377\377\xef", outencode, escaped ;
escapeOutput(testencode,escaped);
binTo64((unsigned char *)testencode.c_str(),testencode.length(),outencode,testencode.length()*2);
cout << "TEST encode input: " << escaped << " Output: " <<
outencode << endl;
#endif
}
cout << "pass=" << pwd << endl;
passwd_str = prefixStream.str() + pwd + salt;
SHA256_Init(&sha256);
SHA256_Update(&sha256,passwd_str.c_str(),passwd_str.length());
SHA256_Final(hash, &sha256);
cout << "password_hash=";
cout << hex;
for (int i=0; i < SHA256_DIGEST_LENGTH; i++)
cout << setw(2) << setfill('0') << (int)hash[i];
cout << endl;
cout << "salt=" << salt << endl;
return 0;
}
|