-
Notifications
You must be signed in to change notification settings - Fork 2
/
knotty.php
318 lines (294 loc) · 10.1 KB
/
knotty.php
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
#!/usr/bin/php
<?php
$arr2str = strval(implode(" ", $argv));
# length of supported algorithms in hash_algos
$_supportedLen = count(hash_algos());
# help function
function generateHelp()
{
echo "\t\e[1;34;40mknotty.php\e[0m\n\n";
echo "\t\e[0;31;40mUsage:\e[0m\n";
echo "\tphp knotty.php plaintext=[PLAINTEXT] [OPTIONS]";
echo "\n\tphp knotty.php file=[FILENAME] [OPTIONS]";
echo "\n";
echo "\n\t\e[1;33;40mREQUIRED ARGUMENTS:\e[0m\n\tEither any of the below\n\n";
echo "\t1. \e[1;37;40mplaintext=[PLAINTEXT]\e[0m\n\tExample: plaintext=passphrase\n\n";
echo "\t2. \e[1;37;40mfile=[FILENAME]\e[0m\n\tExample: file=/home/user/file.txt\n\n";
echo "\n\t\e[1;33;40mOPTIONAL ARGUMENTS:\e[0m\n\n\t** salt1 & salt2 can also be used together **\n";
echo "\n\t\e[1;37;40msalt1=[SALT]\t(Append salt before the plaintext)\e[0m\n\tExample: plaintext=passphrase salt1=abc123 (abc123passphrase)\n";
echo "\t\e[1;37;40msalt2=[SALT]\t(Append salt AFTER the plaintext)\e[0m\n\tExample: plaintext=passphrase salt2=abc123 (passphraseabc123)\n\n";
echo "\t\e[1;37;40mformat=[0 (default), 1, 2, 3]\t(Output Format, if not specified format=0 is used)\e[0m\n\tformat=0\t(Output hashValue)\n\tformat=1\t(Output hashValue:algorithmName)\n\tformat=2\t(Output hashValue:plaintext)\n\tformat=3\t(Output algorithmName:hashValue:plaintext)\n\tExample: plaintext=passphrase format=1\n\t\n";
echo "\t\e[1;37;40malgo=[all (default), algoName, algoIndex]\t(Algorithm to Compute Hash, if not specified algo=all is used)\e[0m\n\talgo=all\t(Hash will be calculated for all supported algorithms mentioned below)\n\tformat=2\t(MD5 algorithm)\n\tformat=md5\t(MD5 algorithm)\n\tExample: plaintext=passphrase algo=2\n\t\n\n";
echo "\t\e[0;30;47mSupported Algorithms\e[0m";
print "\n\tEither Provide the Name or Associated Index Value\n";
echo "\n\t\e[0;33;40mAlgorithm Index\e[0m" . "\t\t==========\t\t" . "\e[0;36;40mAlgorithm Name\e[0m\n\n";
echo "\t all" . "\t\t\t==========\t\t " . "all\t(All below algorithms will be selected)\n";
foreach (hash_algos() as $key => $value)
{
echo "\t " . $key . "\t\t\t==========\t\t " . $value . "\n";
}
echo "\n\t\e[1;37;40mEXAMPLE:\e[0m\n\tphp knotty.php plaintext=password salt1=abc123 salt2=xyz format=2 algo=sha1\n\tphp knotty.php plaintext=password salt1=abc123 salt2=xyz format=2 algo=3\n\tphp knotty.php file=/home/user/file.txt algo=all format=1\n\t\n\n";
exit();
}
# if help is supplied then call help function
if (strpos($arr2str, " help ") !== false || strpos($arr2str, " help") !== false)
{
generateHelp();
}
# retrieve algorithm name (if none passed, then use all)
function algoName()
{
global $arr2str, $argv;
if (strpos($arr2str, "algo") !== false)
{
foreach ($argv as $key => $value)
{
$tmpA = explode("=", $value);
if (strtolower($tmpA[0]) == "algo")
{
$algo = $tmpA[1];
break;
}
}
}
else
{
$algo = "all";
}
return $algo;
}
# retrieve format of the output (if none passed, 0 is set)
function format()
{
global $arr2str, $argv;
if (strpos($arr2str, "format") !== false)
{
foreach ($argv as $key => $value)
{
$tmpF = explode("=", $value);
if (strtolower($tmpF[0]) == "format")
{
$format = $tmpF[1];
break;
}
}
}
else
{
$format = "0";
}
if (intval($format) < 4 && intval($format) >= 0)
{
return $format;
}
else
{
generateHelp();
}
}
# check whether plaintext is passed or a file
function plNfi()
{
global $arr2str, $argv;
if (strpos($arr2str, "plaintext") !== false xor strpos($arr2str, "file") !== false)
{
if (strpos($arr2str, "plaintext") !== false)
{
foreach ($argv as $key => $value)
{
$tmpPT = explode("=", $value);
if (strtolower($tmpPT[0]) == "plaintext")
{
$plaintext = $tmpPT[1];
break;
}
}
# check if salt1 is passed
if (strpos($arr2str, "salt1") !== false)
{
foreach ($argv as $key => $value)
{
$tmpS1 = explode("=", $value);
if (strtolower($tmpS1[0]) == "salt1")
{
$plaintext = $tmpS1[1] . $plaintext;
break;
}
}
}
# check if salt2 is passed
if (strpos($arr2str, "salt2") !== false)
{
foreach ($argv as $key => $value)
{
$tmpS2 = explode("=", $value);
if (strtolower($tmpS2[0]) == "salt2")
{
$plaintext = $plaintext . $tmpS2[1];
break;
}
}
}
}
else
{
foreach ($argv as $key => $value)
{
$tmpFI = explode("=", $value);
if (strtolower($tmpFI[0]) == "file")
{
$plaintext = $tmpFI[1];
break;
}
}
}
# if none is passed, call help function
}
else
{
echo "Either plaintext or file Required!\n";
generateHelp();
}
return $plaintext;
}
# call above function and save the return values in variables
$algo = algoName();
$format = format();
$plaintext = plNfi();
# if algorithm index is pass then compute hash using index
if (is_numeric($algo) && $algo < $_supportedLen)
{
if (strpos($arr2str, "file") !== false)
{
if (strval($format) == "0")
{
echo hash_file(hash_algos() [intval($algo) ], $plaintext) . "\n";
}
elseif (strval($format) == "1")
{
echo hash_file(hash_algos() [intval($algo) ], $plaintext) . ":" . hash_algos() [intval($algo) ] . "\n";
}
elseif (strval($format) == "2")
{
echo hash_file(hash_algos() [intval($algo) ], $plaintext) . ":" . $plaintext . "\n";
}
elseif (strval($format) == "3")
{
echo hash_algos() [intval($algo) ] . ":" . hash_file(hash_algos() [intval($algo) ], $plaintext) . ":" . $plaintext . "\n";
}
# generate output in appropriate format
}
else
{
if (strval($format) == "0")
{
echo hash(hash_algos() [intval($algo) ], $plaintext) . "\n";
}
elseif (strval($format) == "1")
{
echo hash(hash_algos() [intval($algo) ], $plaintext) . ":" . hash_algos() [intval($algo) ] . "\n";
}
elseif (strval($format) == "2")
{
echo hash(hash_algos() [intval($algo) ], $plaintext) . ":" . $plaintext . "\n";
}
elseif (strval($format) == "3")
{
echo hash_algos() [intval($algo) ] . ":" . hash(hash_algos() [intval($algo) ], $plaintext) . ":" . $plaintext . "\n";
}
}
# if algorithm name is passed then check whether the name is valid or not
}
else
{
if (strtolower($algo) == "all")
{
foreach (hash_algos() as $key => $value)
{
if (strpos($arr2str, "file") !== false)
{
if (strval($format) == "0")
{
echo hash_file($value, $plaintext) . "\n";
}
elseif (strval($format) == "1")
{
echo hash_file($value, $plaintext) . ":" . $value . "\n";
}
elseif (strval($format) == "2")
{
echo hash_file($value, $plaintext) . ":" . $plaintext . "\n";
}
elseif (strval($format) == "3")
{
echo $value . ":" . hash_file($value, $plaintext) . ":" . $plaintext . "\n";
}
}
else
{
if (strval($format) == "0")
{
echo hash($value, $plaintext) . "\n";
}
elseif (strval($format) == "1")
{
echo hash($value, $plaintext) . ":" . $value . "\n";
}
elseif (strval($format) == "2")
{
echo hash($value, $plaintext) . ":" . $plaintext . "\n";
}
elseif (strval($format) == "3")
{
echo $value . ":" . hash($value, $plaintext) . ":" . $plaintext . "\n";
}
}
}
}
elseif (in_array(strtolower($algo) , hash_algos()))
{
if (strpos($arr2str, "file") !== false)
{
if (strval($format) == "0")
{
echo hash_file($algo, $plaintext) . "\n";
}
elseif (strval($format) == "1")
{
echo hash_file($algo, $plaintext) . ":" . $value . "\n";
}
elseif (strval($format) == "2")
{
echo hash_file($algo, $plaintext) . ":" . $plaintext . "\n";
}
elseif (strval($format) == "3")
{
echo $value . ":" . hash_file($algo, $plaintext) . ":" . $plaintext . "\n";
}
}
else
{
if (strval($format) == "0")
{
echo hash($algo, $plaintext) . "\n";
}
elseif (strval($format) == "1")
{
echo hash($algo, $plaintext) . ":" . $algo . "\n";
}
elseif (strval($format) == "2")
{
echo hash($algo, $plaintext) . ":" . $plaintext . "\n";
}
elseif (strval($format) == "3")
{
echo $algo . ":" . hash($algo, $plaintext) . ":" . $plaintext . "\n";
}
}
}
else
{
echo "Error Occured!";
generateHelp();
}
}