[32] | 1 | /** |
---|
| 2 | * Program: LogParser.java |
---|
| 3 | * Editor: Waue Chen |
---|
| 4 | * From : NCHC. Taiwn |
---|
[43] | 5 | * Last Update Date: 07/23/2008 |
---|
[32] | 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 { |
---|
[33] | 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; |
---|
[32] | 30 | } |
---|
| 31 | |
---|
[33] | 32 | public SnortParser() { |
---|
| 33 | this.in = "/var/log/snort/alert"; |
---|
| 34 | this.ou = "~/parseSnort.log"; |
---|
[32] | 35 | } |
---|
| 36 | |
---|
| 37 | public void snortParser(String line, int i) throws ParseException, |
---|
| 38 | Exception { |
---|
[33] | 39 | String[] data; |
---|
[32] | 40 | Pattern patten_line; |
---|
[33] | 41 | Matcher matcher; |
---|
[32] | 42 | switch (i) { |
---|
| 43 | case 1: |
---|
| 44 | patten_line = Pattern |
---|
[43] | 45 | .compile("^\\[\\**\\] \\[([0-9]*):([0-9]*):([0-9]*)\\] ([^\\[]*)\\[\\**\\]$"); |
---|
[32] | 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 |
---|
[47] | 57 | .compile("^([^ ]*) [^$]*$"); |
---|
| 58 | // .compile("^([^ ]*) TTL:([^ ]*) TOS:([^ ]*) ID:([^ ]*) IpLen:([^ ]*) DgmLen:([^ ]*)$"); |
---|
| 59 | |
---|
[32] | 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++) { |
---|
[33] | 70 | data[j] = matcher.group(j + 1); |
---|
| 71 | this.logData += (data[j] + ";"); |
---|
[32] | 72 | } |
---|
[47] | 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*"; |
---|
[32] | 83 | } |
---|
[33] | 84 | |
---|
[32] | 85 | } |
---|
| 86 | |
---|
[33] | 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))); |
---|
[32] | 90 | String line = null; |
---|
| 91 | int count = 0; |
---|
| 92 | do { |
---|
[43] | 93 | line = fi.readLine(); |
---|
| 94 | if (line == null) { |
---|
[32] | 95 | break; |
---|
[47] | 96 | } else if (line.isEmpty()) { |
---|
[43] | 97 | fw.write(this.logData.toString() + "\n"); |
---|
| 98 | this.logData = ""; |
---|
[47] | 99 | count = 0; |
---|
| 100 | } else if (count < 4) { |
---|
[33] | 101 | // System.out.println(line); |
---|
| 102 | snortParser(line, count + 1); |
---|
| 103 | count++; |
---|
[43] | 104 | } else { |
---|
[33] | 105 | count++; |
---|
[32] | 106 | } |
---|
| 107 | } while (true); |
---|
[33] | 108 | fw.flush(); |
---|
| 109 | fw.close(); |
---|
[32] | 110 | |
---|
| 111 | } |
---|
[33] | 112 | |
---|
[47] | 113 | // 需搞定icmp ping 的格式問題 |
---|
[32] | 114 | public static void main(String[] args) throws ParseException, Exception { |
---|
[47] | 115 | String in = new String("/home/waue/Desktop/alert_flex.txt"); |
---|
| 116 | String ou = new String("/home/waue/Desktop/alert_flex_parsed.txt"); |
---|
[33] | 117 | SnortParser a = new SnortParser(in, ou); |
---|
[32] | 118 | a.parseToLine(); |
---|
| 119 | } |
---|
| 120 | } |
---|