Content-Length: 631520 | pFad | http://github.com/tilfin/detect-http-attack/commit/9e5f8e6691fbde7381d2e0f12f9378acb9848771

71 Add LTSV tests. · tilfin/detect-http-attack@9e5f8e6 · GitHub
Skip to content

Commit 9e5f8e6

Browse files
committed
Add LTSV tests.
1 parent 5ba672b commit 9e5f8e6

File tree

4 files changed

+79
-17
lines changed

4 files changed

+79
-17
lines changed

detect_http_attack.rb

+26-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
require 'optparse'
1515

1616

17+
module DetectHttpAttack
18+
19+
1720
class LogParser
1821

1922
def parse(line)
@@ -195,22 +198,22 @@ def parse_value(value)
195198

196199
def print_head(count, row)
197200
if @head
198-
print_row(@head, STDOUT, count, row)
201+
print_row(@head, $stdout, count, row)
199202
end
200203
end
201204

202205
def print_body(row)
203-
print_row(@body, STDOUT, "", row)
206+
print_row(@body, $stdout, "", row)
204207
end
205208

206209
def print_foot(count, row)
207210
if @foot
208-
print_row(@foot, STDOUT, count, row)
211+
print_row(@foot, $stdout, count, row)
209212
end
210213
end
211214

212215
def print_serr(count, row)
213-
print_row(@serr, STDERR, count, row)
216+
print_row(@serr, $stderr, count, row)
214217
end
215218

216219
def print_row(templ, ops, count, row)
@@ -260,7 +263,7 @@ def initialize(template)
260263
@realtime_notify = false
261264

262265
@pre_access_map = Hash.new
263-
@ops = STDOUT
266+
@ops = $stdout
264267
end
265268

266269
def proc(row)
@@ -319,8 +322,7 @@ def print_access(host, al)
319322
end
320323

321324

322-
323-
def get_opts
325+
def self.get_opts(argv)
324326
# Settting from Arguments
325327

326328
opts = { :parser => 'combined', :max_interval => 3, :min_seq => 8, :notify => false }
@@ -344,13 +346,14 @@ def get_opts
344346
opts[:conf_file] = path
345347
end
346348

347-
opt.parse!(ARGV)
349+
opt.parse!(argv)
348350

349351
opts
350-
end
351-
352-
def main
353-
opts = get_opts
352+
end
353+
354+
355+
def self.main(argv)
356+
opts = get_opts(argv)
354357

355358
if opts[:parser] == "ltsv"
356359
parser = LtsvLogParser.new
@@ -387,7 +390,10 @@ def main
387390
# Parsing log line, detect attacks
388391
#
389392
begin
390-
while line = STDIN.gets
393+
while line = $stdin.gets
394+
File.open("/tmp/filelog", "w+") do |f|
395+
f.write("LINE: #{line}")
396+
end
391397
row = parser.parse(line)
392398
next unless row
393399

@@ -399,5 +405,11 @@ def main
399405
processor.finalize
400406
end
401407

402-
main
403408

409+
end
410+
411+
412+
case $0
413+
when __FILE__
414+
DetectHttpAttack.main ARGV
415+
end

spec/command_spec.rb

+49-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
describe 'command arguments' do
88

9-
let(:fixture_file) { File.join(File.dirname(__FILE__), "fixtures", "1.txt") }
9+
let(:fixture_file) { File.join(File.dirname(__FILE__), "fixtures", "command_spec.log") }
1010

1111
context 'when args specified -h' do
1212
it "show usage" do
@@ -73,7 +73,7 @@
7373

7474
context 'when args specified -f custom conf file' do
7575
let(:conf_file) { File.join(File.dirname(__FILE__), "fixtures", "command_spec.conf") }
76-
it '' do
76+
it 'should success without error' do
7777
output = <<EOS
7878
\e[36m\e[1m10.1.0.1\e[0m\t\e[35m1\e[0m\t\e[32mAgent/5.0\e[0m
7979
2013-12-18T06:25:01+09:00\t200\t/path2\t-
@@ -83,7 +83,53 @@
8383
DetectHttpAttack.main ["-f", conf_file, "-s", "1"]
8484
}.should eq(output)
8585
end
86-
end
86+
end
87+
88+
let(:fixture_ltsv_file) { File.join(File.dirname(__FILE__), "fixtures", "command_spec.ltsv") }
89+
90+
context 'when args specified -ltsv -s 2 -i 2' do
91+
it "detect attack minimum 2 times for each interval within 2 sec." do
92+
output = <<EOS
93+
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m2\e[0m\t\e[32mAgent/5.0\e[0m
94+
2013-12-18T06:25:00+09:00\t200\t/path1\t-
95+
2013-12-18T06:25:02+09:00\t200\t/path3\t-
96+
97+
EOS
98+
test(fixture_ltsv_file) {
99+
DetectHttpAttack.main ["-ltsv", "-s", "2", "-i", "2"]
100+
}.should eq(output)
101+
end
102+
end
103+
104+
context 'when args specified -s 2 -ltsv -i 4' do
105+
it "detect attack minimum 2 times for each interval within 4 sec." do
106+
output = <<EOS
107+
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m3\e[0m\t\e[32mAgent/5.0\e[0m
108+
2013-12-18T06:25:00+09:00\t200\t/path1\t-
109+
2013-12-18T06:25:02+09:00\t200\t/path3\t-
110+
2013-12-18T06:25:06+09:00\t200\t/path4\t-
111+
112+
EOS
113+
test(fixture_ltsv_file) {
114+
DetectHttpAttack.main ["-s", "2", "-ltsv", "-i", "4"]
115+
}.should eq(output)
116+
end
117+
end
118+
119+
context 'when args specified -s 3 -i 4 -ltsv' do
120+
it "detect attack minimum 3 times for each interval within 4 sec." do
121+
output = <<EOS
122+
\e[36m\e[1m10.0.0.1\e[0m\t\e[35m3\e[0m\t\e[32mAgent/5.0\e[0m
123+
2013-12-18T06:25:00+09:00\t200\t/path1\t-
124+
2013-12-18T06:25:02+09:00\t200\t/path3\t-
125+
2013-12-18T06:25:06+09:00\t200\t/path4\t-
126+
127+
EOS
128+
test(fixture_ltsv_file) {
129+
DetectHttpAttack.main ["-s", "3", "-i", "4", "-ltsv"]
130+
}.should eq(output)
131+
end
132+
end
87133
end
88134

89135
end
File renamed without changes.

spec/fixtures/command_spec.ltsv

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
host:10.0.0.1 ident:- user:- time:[18/Dec/2013:06:25:00 +0900] req:GET /path1 HTTP/1.1 status:200 size:10000 referer:- ua:Agent/5.0
2+
host:10.1.0.1 ident:- user:- time:[18/Dec/2013:06:25:01 +0900] req:GET /path2 HTTP/1.1 status:200 size:20924 referer:- ua:Agent/5.0
3+
host:10.0.0.1 ident:- user:- time:[18/Dec/2013:06:25:02 +0900] req:GET /path3 HTTP/1.1 status:200 size:10000 referer:- ua:Agent/5.0
4+
host:10.0.0.1 ident:- user:- time:[18/Dec/2013:06:25:06 +0900] req:GET /path4 HTTP/1.1 status:200 size:10000 referer:- ua:Agent/5.0

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/tilfin/detect-http-attack/commit/9e5f8e6691fbde7381d2e0f12f9378acb9848771

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy