-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcisco_type7.c
291 lines (265 loc) · 6.9 KB
/
cisco_type7.c
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
/**
* Cisco Type7 password decoder/encoder
* Copyright (C) 2012 - Nicolas Biscos (buffer at 0x90 period fr )
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
const char xorstring[] = "tfd;kfoA,.iyewrkldJKD";
typedef enum { SUCCESS,
OUT_OF_MEMORY,
DIGITAL_FORMAT_ERROR,
SIZE_NOT_EVEN,
HEXA_FORMAT_ERROR} ErrorCode;
/**
* Display help message
*/
void syntax(void)
{
puts("Syntax: cisco_type7 [-e] <pass>\n"
"\nRetrieve Cisco Type7 clear text password\n"
"If -e is defined, encode in type7 format instead\n");
}
/**
* Return whether a character is a decimal number
* @param c the character to check
* @return !=0 is c is a decimal number
*/
int isDecimal(char c)
{
return ! ( c < '0' || c > '9' );
}
/**
* Return whether a character is in a-f range
* @param c the character to check
* @return !=0 is c is in a-f range
*/
int isHexa1(char c)
{
return ! ( c < 'a' || c > 'f' );
}
/**
* Return whether a character is in A-F range
* @param c the character to check
* @return !=0 is c is in A-F range
*/
int isHexa2(char c)
{
return ! ( c < 'A' || c > 'F' );
}
/**
* Reads one byte and return the value if it is a number character
* @param[in] c the character to check
* @param[out] rslt result
* @param[in] hexBool indicates if should be read as hexadecimal number or decimal number
* @return !=0 on success
*/
int read1byte(char c, unsigned int *rslt, int hexBool)
{
if( isDecimal(c) )
*rslt = c - '0';
else if( hexBool && isHexa1(c) )
*rslt = c - 'a'+10;
else if( hexBool && isHexa2(c) )
*rslt = c - 'A'+10;
else
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
/**
* Reads two bytes and return the value if it is a number character
* @param[in] ptr the string contining the two bytes to read from
* @param[out] rslt result
* @param[in] hexBool indicates if should be read as hexadecimal number or decimal number
* @return !=0 on success
*/
int read2bytes(char *ptr, unsigned int *rslt, int hexBool)
{
char c1, c2;
unsigned int i1, i2;
c1 = *ptr;
c2 = *(ptr+1);
if( EXIT_FAILURE == read1byte(c1, &i1, hexBool) )
return EXIT_FAILURE;
if( EXIT_FAILURE == read1byte(c2, &i2, hexBool) )
return EXIT_FAILURE;
if( hexBool )
*rslt = i1*16;
else
*rslt = i1*10;
*rslt += i2;
return EXIT_SUCCESS;
}
/**
* Encode a string using cisco type7 algorithm
* @param[in] ptr string to encode
* @param[out] output the encoded string
* @return SUCCESS on success, else an error code
*/
ErrorCode encode(char *ptr, char **output)
{
size_t ilen, olen, xorlen;
unsigned int i, index;
char code, c[3];
xorlen = strlen(xorstring);
// allocate ilen*2+2 for output. Indeed, each caracter will be encoded in a
// two digits number, and a 2 bytes prefix is added.
ilen = strlen(ptr);
olen = ilen*2 +2;
*output = (char*)malloc((olen+1)*sizeof(char));
if( NULL == *output )
{
return OUT_OF_MEMORY;
}
memset(*output, 0, olen+1);
// Generate random initial index in xorstring
srand( time(NULL) );
index = rand() % xorlen;
sprintf(*output, "%0.2d", index);
index --;
// Iterate over characters to build the encoded version of the password
for( i = 0 ; i < ilen ; ++ptr, ++i )
{
code = ((*ptr)^xorstring[index])&0xff;
snprintf(c, 3, "%0.2X", code);
strncat(*output, c, olen);
index = index + 1 % xorlen;
}
return SUCCESS;
}
/**
* Decode cisco type7 password
* @param[in] ptr string to decode
* @param[out] output cleartext password
* @return SUCCESS on success, else an error code
*/
ErrorCode decode(char *ptr, char **output)
{
size_t ilen, olen, xorlen;
unsigned int code, i, index;
char c;
xorlen = strlen(xorstring);
// Check that len is even
ilen = strlen(ptr);
if( 0 != ilen % 2 )
{
return SIZE_NOT_EVEN;
}
// output will contain (ilen-2)/2 characters. Indeed, the first 2 bytes indicates the initial
// offset in xorstring
olen = (ilen - 2)/2;
*output = (char*)malloc((olen+1)*sizeof(char));
memset(*output, 0, olen+1);
//Initial offset is decimal only. It starts @ 1, so has to be realigned to match C array
if( EXIT_FAILURE == read2bytes(ptr, &index, 0) )
{
free(*output);
return DIGITAL_FORMAT_ERROR;
}
index--;
// Iterate over hex numbers and recover clear text password
for( i = 0 , ptr+=2 ;
i < olen ;
++i, ptr+=2)
{
if( EXIT_FAILURE == read2bytes(ptr, &code, 1) )
{
free(*output);
return HEXA_FORMAT_ERROR;
}
c = xorstring[index]^code;
*(*output+i) = c;
index = index+1 % xorlen;
}
return SUCCESS;
}
/**
* Return whether a character is a decimal number
* @param c the character to check
* @return !=0 is c is a decimal number
*/
int main(int argc, char **argv)
{
char *buffer, *ptr;
int opt;
ErrorCode (*f)(char *, char**);
unsigned char doencode = 0;
if( 0 == argc )
{
syntax();
return EXIT_FAILURE;
}
while( -1 != (opt = getopt(argc, argv, "eh") ) )
{
switch(opt)
{
case 'e':
doencode = 1;
break;
case 'h':
syntax();
return EXIT_SUCCESS;
case '?':
puts("Syntax error");
return EXIT_FAILURE;
}
}
if( doencode )
{
if( 3 != argc )
{
syntax();
return EXIT_FAILURE;
}
f = encode;
ptr = argv[2];
}
else if( 2 != argc )
{
syntax();
return EXIT_FAILURE;
}
else
{
ptr = argv[1];
f = decode;
}
switch( f(ptr, &buffer) )
{
case OUT_OF_MEMORY:
puts("Memory allocation error");
return EXIT_FAILURE;
break;
case SIZE_NOT_EVEN:
puts("Invalid parameter: size must be even");
return EXIT_FAILURE;
break;
case DIGITAL_FORMAT_ERROR:
puts("Invalid : first 2bytes must be digital");
return EXIT_FAILURE;
break;
case HEXA_FORMAT_ERROR:
puts("Invalid : must be hex format [0-9a-fA-F]");
return EXIT_FAILURE;
break;
case SUCCESS:
puts(buffer);
free(buffer);
break;
}
return EXIT_SUCCESS;
}