-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
343 lines (297 loc) · 14 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
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
/*
Udger-update - Data updater for udger local and cloud parser
author The Udger.com Team ([email protected])
copyright Copyright (c) Udger s.r.o.
license GNU Lesser General Public License
link https://udger.com/products
*/
using System;
using System.Configuration;
using System.Reflection;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.IO.Compression;
namespace udger_updater
{
class Program
{
static void Main(string[] args)
{
string accessKey;
string global_destination_directory;
string destination_directory;
string localVersion;
string remoteVersion;
FileStream fileStream;
Console.WriteLine("udger-updater version " + Assembly.GetEntryAssembly().GetName().Version);
Console.WriteLine("Copyright (c) udger.com");
Console.WriteLine("");
#region init, validate
if (args.Length != 1)
{
Console.Error.WriteLine("Error: missing parametr");
Console.Error.WriteLine("");
Console.Error.WriteLine("usage: udger-updater.exe <config_file>");
Console.Error.WriteLine(" config_file ex: c:\\udger\\my.config");
Console.Error.WriteLine("");
Environment.Exit(1);
}
if (!File.Exists(args[0]))
{
Console.Error.WriteLine("Error: config_file not found");
Console.Error.WriteLine("");
Environment.Exit(1);
}
else
{
setFromConfigFile(args);
}
accessKey = ConfigurationManager.AppSettings["access_key"];
if (String.IsNullOrEmpty(accessKey))
{
Console.Error.WriteLine("Error: access_key not set");
Console.Error.WriteLine("");
Environment.Exit(1);
}
global_destination_directory = ConfigurationManager.AppSettings["global_destination_directory"];
if (String.IsNullOrEmpty(global_destination_directory))
{
Console.Error.WriteLine("Error: destination_directory not set");
Console.Error.WriteLine("");
Environment.Exit(5);
}
if (!Directory.Exists(global_destination_directory))
{
Console.Error.WriteLine("Error: global_destination_directory not found");
Console.Error.WriteLine("");
Environment.Exit(1);
}
// check local and remote version
if (File.Exists(global_destination_directory + "\\version"))
{
fileStream = new FileStream(global_destination_directory + "\\version", FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream))
{
localVersion = streamReader.ReadToEnd();
}
}
else
{
localVersion = "00000000-00";
}
downloadFile(accessKey, "version", global_destination_directory);
fileStream = new FileStream(global_destination_directory + "\\version", FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream))
{
remoteVersion = streamReader.ReadToEnd();
}
Console.WriteLine("Local ver: " + localVersion + " - Remote ver: " + remoteVersion);
if(localVersion == remoteVersion)
{
Console.WriteLine("Local files is up to date, exiting");
Environment.Exit(0);
}
#endregion
#region get data files (Local parser subscription)
if (Convert.ToBoolean(ConfigurationManager.AppSettings["udgerdb_v3.dat"]))
{
destination_directory = GetDestinationDirectory("udgerdb_v3.dat", global_destination_directory);
procesFile(accessKey, "udgerdb_v3.dat", destination_directory, "udgerdb_v3_dat.md5");
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["udgerdb_v3-noip.dat"]))
{
destination_directory = GetDestinationDirectory("udgerdb_v3-noip.dat", global_destination_directory);
procesFile(accessKey, "udgerdb_v3-noip.dat", destination_directory, "udgerdb_v3_dat-noip.md5");
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["udgerdb.dat"]))
{
destination_directory = GetDestinationDirectory("udgerdb.dat", global_destination_directory);
procesFile(accessKey, "udgerdb.dat", destination_directory, "udgerdb_dat.md5");
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["udgerdata_old.ini"]))
{
destination_directory = GetDestinationDirectory("udgerdata_old.ini", global_destination_directory);
procesFile(accessKey, "udgerdata_old.ini", destination_directory, "udgerdata_old_ini.md5");
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["udgerdata_old.xml"]))
{
destination_directory = GetDestinationDirectory("udgerdata_old.xml", global_destination_directory);
procesFile(accessKey, "udgerdata_old.xml", destination_directory, "udgerdata_old_xml.md5");
}
#endregion
#region get icons files (Local and Cloud parser subscription)
if (Convert.ToBoolean(ConfigurationManager.AppSettings["ico_ua.zip"]))
{
destination_directory = GetDestinationDirectory("ico_ua.zip", global_destination_directory);
procesFile(accessKey, "ico_ua.zip", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["ico_os.zip"]))
{
destination_directory = GetDestinationDirectory("ico_os.zip", global_destination_directory);
procesFile(accessKey, "ico_os.zip", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["ico_device.zip"]))
{
destination_directory = GetDestinationDirectory("ico_device.zip", global_destination_directory);
procesFile(accessKey, "ico_device.zip", destination_directory);
}
#endregion
#region get CSV files (Local and Cloud parser subscription)
if (Convert.ToBoolean(ConfigurationManager.AppSettings["botIP.csv"]))
{
destination_directory = GetDestinationDirectory("botIP.csv", global_destination_directory);
procesFile(accessKey, "botIP.csv", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["uas_example.csv"]))
{
destination_directory = GetDestinationDirectory("uas_example.csv", global_destination_directory);
procesFile(accessKey, "uas_example.csv", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["uasOS_example.csv"]))
{
destination_directory = GetDestinationDirectory("uasOS_example.csv", global_destination_directory);
procesFile(accessKey, "uasOS_example.csv", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["uaDEVICE_example.csv"]))
{
destination_directory = GetDestinationDirectory("uaDEVICE_example.csv", global_destination_directory);
procesFile(accessKey, "uaDEVICE_example.csv", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["mac_address_vendors.csv"]))
{
destination_directory = GetDestinationDirectory("mac_address_vendors.csv", global_destination_directory);
procesFile(accessKey, "mac_address_vendors.csv", destination_directory);
}
#endregion
#region get CSV files (Local parser subscription)
if (Convert.ToBoolean(ConfigurationManager.AppSettings["anonymizing_vpn_service.csv"]))
{
destination_directory = GetDestinationDirectory("anonymizing_vpn_service.csv", global_destination_directory);
procesFile(accessKey, "anonymizing_vpn_service.csv", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["fake_crawler.csv"]))
{
destination_directory = GetDestinationDirectory("fake_crawler.csv", global_destination_directory);
procesFile(accessKey, "fake_crawler.csv", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["known_attack_source.csv"]))
{
destination_directory = GetDestinationDirectory("known_attack_source.csv", global_destination_directory);
procesFile(accessKey, "known_attack_source.csv", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["public_cgi_proxy.csv"]))
{
destination_directory = GetDestinationDirectory("public_cgi_proxy.csv", global_destination_directory);
procesFile(accessKey, "public_cgi_proxy.csv", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["public_web_proxy.csv"]))
{
destination_directory = GetDestinationDirectory("public_web_proxy.csv", global_destination_directory);
procesFile(accessKey, "public_web_proxy.csv", destination_directory);
}
if (Convert.ToBoolean(ConfigurationManager.AppSettings["tor_exit_node.csv"]))
{
destination_directory = GetDestinationDirectory("tor_exit_node.csv", global_destination_directory);
procesFile(accessKey, "tor_exit_node.csv", destination_directory);
}
#endregion
}
#region proces file
static void procesFile(string access_key, string filename, string destination_dir, string md5filename="")
{
string tmpMD5;
string tempPath = System.IO.Path.GetTempPath();
Boolean md5 = true;
downloadFile(access_key, filename, tempPath);
if (!String.IsNullOrEmpty(md5filename))
{
downloadFile(access_key, md5filename, tempPath);
FileStream fileStream = new FileStream(tempPath + md5filename, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream))
{
tmpMD5 = streamReader.ReadToEnd();
}
File.Delete(tempPath + md5filename);
if (tmpMD5 != GetMD5HashFromFile(tempPath + filename))
{
md5 = false;
File.Delete(tempPath + filename);
Console.Error.WriteLine("Warning: md5 hash not ok, file not save to destination dir");
Console.Error.WriteLine("");
}
}
if(md5)
{
if (File.Exists(destination_dir + "\\" + filename))
{
File.Delete(destination_dir + "\\" + filename);
}
File.Move(tempPath + filename, destination_dir + "\\" + filename);
}
}
#endregion
#region download file
static void downloadFile(string access_key, string filename, string dir)
{
string baseUrl = @"http://data.udger.com/";
if (File.Exists(dir + "\\" + filename))
{
File.Delete(dir + "\\" + filename);
}
Console.Write("Downloading file: {0} ...", filename);
using (WebClient myWebClient = new WebClient())
{
try
{
myWebClient.DownloadFile(new Uri(baseUrl + access_key + "/" + filename), dir + "\\" + filename);
myWebClient.Dispose();
}
catch (WebException e)
{
Console.Error.WriteLine("\nError: " + e.Message);
Environment.Exit(1);
}
}
Console.Write("... Done\n");
}
#endregion
#region set Config file
static void setFromConfigFile(string[] args)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File = args[0];
config.Save(ConfigurationSaveMode.Modified);
}
#endregion
#region MD5
static string GetMD5HashFromFile(string ff)
{
using (var md5 = MD5.Create())
{
using (var stream = File.Open(ff, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty).ToLower();
}
}
}
#endregion
#region destination directory
static string GetDestinationDirectory(string key, string global_dir)
{
string destination_directory = ConfigurationManager.AppSettings[key + "_destination_directory"];
if (String.IsNullOrEmpty(destination_directory))
{
destination_directory = global_dir;
}
if (!Directory.Exists(destination_directory))
{
Console.Error.WriteLine("Error: " + key + "_destination_directory not found");
Console.Error.WriteLine("");
Environment.Exit(1);
}
return destination_directory;
}
#endregion
}
}