This repository has been archived by the owner on Jul 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Host.cs
190 lines (153 loc) · 6.26 KB
/
Host.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using System.Net.NetworkInformation;
namespace PassiveScanning
{
[Serializable]
public class Host
{
public IPAddress Address
{
get;
private set;
}
public string AddressString
{
get;
private set;
}
public List<string> HostNames = new List<string>();
public List<Service> Services = new List<Service>();
public Host(IPAddress address)
{
Address = address;
AddressString = Address.ToString();
}
/*public void FillHostnames()
{
HostNames = new List<string>();
/*using (WebClient client = new WebClient())
{
string source = client.DownloadString("http://www.samdns.com/lookup/reverse/" + AddressString + "/");
Regex regex = new Regex("points to: <strong>([^<]+)</strong>");
foreach (Match match in regex.Matches(source))
HostNames.Add(match.Groups[1].Value);
}*/
/*
try
{
IPHostEntry hostInfo = Dns.GetHostEntry(Address);
if (hostInfo.HostName != AddressString)
HostNames.Add(hostInfo.HostName);
if (hostInfo.Aliases.Length > 0)
{
Console.WriteLine("Blabla");
}
}
catch
{
}
}*/
public bool HasHeartbleed(string resultPath)
{
using (StreamReader reader = new StreamReader(Path.Combine(resultPath, "services-Heartbleed")))
{
while (!reader.EndOfStream)
{
string line;
try
{
line = reader.ReadLine();
if (!line.StartsWith(AddressString))
continue;
return true;
}
catch
{
}
}
}
return false;
}
public List<string> GetMissingHTTPHeaders()
{
List<string> missingHeaders = new List<string>();
Regex matchCookieRegex = new Regex("set-cookie: .+", RegexOptions.IgnoreCase);
try
{
Service service = Services.Single(s => s.Name == "HTTP");
JObject data = JObject.Parse(service.RawData);
string banner = Encoding.ASCII.GetString(Convert.FromBase64String(data["data"].Value<string>())).ToUpper();
foreach (Match match in matchCookieRegex.Matches(banner))
{
if (!match.Captures[0].Value.Contains("HttpOnly"))
{
missingHeaders.Add("HttpOnly");
break;
}
}
Regex matchXFrameOptionsRegex = new Regex("X-Frame-Options", RegexOptions.IgnoreCase);
if (!matchXFrameOptionsRegex.Match(banner).Success)
missingHeaders.Add("X-Frame-Options");
Regex matchContentSecurityPolicyRegex = new Regex("Content-Security-Policy", RegexOptions.IgnoreCase);
if (!matchContentSecurityPolicyRegex.Match(banner).Success)
missingHeaders.Add("Content-Security-Policy");
Regex matchContentTypeOptionsRegex = new Regex("X-Content-Type-Options", RegexOptions.IgnoreCase);
if (!matchContentTypeOptionsRegex.Match(banner).Success)
missingHeaders.Add("X-Content-Type-Options");
}
catch
{
}
return missingHeaders;
}
public List<string> GetMissingHTTPSHeaders()
{
List<string> missingHeaders = new List<string>();
Regex matchCookieRegex = new Regex("set-cookie: .+", RegexOptions.IgnoreCase);
try
{
Service service = Services.Single(s => s.Name == "HTTPS");
JObject data = JObject.Parse(service.RawData);
string banner = Encoding.ASCII.GetString(Convert.FromBase64String(data["data"].Value<string>())).ToUpper();
foreach (Match match in matchCookieRegex.Matches(banner))
{
if (!match.Captures[0].Value.Contains("HttpOnly"))
{
if (!missingHeaders.Contains("HttpOnly"))
missingHeaders.Add("HttpOnly");
}
else if (!match.Captures[0].Value.Contains("Secure"))
{
if (!missingHeaders.Contains("Secure"))
missingHeaders.Add("Secure");
}
}
Regex matchXFrameOptionsRegex = new Regex("X-Frame-Options", RegexOptions.IgnoreCase);
if (!matchXFrameOptionsRegex.Match(banner).Success)
missingHeaders.Add("X-Frame-Options");
Regex matchContentSecurityPolicyRegex = new Regex("Content-Security-Policy", RegexOptions.IgnoreCase);
if (!matchContentSecurityPolicyRegex.Match(banner).Success)
missingHeaders.Add("Content-Security-Policy");
Regex matchContentTypeOptionsRegex = new Regex("X-Content-Type-Options", RegexOptions.IgnoreCase);
if (!matchContentTypeOptionsRegex.Match(banner).Success)
missingHeaders.Add("X-Content-Type-Options");
Regex matchStrictTransportSecurityRegex = new Regex("X-Content-Type-Options", RegexOptions.IgnoreCase);
if (!matchStrictTransportSecurityRegex.Match(banner).Success)
missingHeaders.Add("Strict-Transport-Security");
}
catch (Exception e)
{
//Console.WriteLine(e.Message);
//Console.ReadLine();
}
return missingHeaders;
}
}
}