-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacketEvaluator.java
More file actions
263 lines (252 loc) · 7.18 KB
/
packetEvaluator.java
File metadata and controls
263 lines (252 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import java.io.*;
public class packetEvaluator{
public static boolean PacketEvaluator(byte[] packet, String rule) throws IOException{
String packetStatus = null;
int type = EtherParser.getType(packet);
String prot = ipParser.getprot(packet);
String sourceIP;
String destIP;
int sourcePort = 0;
int destPort = 0;
byte[] ipPacket = null;
byte[] arpPacket = null;
if (type == 2048){
ipPacket = ipParser.IPparser(packet);
sourceIP = ipParser.getipSaddress(ipPacket);
destIP = ipParser.getipDaddress(ipPacket);
if (prot.equals("This is a TCP packet")){
sourcePort = tcpParser.gettcpSport(ipPacket);
}
if (prot.equals("This is a UDP packet")){
sourcePort = udpParser.getudpSport(ipPacket);
}
packetStatus = checkPacketAgainstRules(sourceIP, sourcePort, destIP, destPort, rule);
if (packetStatus.equals("Good to go!")){
return true;
}else{
logPacket(packetStatus, ipPacket, "log.txt");
return checkOption(sourceIP, destIP, packetStatus, ipPacket);
}
}
if (type == 2054){
arpPacket = arpParser.ARPparser(packet);
sourceIP = arpParser.getsIPAddr(arpPacket);
destIP = arpParser.gettargetIPAddr(arpPacket);
packetStatus = checkPacketAgainstRules(sourceIP, destIP, arpPacket, rule);
if (packetStatus.equals("Good to go!")){
return true;
}else{
logPacket(packetStatus, arpPacket, "log.txt");
return checkOption(sourceIP, destIP, packetStatus, arpPacket);
}
}else{
return false;
}
}
public static void logPacket(String packetStatus, byte[] packet, String logname) throws IOException{
StringBuilder log = new StringBuilder();
BufferedWriter bw = null;
try{
bw = new BufferedWriter(new FileWriter(logname, true));
log.append(packetStatus);
log.append("\n");
log.append(Lib.getString(packet));
log.append("\n");
bw.write(log.toString());
}catch(Exception e){
e.printStackTrace();
}finally{
if(bw!=null)
bw.close();
if(bw!=null)
bw.close();
}
}
public static String checkPacketAgainstRules(String sourceIP, int sourcePort, String destIP, int destPort, String ruleDoc){
String[] ruleSet = rulesParser.readRules(ruleDoc);
String action, rulesIP, ruledIP;
String rulesPort, ruledPort;
for (int i = 0; i < ruleSet.length;i++){
String rule = ruleSet[i];
action = rulesParser.getAction(rule);
rulesIP = rulesParser.getSourceIP(rule)+".";
ruledIP = rulesParser.getDestIP(rule)+".";
rulesPort = rulesParser.getSourcePort(rule);
ruledPort = rulesParser.getDestPort(rule);
if(sourceIP.equals(rulesIP) || rulesIP.equals("any")){
if(Integer.toString(sourcePort).equals(rulesPort) || rulesPort.equals("any")){
if(destIP.equals(ruledIP) || rulesIP.equals("any")){
if(Integer.toString(destPort).equals(ruledPort) || ruledPort.equals("any")){
if(action.equalsIgnoreCase("Alert")){
return ruleSet[i];
}else{
return "Good to go!";
}
}else{
continue;
}
}else{
continue;
}
}else{
continue;
}
}else{
continue;
}
}
return "Good to go!"; //MAY NEED TO CHECK LOGIC HERE
}
public static String checkPacketAgainstRules(String sourceIP, String destIP, byte[] arpPacket, String ruleDoc){
String[] ruleSet = rulesParser.readRules(ruleDoc);
String action, rulesIP, ruledIP;
for (int i = 0; ruleSet.length >= 1;i++){
String rule = ruleSet[i];
action = rulesParser.getAction(rule);
rulesIP = rulesParser.getSourceIP(rule)+".";
ruledIP = rulesParser.getDestIP(rule)+".";
if(sourceIP.equals(rulesIP) || rulesIP.equals("any")){
if(destIP.equals(ruledIP) || rulesIP.equals("any")){
if(action.equalsIgnoreCase("Alert")){
return ruleSet[i];
}else{
return "Good to go!";
}
}else{
continue;
}
}else{
continue;
}
}
return "Good to go!"; //MAY NEED TO CHECK LOGIC HERE
}
public static boolean checkOption(String srcIP, String destIP, String rule, byte[] packet) throws IOException{
String option = rulesParser.getOption(rule);
String[] arr = option.split("; ");
byte[] tcpPacket = tcpParser.TCPparser(packet);
byte[] icmpPacket = icmpParser.ICMPparser(packet);
for (int i = 0; i < arr.length; i++){
String[] keyArr = arr[i].split(":");
String keyword = keyArr[0].replaceAll("[()]", "");
String desc = keyArr[1].replaceAll("[()]", "");
desc.replaceAll("[\"]", "");
if (keyword.equals("msg")){
System.out.println(desc);
continue;
}
if (keyword.equals("logto")){
logPacket(rule, packet, desc);
continue;
}
if (keyword.equals("ttl")){
if (ipParser.gettimeToLive(packet) == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("tos")){
if (ipParser.gettos(packet) == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("id")){
if (ipParser.getID(packet) == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("fragoffset")){
if (ipParser.getfragOffset(packet) == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("ipoption")){
String ipOp = Lib.getString(ipParser.getoptions(packet));
String[] descArr = desc.split("|");
for(int j = 0; j < descArr.length; j++){
if(ipOp.equals(descArr[i])){
return false;
}else{
continue;
}
}
continue;
}
if (keyword.equals("fragbits")){
if (ipParser.getFlagsType(packet).equalsIgnoreCase(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("dsize")){
if (ipParser.getIpPayload(packet).length == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("flags")){//tcp flags?
if (ipParser.getflags(packet) == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("seq")){
if (tcpParser.getseqNum(tcpPacket) == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("ack")){
if (tcpParser.getackNum(tcpPacket) == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("itype")){
if (icmpParser.geticmpType(icmpPacket) == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("icode")){
if (icmpParser.getcode(icmpPacket) == Integer.parseInt(desc)){
return false;
}else{
continue;
}
}
if (keyword.equals("content")){
String ipPL = Lib.getString(ipParser.getIpPayload(packet));
String[] descArr = desc.split("|");
for(int j = 0; j < descArr.length; j++){
if(ipPL.equals(descArr[i])){
return false;
}else{
continue;
}
}
}
if (keyword.equals("sameip")){
if (srcIP.equals(destIP)){
return false;
}else{
continue;
}
}
}
return true;
}
}