1 | /** |
---|
2 | * Program: LogParser.java |
---|
3 | * Editor: Waue Chen |
---|
4 | * From : NCHC. Taiwn |
---|
5 | * Last Update Date: 07/17/2008 |
---|
6 | */ |
---|
7 | |
---|
8 | package tw.org.nchc.code; |
---|
9 | |
---|
10 | import java.io.BufferedReader; |
---|
11 | import java.io.BufferedWriter; |
---|
12 | import java.io.File; |
---|
13 | import java.io.FileReader; |
---|
14 | import java.io.FileWriter; |
---|
15 | import java.io.IOException; |
---|
16 | import java.text.ParseException; |
---|
17 | import java.util.regex.Matcher; |
---|
18 | import java.util.regex.Pattern; |
---|
19 | |
---|
20 | public class SnortParser { |
---|
21 | private String logData = new String(); |
---|
22 | |
---|
23 | private String in; |
---|
24 | |
---|
25 | private String ou; |
---|
26 | |
---|
27 | public SnortParser(String in, String ou) { |
---|
28 | this.in = in; |
---|
29 | this.ou = ou; |
---|
30 | } |
---|
31 | |
---|
32 | public SnortParser() { |
---|
33 | this.in = "/var/log/snort/alert"; |
---|
34 | this.ou = "~/parseSnort.log"; |
---|
35 | } |
---|
36 | |
---|
37 | public void snortParser(String line, int i) throws ParseException, |
---|
38 | Exception { |
---|
39 | String[] data; |
---|
40 | Pattern patten_line; |
---|
41 | Matcher matcher; |
---|
42 | switch (i) { |
---|
43 | case 1: |
---|
44 | patten_line = Pattern |
---|
45 | .compile("^\\[\\**\\] \\[([1-9]*):([1-9]*):([1-9]*)\\] ([^\\[]*)\\[\\**\\]$"); |
---|
46 | break; |
---|
47 | case 2: |
---|
48 | patten_line = Pattern |
---|
49 | .compile("^\\[Classification: ([^\\]]*)\\] \\[Priority: ([1-9]*)\\].*$"); |
---|
50 | break; |
---|
51 | case 3: |
---|
52 | patten_line = Pattern |
---|
53 | .compile("(^[0-9]*)\\/([0-9]*)\\-([0-9]*)\\:([0-9]*)\\:([0-9]*)\\.[0-9]* ([^ ]*) -> ([^$]*)$"); |
---|
54 | break; |
---|
55 | case 4: |
---|
56 | patten_line = Pattern |
---|
57 | .compile("^([^ ]*) TTL:([^ ]*) TOS:([^ ]*) ID:([^ ]*) IpLen:([^ ]*) DgmLen:([^ ]*)$"); |
---|
58 | break; |
---|
59 | default: |
---|
60 | patten_line = null; |
---|
61 | break; |
---|
62 | } |
---|
63 | matcher = patten_line.matcher(line); |
---|
64 | if (matcher.matches()) { |
---|
65 | int number = matcher.groupCount(); |
---|
66 | data = new String[number]; |
---|
67 | for (int j = 0; j < number; j++) { |
---|
68 | data[j] = matcher.group(j + 1); |
---|
69 | this.logData += (data[j] + ";"); |
---|
70 | } |
---|
71 | |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | void parseToLine() throws IOException, ParseException, Exception { |
---|
77 | BufferedReader fi = new BufferedReader(new FileReader(new File(in))); |
---|
78 | BufferedWriter fw = new BufferedWriter(new FileWriter(new File(ou))); |
---|
79 | String line = null; |
---|
80 | int count = 0; |
---|
81 | do { |
---|
82 | String tmp = fi.readLine(); |
---|
83 | if (tmp == null) { |
---|
84 | break; |
---|
85 | } else if (count < 4) { |
---|
86 | line = tmp; |
---|
87 | // System.out.println(line); |
---|
88 | snortParser(line, count + 1); |
---|
89 | count++; |
---|
90 | } else if (count == 4) { |
---|
91 | count++; |
---|
92 | } else if (count == 5) { |
---|
93 | fw.write(this.logData.toString() + "\n"); |
---|
94 | this.logData = ""; |
---|
95 | count = 0; |
---|
96 | } else { |
---|
97 | System.err.print(" Error ! "); |
---|
98 | return; |
---|
99 | } |
---|
100 | } while (true); |
---|
101 | fw.flush(); |
---|
102 | fw.close(); |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | public static void main(String[] args) throws ParseException, Exception { |
---|
107 | String in = new String("/home/waue/Desktop/alert"); |
---|
108 | String ou = new String("/home/waue/Desktop/bb"); |
---|
109 | SnortParser a = new SnortParser(in, ou); |
---|
110 | a.parseToLine(); |
---|
111 | } |
---|
112 | } |
---|