forked from ColumPaget/Hashrat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgi.c
More file actions
281 lines (233 loc) · 9.71 KB
/
cgi.c
File metadata and controls
281 lines (233 loc) · 9.71 KB
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
#include "cgi.h"
#include "files.h"
//This file provides an HTTP Common Gateway Interface for Hashrat
#define CGI_DOHASH 1
#define CGI_LF 2
#define CGI_CRLF 4
#define CGI_OCTAL 8
#define CGI_HEX 16
#define CGI_HEXUPPER 32
#define CGI_BASE64 64
#define CGI_DECIMAL 128
#define CGI_HIDETEXT 256
#define CGI_SHOWTEXT 512
void CGIPrintHashTypeSelect(char *CurrType)
{
ListNode *Vars, *Curr;
Vars=ListCreate();
HashAvailableTypes(Vars);
printf("<select name=HashType>\r\n");
Curr=ListGetNext(Vars);
while(Curr)
{
if (StrLen(CurrType) && (strcmp(Curr->Tag,CurrType)==0)) printf("<option selected> %s\r\n",Curr->Tag);
else printf("<option> %s\r\n",Curr->Tag);
Curr=ListGetNext(Curr);
}
printf("</select>\r\n");
ListDestroy(Vars, DestroyString);
}
void CGIPrintRadioButton(char *Name, char *Value, char *Caption, char *Color, char *Checked, char *Title, char *ColSpan)
{
printf("<td bgcolor=\"%s\" title=\"%s\" colspan=\"%s\"> %s <input type=radio title=\"%s\" name=\"%s\" value=\"%s\" %s></td>\n",Color,Title,ColSpan,Caption,Title,Name,Value,Checked);
}
void CGIPrintHashEncodingSelect(int Flags)
{
printf("<td>Encoding:</td>\r\n");
if (Flags & CGI_OCTAL)
{
CGIPrintRadioButton("Encoding", "oct", "Octal", "#BFBB99", "checked", "Encode the hash in octal","1");
CGIPrintRadioButton("Encoding", "dec", "Decimal", "#99BBFF", "", "Encode the hash in decimal","1");
CGIPrintRadioButton("Encoding", "hex", "Hex", "#BBBBFF", "", "Encode the hash in hexadecimal","1");
CGIPrintRadioButton("Encoding", "hexupper", "Uppercase Hex", "#BB88FF", "", "Encode the hash in UPPERCASE hexadecimal","1");
CGIPrintRadioButton("Encoding", "base64", "Base64", "#BBFFFF", "", "Encode the hash with base64","1");
}
else if (Flags & CGI_BASE64)
{
CGIPrintRadioButton("Encoding", "oct", "Octal", "#BFBB99", "", "Encode the hash in octal","1");
CGIPrintRadioButton("Encoding", "dec", "Decimal", "#99BBFF", "", "Encode the hash in decimal","1");
CGIPrintRadioButton("Encoding", "hex", "Hex", "#BBBBFF", "", "Encode the hash in hexadecimal","1");
CGIPrintRadioButton("Encoding", "hexupper", "Uppercase Hex", "#BB88FF", "", "Encode the hash in UPPERCASE hexadecimal","1");
CGIPrintRadioButton("Encoding", "base64", "Base64", "#BBFFFF", "checked", "Encode the hash with base64","1");
}
else if (Flags & CGI_DECIMAL)
{
CGIPrintRadioButton("Encoding", "oct", "Octal", "#BFBB99", "", "Encode the hash in octal","1");
CGIPrintRadioButton("Encoding", "dec", "Decimal", "#99BBFF", "checked", "Encode the hash in decimal","1");
CGIPrintRadioButton("Encoding", "hex", "Hex", "#BBBBFF", "", "Encode the hash in hexadecimal","1");
CGIPrintRadioButton("Encoding", "hexupper", "Uppercase Hex", "#BB88FF", "", "Encode the hash in UPPERCASE hexadecimal","1");
CGIPrintRadioButton("Encoding", "base64", "Base64", "#BBFFFF", "", "Encode the hash with base64","1");
}
else if (Flags & CGI_HEXUPPER)
{
CGIPrintRadioButton("Encoding", "oct", "Octal", "#BFBB99", "", "Encode the hash in octal","1");
CGIPrintRadioButton("Encoding", "dec", "Decimal", "#99BBFF", "", "Encode the hash in decimal","1");
CGIPrintRadioButton("Encoding", "hex", "Hex", "#BBBBFF", "", "Encode the hash in hexadecimal","1");
CGIPrintRadioButton("Encoding", "hexupper", "Uppercase Hex", "#BB88FF", "checked", "Encode the hash in UPPERCASE hexadecimal","1");
CGIPrintRadioButton("Encoding", "base64", "Base64", "#BBFFFF", "", "Encode the hash with base64","1");
}
else
{
CGIPrintRadioButton("Encoding", "oct", "Octal", "#BFBB99", "", "Encode the hash in octal","1");
CGIPrintRadioButton("Encoding", "dec", "Decimal", "#99BBFF", "", "Encode the hash in decimal","1");
CGIPrintRadioButton("Encoding", "hex", "Hex", "#BBBBFF", "checked", "Encode the hash in hexadecimal","1");
CGIPrintRadioButton("Encoding", "hexupper", "Uppercase Hex", "#BB88FF", "", "Encode the hash in UPPERCASE hexadecimal","1");
CGIPrintRadioButton("Encoding", "base64", "Base64", "#BBFFFF", "", "Encode the hash with base64","1");
}
}
void CGIPrintHashLineEndSelect(int Flags)
{
printf("<td>Line Ending:</td>\r\n");
if (Flags & CGI_CRLF)
{
CGIPrintRadioButton("LineEnding", "none", "None", "#FFAAFF", "","","1");
CGIPrintRadioButton("LineEnding", "lf","Unix (newline)", "#AAFFFF", "","add newline before hashing (compatible with things like 'echo text | md5sum')","1");
CGIPrintRadioButton("LineEnding", "crlf", "DOS (carriage-return,newline)", "#AAFFAA", "checked","add carriage-return/newline before hashing","3");
}
else if (Flags & CGI_LF)
{
CGIPrintRadioButton("LineEnding", "none", "None", "#FFAAFF", "","","1");
CGIPrintRadioButton("LineEnding", "lf","Unix (newline)", "#AAFFFF", "checked","add newline before hashing (compatible with things like 'echo text | md5sum')","1");
CGIPrintRadioButton("LineEnding", "crlf", "DOS (carriage-return,newline)", "#AAFFAA", "","add carriage-return/newline before hashing","3");
}
else
{
CGIPrintRadioButton("LineEnding", "none", "None", "#FFAAFF", "checked","","1");
CGIPrintRadioButton("LineEnding", "lf","Unix (newline)", "#AAFFFF", "","add newline before hashing (compatible with things like 'echo text | md5sum')","1");
CGIPrintRadioButton("LineEnding", "crlf", "DOS (carriage-return,newline)", "#AAFFAA", "","add carriage-return/newline before hashing","3");
}
}
void CGIDrawTextInput(int Flags)
{
printf("<td>Text:</td><td colspan=3>\r\n");
if (Flags & CGI_HIDETEXT)
{
printf("<input type=password width=90%% name=\"PlainText\" style=\"font-weight: bold; font-size:16px\">\r\n");
printf(" <input type=submit name=\"ShowText\" value=\"Show Text\" title=\"Show text while typing\">\r\n");
printf("<input type=hidden name=\"HideText\" value=\"Hide Text\">\r\n");
}
else
{
printf("<input type=text width=90%% name=\"PlainText\" style=\"font-weight: bold; font-size:16px\">\r\n");
printf(" <input type=submit name=\"HideText\" value=\"Hide Text\" title=\"Hide text while typing\">\r\n");
}
printf("</td>\r\n");
}
int CGIParseArgs(char **HashType, char **Text)
{
char *QName=NULL, *QValue=NULL, *Name=NULL, *Value=NULL, *ptr;
int Flags=0;
ptr=getenv("QUERY_STRING");
ptr=GetNameValuePair(ptr, "&","=",&QName,&QValue);
while (ptr)
{
Name=HTTPUnQuote(Name,QName);
Value=HTTPUnQuote(Value,QValue);
if (strcmp(Name,"HashType")==0) *HashType=CopyStr(*HashType, Value);
if (strcmp(Name,"PlainText")==0)
{
*Text=CopyStr(*Text, Value);
Flags |= CGI_DOHASH;
}
if (strcmp(Name,"Encoding")==0)
{
if (strcmp(Value,"base64")==0) Flags |= CGI_BASE64;
if (strcmp(Value,"hexupper")==0) Flags |= CGI_HEXUPPER;
if (strcmp(Value,"oct")==0) Flags |= CGI_OCTAL;
if (strcmp(Value,"dec")==0) Flags |= CGI_DECIMAL;
}
if (strcmp(Name,"LineEnding")==0)
{
if (strcmp(Value,"lf")==0) Flags |= CGI_LF;
if (strcmp(Value,"crlf")==0) Flags |= CGI_CRLF;
}
if (strcmp(Name,"HideText")==0) Flags |= CGI_HIDETEXT;
if (strcmp(Name,"ShowText")==0) Flags |= CGI_SHOWTEXT;
ptr=GetNameValuePair(ptr, "&","=",&QName,&QValue);
}
//if we *were* in HIDETEXT mode, but the user has clicked on 'Show Text', then
//override the 'HIDETEXT'
if (Flags & CGI_SHOWTEXT) Flags &= ~CGI_HIDETEXT;
DestroyString(QName);
DestroyString(QValue);
DestroyString(Name);
DestroyString(Value);
return(Flags);
}
/*
void CGIDrawHashResult(char *Hash)
{
printf("<p/>\r\n");
printf("<table align=center>\r\n");
printf("<tr><th>Your Hash is:</th></tr>\r\n");
printf("<tr><td style=\"font-size: 16px\">%s</td></tr>\n",Hash);
printf("<tr><td><textarea rows=2 cols=64 readonly style=\"font-weight: bold; font-size:16px\">%s</textarea></td></tr>\n",Hash);
printf("</table>\r\n");
printf("<p/>\r\n");
}
*/
void CGIDrawHashResult(char *Hash)
{
printf("<p /><p />\r\n");
printf("<div align=center>\r\n");
printf("<h4>Your Hash is:</h4>\r\n");
printf("<h2>%s<h2>\r\n",Hash);
printf("<h4>Here it is in a text box so you can 'select all'</h4>\r\n");
printf("<textarea rows=2 cols=64 readonly style=\"font-weight: bold; font-size:16px\">%s</textarea>\n",Hash);
printf("<p />\r\n");
printf("</div>\r\n");
}
void CGIDisplayPage()
{
char *HashType=NULL, *Text=NULL, *Hash=NULL;
HashratCtx *Ctx;
int Flags;
//We don't need to read anything from disk, so in case we're running
//as root, or something like that, let's try to chroot, so no weird attacks are possible
//shouldn't work, because we shouldn't be running as root
chdir("/var/empty");
chroot(".");
//Send HTTP Headers
printf("Content-type: text/html\r\n");
printf("Connection: close\r\n");
printf("Cache-control: private, max-age=0, no-cache\r\n");
printf("\r\n");
Flags=CGIParseArgs(&HashType, &Text);
printf("<body><html><form>\r\n");
printf("<h2 align=center>Hashrat: Online hash calculator</h2>\r\n");
printf("<div align=center>Version: %s, Licence: GPLv3, Author: Colum Paget, BugReports: colums projects at gmail dot com</div>\r\n",VERSION);
if (Flags & CGI_DOHASH)
{
Ctx=(HashratCtx *) calloc(1,sizeof(HashratCtx));
Ctx->HashType=CopyStr(Ctx->HashType,HashType);
Ctx->Encoding |=ENCODE_HEX;
if (Flags & CGI_BASE64) Ctx->Encoding |=ENCODE_BASE64;
if (Flags & CGI_HEXUPPER) Ctx->Encoding =ENCODE_HEXUPPER;
if (Flags & CGI_DECIMAL) Ctx->Encoding |= ENCODE_DECIMAL;
if (Flags & CGI_OCTAL) Ctx->Encoding |= ENCODE_OCTAL;
if (Flags & CGI_CRLF) Text=CatStr(Text,"\r\n");
if (Flags & CGI_LF) Text=CatStr(Text,"\n");
ProcessData(&Hash, Ctx, Text, StrLen(Text));
CGIDrawHashResult(Hash);
}
printf("<table align=center>\r\n");
printf("<tr>\r\n");
printf("<td>Hash Type:</td><td>");
CGIPrintHashTypeSelect(HashType);
printf("</td></tr>\r\n");
printf("<tr>\r\n");
CGIDrawTextInput(Flags);
printf("<tr>\r\n");
CGIPrintHashEncodingSelect(Flags);
printf("</tr>\r\n");
printf("<tr>\r\n");
CGIPrintHashLineEndSelect(Flags);
printf("</tr>\r\n");
printf("<tr><td colspan=4><input type=submit value=\"Hash it!\"></td></tr>\r\n");
printf("</table>\r\n");
printf("</form></html></body>\r\n");
fflush(NULL);
DestroyString(HashType);
DestroyString(Hash);
DestroyString(Text);
}