-
Notifications
You must be signed in to change notification settings - Fork 4
/
HTTP.cs
165 lines (159 loc) · 7.32 KB
/
HTTP.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
using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Linq;
using System.Collections.Generic;
namespace Reecon
{
class HTTP
{
// Split to 4 in threads?
public (HttpStatusCode StatusCode, string Title, string DNS, WebHeaderCollection Headers, X509Certificate2 SSLCert) GetHTTPInfo(string ip, int port, bool isHTTPS)
{
string pageTitle = "";
string pageData = "";
string dns = "";
string urlPrefix = "http";
HttpStatusCode statusCode = new HttpStatusCode();
if (isHTTPS)
{
urlPrefix += "s";
}
WebHeaderCollection headers = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlPrefix + "://" + ip + ":" + port);
try
{
// Ignore invalid SSL Cert
request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
request.AllowAutoRedirect = false;
// Can crash here due to a WebException on 401 Unauthorized / 403 Forbidden errors, so have to do some things twice
using (var response = request.GetResponse() as HttpWebResponse)
{
statusCode = response.StatusCode;
dns = response.ResponseUri.DnsSafeHost;
headers = response.Headers;
using (StreamReader readStream = new StreamReader(response.GetResponseStream()))
{
pageData = readStream.ReadToEnd();
}
response.Close();
}
}
catch (WebException ex)
{
if (ex.Response == null)
{
// WebClient wc = new WebClient();
// string someString = wc.DownloadString("https://" + ip + ":" + port);
return (statusCode, null, null, null, null);
}
HttpWebResponse response = (HttpWebResponse)ex.Response;
statusCode = response.StatusCode;
dns = response.ResponseUri.DnsSafeHost;
headers = response.Headers;
using (StreamReader readStream = new StreamReader(response.GetResponseStream()))
{
pageData = readStream.ReadToEnd();
}
response.Close();
}
catch (Exception ex)
{
// Something went really wrong...
Console.WriteLine("GetHTTPInfo - Fatal Woof :(: " + ex.Message);
return (statusCode, null, null, null, null);
}
if (pageData.Contains("<title>") && pageData.Contains("</title>"))
{
pageTitle = pageData.Remove(0, pageData.IndexOf("<title>") + "<title>".Length);
pageTitle = pageTitle.Substring(0, pageTitle.IndexOf("</title>"));
}
X509Certificate2 cert = null;
if (request.ServicePoint.Certificate != null)
{
cert = new X509Certificate2(request.ServicePoint.Certificate);
}
return (statusCode, pageTitle, dns, headers, cert);
}
public string FormatResponse(HttpStatusCode StatusCode, string Title, string DNS, WebHeaderCollection Headers, X509Certificate2 SSLCert)
{
string responseText = "";
if (StatusCode != HttpStatusCode.OK)
{
responseText += Environment.NewLine + "- Non-OK Status Code: " + StatusCode.ToString();
if (StatusCode != HttpStatusCode.OK)
{
if (Headers != null && Headers.Get("Location") != null)
{
responseText += Environment.NewLine + "- Location: " + Headers.Get("Location");
}
}
}
if (!string.IsNullOrEmpty(Title))
{
responseText += Environment.NewLine + "- Page Title: " + Title;
}
if (!string.IsNullOrEmpty(DNS))
{
responseText += Environment.NewLine + "- DNS: " + DNS;
}
if (Headers != null)
{
responseText += Environment.NewLine + "- Headers: " + string.Join(",", Headers.AllKeys);
if (Headers.Get("Server") != null)
{
responseText += Environment.NewLine + "- Server: " + Headers.Get("Server");
}
if (Headers.Get("X-Powered-By") != null)
{
responseText += Environment.NewLine + "- X-Powered-By: " + Headers.Get("X-Powered-By");
}
if (Headers.Get("WWW-Authenticate") != null)
{
responseText += Environment.NewLine + "- WWW-Authenticate: " + Headers.Get("WWW-Authenticate");
}
}
if (SSLCert != null)
{
string certIssuer = SSLCert.Issuer;
string certSubject = SSLCert.Subject;
// string certAltName = SSLCert.SubjectName.Name;
responseText += Environment.NewLine + "- SSL Cert Issuer: " + certIssuer;
responseText += Environment.NewLine + "- SSL Cert Subject: " + certSubject;
if (SSLCert.Extensions != null)
{
X509ExtensionCollection extensionCollection = SSLCert.Extensions;
foreach (X509Extension extension in extensionCollection)
{
string extensionType = extension.Oid.FriendlyName;
if (extensionType == "Subject Alternative Name")
{
AsnEncodedData asndata = new AsnEncodedData(extension.Oid, extension.RawData);
List<string> formattedValues = asndata.Format(true).Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();
string itemList = "";
foreach (string item in formattedValues)
{
string theItem = item;
theItem = theItem.Replace("DNS Name=", "");
if (theItem.Contains("("))
{
theItem = theItem.Remove(0, theItem.IndexOf("(") + 1).Replace(")", "");
itemList += theItem + ",";
}
else
{
itemList += theItem + ",";
}
}
itemList = itemList.Trim(',');
responseText += Environment.NewLine + "- Subject Alternative Name: " + itemList;
}
}
}
}
return responseText;
}
}
}