source: sample/hadoop-0.16/tw/org/nchc/code/SnortParser.java @ 45

Last change on this file since 45 was 45, checked in by waue, 16 years ago

SnortBase? is needed to debug.

File size: 2.6 KB
Line 
1/**
2 * Program: LogParser.java
3 * Editor: Waue Chen
4 * From :  NCHC. Taiwn
5 * Last Update Date: 07/23/2008
6 */
7
8package tw.org.nchc.code;
9
10import java.io.BufferedReader;
11import java.io.BufferedWriter;
12import java.io.File;
13import java.io.FileReader;
14import java.io.FileWriter;
15import java.io.IOException;
16import java.text.ParseException;
17import java.util.regex.Matcher;
18import java.util.regex.Pattern;
19
20public 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("^\\[\\**\\] \\[([0-9]*):([0-9]*):([0-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      line = fi.readLine();
83      if (line == null) {
84        break;
85      }else if(line.isEmpty()){
86        fw.write(this.logData.toString() + "\n");
87        this.logData = "";
88        count = 0;       
89      }else if (count < 4) {
90        // System.out.println(line);
91        snortParser(line, count + 1);
92        count++;
93      } else {
94        count++;
95      }
96    } while (true);
97    fw.flush();
98    fw.close();
99
100  }
101
102  public static void main(String[] args) throws ParseException, Exception {
103    String in = new String("/home/waue/Desktop/alert_m");
104    String ou = new String("/tmp/alert_SnortBase");
105    SnortParser a = new SnortParser(in, ou);
106    a.parseToLine();
107  }
108}
Note: See TracBrowser for help on using the repository browser.