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

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

SnortParser? is ok ,
It will parse snort log file become my specific form.

Next step will code SnortBase? to upload the parsed file into Hbase;

File size: 3.1 KB
Line 
1/**
2 * Program: LogParser.java
3 * Editor: Waue Chen
4 * From :  NCHC. Taiwn
5 * Last Update Date: 07/17/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.StringTokenizer;
18import java.util.regex.Matcher;
19import java.util.regex.Pattern;
20
21public class SnortParser {
22  private String logData = new String(); 
23  private BufferedReader fi ;
24  private BufferedWriter fw ;
25  public SnortParser(String in, String ou) throws IOException {
26    fi = new BufferedReader(new FileReader(new File(in)));
27    fw = new BufferedWriter(new FileWriter(new File(ou)));
28  }
29
30  public static boolean isIpAddress(String inputString) {
31    StringTokenizer tokenizer = new StringTokenizer(inputString, ".");
32    if (tokenizer.countTokens() != 4) {
33      return false;
34    }
35    try {
36      for (int i = 0; i < 4; i++) {
37        String t = tokenizer.nextToken();
38        int chunk = Integer.parseInt(t);
39        if ((chunk & 255) != chunk) {
40          return false;
41        }
42      }
43    } catch (NumberFormatException e) {
44      return false;
45    }
46    if (inputString.indexOf("..") >= 0) {
47      return false;
48    }
49    return true;
50  }
51
52  public void snortParser(String line, int i) throws ParseException,
53      Exception {
54    String[] data ;
55    Pattern patten_line;
56    Matcher matcher;   
57    switch (i) {
58    case 1:
59      patten_line = Pattern
60          .compile("^\\[\\**\\] \\[([1-9]*):([1-9]*):([1-9]*)\\] ([^\\[]*)\\[\\**\\]$");
61      break;
62    case 2:
63      patten_line = Pattern
64          .compile("^\\[Classification: ([^\\]]*)\\] \\[Priority: ([1-9]*)\\].*$");
65      break;
66    case 3:
67      patten_line = Pattern
68          .compile("(^[0-9]*)\\/([0-9]*)\\-([0-9]*)\\:([0-9]*)\\:([0-9]*)\\.[0-9]* ([^ ]*) -> ([^$]*)$");
69      break;
70    case 4:
71      patten_line = Pattern
72          .compile("^([^ ]*) TTL:([^ ]*) TOS:([^ ]*) ID:([^ ]*) IpLen:([^ ]*) DgmLen:([^ ]*)$");
73      break;
74    default:
75      patten_line = null;
76      break;
77    }
78    matcher = patten_line.matcher(line);
79    if (matcher.matches()) {
80      int number = matcher.groupCount();
81      data = new String[number];
82      for (int j = 0; j < number; j++) {
83        data[j] = matcher.group(j+1);
84        this.logData += (data[j]+";");
85      }
86     
87    }
88   
89  }
90  void parseToLine() throws IOException,ParseException,Exception {
91
92    String line = null;
93    int count = 0;
94
95    do {
96      String tmp = this.fi.readLine();
97      if (tmp == null) {
98        break;
99      }else if(count < 4){
100        line = tmp;
101//        System.out.println(line);
102        snortParser(line, count+1);
103        count ++;
104      }else if(count ==4 ){
105        count ++;
106      }else if (count == 5){
107        this.fw.write(this.logData.toString() + "\n");
108        this.logData = "" ;
109        count = 0;
110      }
111      else
112      {
113        System.err.print(" Error ! ");
114        return ;
115      }
116    } while (true);
117    this.fw.flush();
118    this.fw.close();
119
120  }
121  public static void main(String[] args) throws ParseException, Exception {
122    String in = new String("/home/waue/Desktop/alert");
123    String ou = new String("/home/waue/Desktop/bb");
124    SnortParser a = new SnortParser(in,ou);
125    a.parseToLine();
126  }
127}
Note: See TracBrowser for help on using the repository browser.