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

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

SnortBase?.java -> tuning some field
SnortParser?.java -> debug and fix

File size: 3.0 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("^([^ ]*) [^$]*$");
58      // .compile("^([^ ]*) TTL:([^ ]*) TOS:([^ ]*) ID:([^ ]*) IpLen:([^ ]*) DgmLen:([^ ]*)$");
59
60      break;
61    default:
62      patten_line = null;
63      break;
64    }
65    matcher = patten_line.matcher(line);
66    if (matcher.matches()) {
67      int number = matcher.groupCount();
68      data = new String[number];
69      for (int j = 0; j < number; j++) {
70        data[j] = matcher.group(j + 1);
71        this.logData += (data[j] + ";");
72      }
73    }else if(i ==1 ){
74      this.logData += "0;0;0;parse error;";
75    }else if(i == 2){
76      this.logData += "Port Scan;3;";
77    }else if(i == 3){
78      this.logData += "01;01;00;00;00;error;error;";
79    }else if(i == 4){
80      this.logData += "0;";
81    }else{
82      this.logData = "*FatalError*";
83    }
84
85  }
86
87  void parseToLine() throws IOException, ParseException, Exception {
88    BufferedReader fi = new BufferedReader(new FileReader(new File(in)));
89    BufferedWriter fw = new BufferedWriter(new FileWriter(new File(ou)));
90    String line = null;
91    int count = 0;
92    do {
93      line = fi.readLine();
94      if (line == null) {
95        break;
96      } else if (line.isEmpty()) {
97        fw.write(this.logData.toString() + "\n");
98        this.logData = "";
99        count = 0;
100      } else if (count < 4) {
101        // System.out.println(line);
102        snortParser(line, count + 1);
103        count++;
104      } else {
105        count++;
106      }
107    } while (true);
108    fw.flush();
109    fw.close();
110
111  }
112
113  // 需搞定icmp ping 的格式問題
114  public static void main(String[] args) throws ParseException, Exception {
115    String in = new String("/home/waue/Desktop/alert_flex.txt");
116    String ou = new String("/home/waue/Desktop/alert_flex_parsed.txt");
117    SnortParser a = new SnortParser(in, ou);
118    a.parseToLine();
119  }
120}
Note: See TracBrowser for help on using the repository browser.