This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
pagegrab.cs
363 lines (309 loc) · 14.4 KB
/
pagegrab.cs
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* USAGE: pagegrab.exe [-p http(s)://<proxy>:<proxy_port>] [-m <method>] [-d <URL encoded POST data>] [-v] <URL>
*
* -v Print the HTML contents of the response
*
* EXAMPLES:
* pagegrab.exe -m post https://postman-echo.com/post -d query=test -v -p http://127.0.0.1:8000
*/
// To Compile:
// C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:exe /out:pagegrab.exe pagegrab.cs
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
// Needed for SSL / TLS certificate support
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
public class PageGrab
{
public static int Main(string[] args)
{
try
{
if (args.Length == 0 || (args.Length > 0 && args[0] == "/?"))
{
// Print usage
Console.WriteLine(@"USAGE: pagegrab.exe [-p http(s)://<proxy>:<proxy_port>] [-m <method>] [-d <URL encoded POST data>] [-h <header_1_name> <header_1_value> [-h <header_2_name> <header_2_value>]] [-c] [-v] <URL> [/?]
-c Display the SSL / TLS certificate
-v Print the HTML contents of the response");
return 0;
}
string url = "";
string userAgent = "";
string proxyAddress = "";
string method = "GET";
string postData = "";
bool displaySSLCert = false;
bool verbose = false;
Dictionary<string, string> headers = new Dictionary<string, string>();
List<string> unsupportedHeaders = new List<string>();
unsupportedHeaders.Add("Date");
unsupportedHeaders.Add("Host");
unsupportedHeaders.Add("If-Modified-Since");
try
{
string edgeVersion = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\EdgeUpdate\\Clients\\{56EB18F8-B008-4CBD-B6D2-8C97FE7E9062}", "pv", "").ToString();
userAgent = String.Format("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{0} Safari/537.36 Edg/{0}", edgeVersion);
}
catch
{
// Ignore the error for now
}
// Parse arguments
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg.ToUpper())
{
case "-C": // Display SSL / TLS certificate
displaySSLCert = true;
break;
case "-D": // POST data
i++;
try
{
postData = args[i];
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No POST data specified");
}
break;
case "-H": // Header (multiples are allowed)
i++;
// Throw an error and exit if an unsupported header is specified
if (i < args.Length && unsupportedHeaders.Contains(args[i]))
{
throw new ArgumentException("Unsupported header specified:" + args[i]);
}
else if (i + 1 < args.Length)
{
// Treat specified headers are a key-value pair
headers.Add(args[i++], args[i]);
}
else
{
throw new ArgumentException("Incomplete header specified");
}
break;
case "-M": // HTTP Method
i++;
if (i < args.Length)
{
method = args[i].ToUpper();
}
else
{
throw new ArgumentException("No HTTP Method specified");
}
string[] methods = { "GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE" };
// Verify the method is a valid
if (Array.IndexOf(methods, method) == -1)
{
throw new ArgumentException(String.Format("Invalid method '{0}'", method));
}
break;
case "-P": // Proxy
i++;
if (i < args.Length)
{
proxyAddress = args[i];
}
else
{
throw new ArgumentException("No Proxy info specified");
}
Regex rex = new Regex(@"https?://.*:\d{1,5}", RegexOptions.IgnoreCase);
if (!rex.IsMatch(proxyAddress))
{
throw new ArgumentException("Invalid proxy configuration; use 'http(s)://<host>:<port>'");
}
break;
case "-Q": // Query user agent
Console.WriteLine("[*] User-Agent: {0}", userAgent);
return 0;
case "-V": // Verbose output
verbose = true;
break;
default: // URL
url = arg;
break;
}
}
if (url.Length != 0)
{
// Initialize request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = method;
// Add user-specified headers
foreach (KeyValuePair<string, string> header in headers)
{
switch (header.Key)
{
case "Accept":
request.Accept = header.Value;
break;
case "Connection":
request.Connection = header.Value;
break;
case "Content-Length":
request.ContentLength = long.Parse(header.Value);
break;
case "Content-Type":
request.ContentType = header.Value;
break;
case "Expect":
request.Expect = header.Value;
break;
case "Range":
string[] ranges = header.Value.Split('-');
if (ranges.Length >= 2)
{
if (ranges[0] == "")
{
// Specified range is negative
request.AddRange(int.Parse("-" + ranges[1]));
}
else
{
// To and From are specified; additional ranges unsupported at this time
request.AddRange(int.Parse(ranges[0]), int.Parse(ranges[1]));
}
}
else
{
// Specified range is negative
request.AddRange(int.Parse(ranges[0]));
}
break;
case "Referer":
request.Referer = header.Value;
break;
case "Transfer-Encoding":
request.TransferEncoding = header.Value;
break;
case "User-Agent":
userAgent = header.Value;
break;
default:
request.Headers[header.Key] = header.Value;
break;
}
}
// Throw an exception if no user agent was specified and we weren't able to pull the version of Edge to use as a default
// NOTE: This isn't perfect because the AppleWebKit / Safari version number won't match, but it's better probably better than sending nothing
if (userAgent == "")
{
throw new Exception("Unable to auto-detect Edge version; please specify a user agent");
}
request.UserAgent = userAgent;
// Print request data
Console.WriteLine("REQUEST");
Console.WriteLine("---------------");
Console.WriteLine(request.Method + " " + request.RequestUri);
if (proxyAddress.Length != 0)
{
// Manually set the proxy
WebProxy proxy = new WebProxy();
proxy.Address = new Uri(proxyAddress);
proxy.Credentials = CredentialCache.DefaultCredentials;
request.Proxy = proxy;
Console.WriteLine("Proxy: " + proxyAddress);
}
else
{
// Detect the system proxy, if applicable
Uri resource = new Uri(url);
IWebProxy proxy = System.Net.WebRequest.GetSystemWebProxy();
Uri resourceProxy = proxy.GetProxy(resource);
// Test to see whether a proxy was selected
if (resourceProxy == resource)
{
Console.WriteLine("Proxy: None");
}
else
{
Console.WriteLine("Proxy: " + resourceProxy.ToString());
}
}
// Add POST data, if applicable
if (method == "POST")
{
request.ContentType = "application/x-www-form-urlencoded";
// Add the post data to the request
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
Console.WriteLine(request.Headers);
// Print POST data after headers
if (method == "POST")
{
Console.WriteLine(postData);
}
Console.WriteLine("\n\nRESPONSE");
Console.WriteLine("---------------");
// Get the response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status and headers
Console.WriteLine("Status: " + response.StatusDescription, Environment.NewLine);
Console.WriteLine(response.Headers);
// Optionally print the HTML from the response
if (verbose)
{
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
}
response.Close();
if (displaySSLCert)
{
X509Certificate cert = request.ServicePoint.Certificate;
Console.WriteLine("SSL / TLS Certificate");
Console.WriteLine("---------------------");
Console.WriteLine(" Subject: " + cert.Subject);
Console.WriteLine(" Issuer: " + cert.Issuer);
Console.WriteLine(" Valid From: " + cert.GetEffectiveDateString());
Console.WriteLine(" Valid To: " + cert.GetExpirationDateString());
Console.WriteLine(" Serial Number: " + cert.GetSerialNumberString());
Console.WriteLine(" SHA-1 Fingerprint: " + cert.GetCertHashString());
Console.WriteLine(" Public Key: " + cert.GetPublicKeyString());
Console.WriteLine("");
Console.WriteLine("Certificate PEM:");
Console.WriteLine("----BEGIN CERTIFICATE----");
Console.WriteLine(System.Convert.ToBase64String(cert.GetRawCertData()));
Console.WriteLine("----END CERTIFICATE----");
}
}
else
{
throw new ArgumentException("No URL specified");
}
return 0;
}
catch (Exception e)
{
Console.Error.WriteLine("[-] ERROR: {0}", e.Message.Trim());
return 1;
}
finally
{
Console.WriteLine("\nDONE");
}
}
}