-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
229 lines (198 loc) · 9.91 KB
/
Program.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
using Microsoft.Web.Administration;
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace spamblocker
{
class Program
{
private static void Main(string[] args)
{
var urllist = "";
var sourceslist = "https://raw.githubusercontent.com/nbrightproject/spamsafelists/master/spam-blacklist-sources.txt";
if (args.Length == 1) sourceslist = args[0];
// get the source list into an array
var sourceslists = GetUrlListFromHttp(sourceslist);
var slists = sourceslists.Split(Environment.NewLine.ToCharArray());
// loop on each source and add to the list.
foreach (var source in slists)
{
if (source.ToLower().StartsWith("http"))
{
urllist += GetUrlListFromHttp(source);
urllist += Environment.NewLine;
}
}
urllist = urllist.Trim();
// print out page source
var lines = urllist.Split(Environment.NewLine.ToCharArray());
StringBuilder sbWrite = new StringBuilder();
sbWrite.Append("<rule name=\"Spam-Referrals\">" + Environment.NewLine);
sbWrite.Append("<match url=\".*\" />" + Environment.NewLine);
sbWrite.Append("<conditions>" + Environment.NewLine);
sbWrite.Append("<add input=\"{HTTP_REFERER}\" matchType=\"Pattern\" pattern=\"");
var regx = "(^http://)(";
foreach (var line in lines)
{
if (line.Trim() != "")
{
string sOut = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(line));
if (!sOut.Contains("?") && sOut.Trim(' ') != "") // remove any invalid chars lines
{
if (!regx.Contains(sOut.Replace(".", "\\.")))
{
regx += ".*" + sOut.Replace(".", "\\.") + "|";
}
}
}
}
regx = regx.TrimEnd('|') + ")";
sbWrite.Append(regx + "\" ignoreCase=\"true\" negate=\"false\" />" + Environment.NewLine);
sbWrite.Append("</conditions>" + Environment.NewLine);
sbWrite.Append("<action type=\"CustomResponse\" statusCode=\"403\" statusReason=\"Forbidden: Access is denied.\" statusDescription=\"You do not have permission to view this directory or page.\" />" + Environment.NewLine);
sbWrite.Append("</rule>" + Environment.NewLine);
// check for changes
if (File.Exists(".\\spammerblock_regexpr.txt"))
{
var oldregx = File.ReadAllText(".\\spammerblock_regexpr.txt");
if (oldregx != regx)
{
File.WriteAllText(".\\spammerblock_regexpr.txt", regx);
File.WriteAllText(".\\spammerblock_xmlnode.txt", sbWrite.ToString());
UpdateIIS(sbWrite.ToString());
}
}
else
{
File.WriteAllText(".\\spammerblock_regexpr.txt", regx);
File.WriteAllText(".\\spammerblock_xmlnode.txt", sbWrite.ToString());
UpdateIIS(sbWrite.ToString());
}
}
private static String GetUrlListFromHttp(String fullurl)
{
try
{
StringBuilder sb = new StringBuilder();
// used on each read operation
Char[] read = new Char[256];
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(fullurl);
// execute the request
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
if (resStream != null)
{
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(resStream, encode);
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = readStream.Read(read, 0, 256);
// make sure we read some data
if (count != 0)
{
tempString = new String(read, 0, count);
// continue building the string
sb.Append(tempString);
}
} while (count > 0); // any more data to read?
}
return sb.ToString();
}
catch (Exception)
{
return "";
}
}
private static void UpdateIIS(String rewriteXmlNode)
{
if (rewriteXmlNode != "")
{
using (ServerManager serverManager = new ServerManager())
{
Configuration configapp = serverManager.GetApplicationHostConfiguration();
try
{
ConfigurationSection webServerSection = configapp.GetSection("system.webServer/rewrite");
ConfigurationElementCollection webServerCollection = webServerSection.GetCollection();
ConfigurationElement addElement = webServerCollection.CreateElement("rewrite");
webServerCollection.Add(addElement);
}
catch (Exception ex)
{
Console.Write(ex.ToString());
throw ex;
}
serverManager.CommitChanges();
}
File.Copy("C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config", "C:\\Windows\\System32\\inetsrv\\config\\applicationHost.copy", true);
var hostconfigpath = @"C:\Windows\System32\inetsrv\config\applicationHost.copy";
if (File.Exists(hostconfigpath))
{
try
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(hostconfigpath);
// create a global element which will be injected
XmlNode newRuleOuter = xmlDoc.CreateNode(XmlNodeType.Element, "newRule", null);
newRuleOuter.InnerXml = rewriteXmlNode;
XmlNode newRule = newRuleOuter.SelectSingleNode("rule");
var rewriteNode = xmlDoc.SelectSingleNode("configuration/system.webServer/rewrite");
if (rewriteNode == null)
{
var webServerNod = xmlDoc.SelectSingleNode("configuration/system.webServer"); // should always exists
XmlNode rewriteRules = xmlDoc.CreateNode(XmlNodeType.Element, "rewrite", null);
webServerNod?.AppendChild(rewriteRules);
rewriteNode = xmlDoc.SelectSingleNode("configuration/system.webServer/rewrite");
}
var globalRulesNode = xmlDoc.SelectSingleNode("configuration/system.webServer/rewrite/globalRules");
if (globalRulesNode == null)
{
// create globalrules node and inject rule node/
XmlNode newglobalRules = xmlDoc.CreateNode(XmlNodeType.Element, "globalRules", null);
newglobalRules.InnerXml = rewriteXmlNode;
rewriteNode?.AppendChild(newglobalRules);
globalRulesNode = xmlDoc.SelectSingleNode("configuration/system.webServer/rewrite/globalRules");
}
// rules exists, find name="Spam-Referrals" and replace, if diff
XmlNode newruleNode = xmlDoc.CreateNode(XmlNodeType.Element, "rule", null);
XmlAttribute nameatt = xmlDoc.CreateAttribute("name");
nameatt.Value = "Spam-Referrals";
newruleNode.Attributes.Append(nameatt);
var innerXml = rewriteXmlNode.Replace("<rule name=\"Spam-Referrals\">", "").Replace("</rule>", "").Trim() + "\r\n";
newruleNode.InnerXml = innerXml;
var ruleNode = xmlDoc.SelectSingleNode("configuration/system.webServer/rewrite/globalRules/rule[@name='Spam-Referrals']");
if (ruleNode == null)
{
globalRulesNode.AppendChild(newruleNode);
}
else
{
globalRulesNode.ReplaceChild(newruleNode, ruleNode);
}
xmlDoc.Save(".\\spammerblock_config.xml");
// now we've created it overwrite live config file
File.Copy("C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config", "C:\\Windows\\System32\\inetsrv\\config\\applicationHost.copy", true);
File.Copy(".\\spammerblock_config.xml", "C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config", true);
}
catch (Exception e)
{
File.WriteAllText(".\\spammerblock_ERROR.txt", e.Message);
}
}
}
}
}
}