Class: F2bread

Inherits:
Object
  • Object
show all
Defined in:
lib/f2bread.rb

Overview

class starts here

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log) ⇒ F2bread

Returns a new instance of F2bread.

Raises:

  • (ArgumentError)


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
# File 'lib/f2bread.rb', line 27

def initialize(log)
	# check if Fail2ban.log exists and then check if it's valid!
	raise ArgumentError, "No fail2ban.log found!!!" if File.exists?(log) == FALSE # <-- this is not needed right now!

	@data_table = []
	@log = log

	# grep lines that containt the keyword 'Ban' from log file
	lines = File.readlines(log).select {|line| line.match /Ban/}

	# Available internet country codes
	cdc = {
		"AD" => "Andorra",
		"AE" => "United_Arab_Emirates",
		"AF" => "Afghanistan",
		"AG" => "Antigua_and_Barbuda",
		"AI" => "Anguilla",
		"AL" => "Albania",
		"AM" => "Armenia",
		"AN" => "Netherlands_Antilles",
		"AO" => "Angola",
		"AQ" => "Antarctica",
		"AR" => "Argentina",
		"ARPA" => "Old_style_Arpanet",
		"AS" => "American Samoa",
		"AT" => "Austria",
		"AU" => "Australia",
		"AW" => "Aruba",
		"AZ" => "Azerbaijan",
		"BA" => "Bosnia_and_Herzegovina",
		"BB" => "Barbados",
		"BD" => "Bangladesh",
		"BE" => "Belgium",
		"BF" => "Burkina_Faso",
		"BG" => "Bulgaria",
		"BH" => "Bahrain",
		"BI" => "Burundi",
		"BJ" => "Benin",
		"BM" => "Bermuda",
		"BN" => "Brunei_Darussalam",
		"BO" => "Bolivia",
		"BR" => "Brazil",
		"BS" => "Bahamas",
		"BT" => "Bhutan",
		"BV" => "Bouvet_Island",
		"BW" => "Botswana",
		"BY" => "Belarus",
		"BZ" => "Belize",
		"CA" => "Canada",
		"CC" => "Cocos_Islands",
		"CF" => "Central_African_Republic",
		"CG" => "Congo",
		"CH" => "Switzerland",
		"CI" => "Ivory_Coast",
		"CK" => "Cook_Islands",
		"CL" => "Chile",
		"CM" => "Cameroon",
		"CN" => "China",
		"CO" => "Colombia",
		"COM" => "US_Commercial(USA)",
		"CR" => "Costa Rica",
		"CS" => "Czechoslovakia(former)",
		"CU" => "Cuba",
		"CV" => "Cape Verde",
		"CX" => "Christmas_Island",
		"CY" => "Cyprus",
		"CZ" => "Czech_Republic",
		"DE" => "Germany",
		"DJ" => "Djibouti",
		"DK" => "Denmark",
		"DM" => "Dominica",
		"DO" => "Dominican_Republic",
		"DZ" => "Algeria",
		"EC" => "Ecuador",
		"EDU" => "US_Educational(USA)",
		"EE" => "Estonia",
		"EG" => "Egypt",
		"EH" => "Western_Sahara",
		"ER" => "Eritrea",
		"ES" => "Spain",
		"ET" => "Ethiopia",
		"FI" => "Finland",
		"FJ" => "Fiji",
		"FK" => "Falkland_Islands",
		"FM" => "Micronesia",
		"FO" => "Faroe_Islands",
		"FR" => "France_(Francie)",
		"FX" => "France_Metropolitan",
		"GA" => "Gabon",
		"GB" => "Great_Britain(UK)",
		"GD" => "Grenada",
		"GE" => "Georgia",
		"GF" => "French_Guiana",
		"GH" => "Ghana",
		"GI" => "Gibraltar",
		"GL" => "Greenland(Island)",
		"GM" => "Gambia",
		"GN" => "Guinea",
		"GOV" => "US_Government(USA)",
		"GP" => "Guadeloupe",
		"GQ" => "Equatorial_Guinea",
		"GR" => "Greece",
		"GS" => "S.Georgia_and_S.Sandwich_Isls.",
		"GT" => "Guatemala",
		"GU" => "Guam",
		"GW" => "Guinea-Bissau",
		"GY" => "Guyana",
		"HK" => "Hong Kong",
		"HM" => "Heard_and_McDonald_Islands",
		"HN" => "Honduras",
		"HR" => "Croatia",
		"HT" => "Haiti",
		"HU" => "Hungary",
		"ID" => "Indonesia",
		"IE" => "Ireland",
		"IL" => "Israel",
		"IN" => "India",
		"INT" => "International",
		"IO" => "British_Indian_Ocean_Territory",
		"IQ" => "Iraq",
		"IR" => "Iran",
		"IS" => "Iceland(Island)",
		"IT" => "Italy",
		"JM" => "Jamaica",
		"JO" => "Jordan",
		"JP" => "Japan",
		"KE" => "Kenya",
		"KG" => "Kyrgyzstan",
		"KH" => "Cambodia",
		"KI" => "Kiribati",
		"KM" => "Comoros",
		"KN" => "Saint_Kitts_and_Nevis",
		"KP" => "Korea(North)",
		"KR" => "Korea(South)",
		"KW" => "Kuwait",
		"KY" => "Cayman_Islands",
		"KZ" => "Kazakhstan",
		"LA" => "Laos",
		"LB" => "Lebanon",
		"LC" => "Saint_Lucia",
		"LI" => "Liechtenstein",
		"LK" => "Sri_Lanka",
		"LR" => "Liberia",
		"LS" => "Lesotho",
		"LT" => "Lithuania",
		"LU" => "Luxembourg",
		"LV" => "Latvia",
		"LY" => "Libya",
		"MA" => "Morocco",
		"MC" => "Monaco",
		"MD" => "Moldova",
		"MG" => "Madagascar",
		"MH" => "Marshall_Islands",
		"MIL" => "US_Military",
		"MK" => "Macedonia",
		"ML" => "Mali",
		"MM" => "Myanmar",
		"MN" => "Mongolia",
		"MO" => "Macau",
		"MP" => "Northern_Mariana_Islands",
		"MQ" => "Martinique",
		"MR" => "Mauritania",
		"MS" => "Montserrat",
		"MT" => "Malta",
		"MU" => "Mauritius",
		"MV" => "Maldives",
		"MW" => "Malawi",
		"MX" => "Mexico",
		"MY" => "Malaysia",
		"MZ" => "Mozambique",
		"NA" => "Namibia",
		"NATO" => "Nato_field",
		"NC" => "New_Caledonia",
		"NE" => "Niger",
		"NET" => "Network",
		"NF" => "Norfolk_Island",
		"NG" => "Nigeria",
		"NI" => "Nicaragua",
		"NL" => "Netherlands",
		"NO" => "Norway",
		"NP" => "Nepal",
		"NR" => "Nauru",
		"NT" => "Neutral_Zone",
		"NU" => "Niue",
		"NZ" => "New_Zealand",
		"OM" => "Oman",
		"ORG" => "US-Non-Profit_Organization(USA)",
		"PA" => "Panama",
		"PE" => "Peru",
		"PF" => "French_Polynesia",
		"PG" => "Papua_New_Guinea",
		"PH" => "Philippines",
		"PK" => "Pakistan",
		"PL" => "Poland",
		"PM" => "St.Pierre_and_Miquelon",
		"PN" => "Pitcairn",
		"PR" => "Puerto_Rico",
		"PT" => "Portugal",
		"PW" => "Palau",
		"PY" => "Paraguay",
		"QA" => "Qatar",
		"RE" => "Reunion",
		"RO" => "Romania",
		"RU" => "Russian_Federation",
		"RW" => "Rwanda",
		"SA" => "Saudi_Arabia",
		"SC" => "Seychelles",
		"SD" => "Sudan",
		"SE" => "Sweden",
		"SG" => "Singapore",
		"SH" => "St.Helena",
		"SI" => "Slovenia",
		"SJ" => "Svalbard_and_Jan_Mayen_Islands",
		"SK" => "SlovakRepublic",
		"SL" => "SierraLeone",
		"SM" => "SanMarino",
		"SN" => "Senegal",
		"SO" => "Somalia",
		"SR" => "Suriname",
		"ST" => "Sao_Tome_and_Principe",
		"SU" => "USSR(former)",
		"SV" => "ElSalvador",
		"SY" => "Syria",
		"SZ" => "Swaziland",
		"Sb" => "SolomonIslands",
		"TC" => "Turks_and_Caicos_Islands",
		"TD" => "Chad",
		"TF" => "French_Southern_Territories",
		"TG" => "Togo",
		"TH" => "Thailand",
		"TJ" => "Tajikistan",
		"TK" => "Tokelau",
		"TM" => "Turkmenistan",
		"TN" => "Tunisia",
		"TO" => "Tonga",
		"TP" => "East_Timor",
		"TR" => "Turkey",
		"TT" => "Trinidad_and_Tobago",
		"TV" => "Tuvalu",
		"TW" => "Taiwan",
		"TZ" => "Tanzania",
		"UA" => "Ukraine",
		"UG" => "Uganda",
		"UK" => "United_Kingdom",
		"UM" => "USMinor_Outlying_Islands",
		"US" => "United_States (USA)",
		"UY" => "Uruguay",
		"UZ" => "Uzbekistan",
		"VA" => "Vatican City State",
		"VC" => "Saint Vincent and the Grenadines",
		"VE" => "Venezuela",
		"VG" => "Virgin_Islands(British)",
		"VI" => "Virgin_Islands(U.S.)",
		"VN" => "Vietnam",
		"VU" => "Vanuatu",
		"WF" => "Wallis_and_FutunaIslands",
		"WS" => "Samoa",
		"YE" => "Yemen",
		"YT" => "Mayotte",
		"YU" => "Yugoslavia",
		"ZA" => "South_Africa",
		"ZM" => "Zambia",
		"ZR" => "Zaire",
		"ZW" => "Zimbabwe",
	}

	lines.each do |line|
		kwords = line.split(' ')
		time = kwords[1].split(',')
		ip = kwords[6] # the IP string
		c = GeoIP.new($GeoIP).country(ip) # <= resolv country by IP using the GeoIP database
		country = cdc[c.country_code2] # <= retrieve country from cdc hash table
		@data_table << "#{kwords[0]} #{time[0]} #{kwords[4]} #{ip} #{c.country_code2} #{country}"
	end
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



26
27
28
# File 'lib/f2bread.rb', line 26

def log
  @log
end

Instance Method Details

#infoObject

print all data considered ‘important’ here



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
# File 'lib/f2bread.rb', line 304

def info
	entries = @data_table.size
	f2blog = File.expand_path(@log)
	fdate = "#{@data_table[0].split(' ')[0]} #{@data_table[0].split(' ')[1]}" #Firt entry date
	ldate = "#{@data_table[entries-1].split(' ')[0]} #{@data_table[entries-1].split(' ')[1]} " #Last entry date
	tdiff = Time.diff(fdate, ldate) #time diference
	ucl = [] # unique country list
	upl = [] # unique protocol list
	@data_table.each { |line| c = line.split(' ')[5]; ucl << c if ucl.include?(c) == false} # create the ucl list
	@data_table.each { |line| c = line.split(' ')[2]; upl << c if upl.include?(c) == false} # create the ucl list
	upl.size > 1 ? upl_print = upl.size.join(', ') : upl_print = upl[0]
	ban_avg = entries.to_f/((tdiff[:year] * 365) + (tdiff[:month] * 30) + (tdiff[:week] * 7) + tdiff[:day]).to_f
	sbanner = "Log file: '#{f2blog}'"
	puts "=" * sbanner.length
	puts sbanner
	puts ""
	printf("First entry: %s\n", fdate)
	printf("Last  entry: %s\n", ldate)
	printf("Time frame:\t%s\n", tdiff[:diff])
	printf("Banned IPs:\t%i\n", entries)
	printf("Countries:\t%i\n", ucl.size)
	printf("Protocol(s):\t%s\n", upl_print)
	printf("Bans per day:\t%0.2f\n\n", ban_avg)
	puts "Most banned IP(s) by fail2ban: "
	puts "-------------------------"
	puts "IP address    -   Attacks"
	puts "-------------------------"
	top_ips
	puts ""
	puts "Most hostile Countries:"
	puts "--------------------------"
	sort_by_country(5)
end

#sort_by_country(a) ⇒ Object

sort banned hosts by country



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/f2bread.rb', line 352

def sort_by_country(a)
	count = Hash.new(0)
	elog = []
	@data_table.each do |x| 
		elog << "#{x.split(' ')[3]} #{x.split(' ')[5]}"
	end
	for entry in elog
		count[entry.split(' ')[1]] += 1
	end
	sorted = count.sort_by { |country, no| no }.reverse
	x = a.to_i
	if x.to_i > 0
		sorted.take(x).each {|line| puts "Country: #{line[0]} - IP(s): #{line[1]}"}
	else
		sorted.each {|line| puts "Country: #{line[0]} - IP(s): #{line[1]}"}
	end
end

#sort_by_date(a) ⇒ Object

sort by date



381
382
383
384
385
386
387
# File 'lib/f2bread.rb', line 381

def sort_by_date(a)
	if a.to_i > 0
		@data_table.take(a).each {|line| puts line}
	else
		@data_table.each {|line| puts line}
	end
end

#sort_by_ip(a) ⇒ Object

sort banned hosts by IP (if more than one!)



371
372
373
374
375
376
377
378
# File 'lib/f2bread.rb', line 371

def sort_by_ip(a)
	count = Hash.new(0)
	for entry in @data_table
		count[entry.split(' ')[3]] += 1
	end
	sorted = count.sort_by {|data, no| no }.reverse   
	a.to_i > 0 ? sorted.take(a).each{|line| puts "Received #{line[1]} attacks from #{line[0]}"} : sorted.each{|line| puts "Received #{line[1]} attacks from #{line[0]}"} 
end

#top_ipsObject



339
340
341
342
343
344
345
346
347
348
349
# File 'lib/f2bread.rb', line 339

def top_ips
	count = Hash.new(0)
	for entry in @data_table
		count[entry.split(' ')[3]] += 1
	end
	sorted = count.sort_by {|data, no| no }.reverse
	$top_list = []
	best_count = sorted[0][1]
	ready = sorted.select { |data,no| no == best_count }
	ready.each {|line| printf("%s\t\t%s\n", line[0], line[1])}
end