-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.java
363 lines (304 loc) · 10.9 KB
/
Utils.java
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Utils {
public static byte[] readFile(String file) throws IOException {
Path path = Paths.get(file);
byte[] fileContents = Files.readAllBytes(path);
// StringBuilder result = new StringBuilder();
// for (byte aByte: fileContents) {
// result.append(String.format("%02x", aByte));
// }
return fileContents;
}
/** ETHER */
public static String setDestinationMAC(byte[] packet) {
StringBuilder result = new StringBuilder();
for (int i=0; i<6; i++) {
result.append(String.format("%02x", packet[i]));
result.append(':');
}
return result.substring(0, result.length()-1);
}
public static String setSourceMAC(byte[] packet) {
StringBuilder result = new StringBuilder();
for (int i=6; i<12; i++) {
result.append(String.format("%02x", packet[i]));
result.append(':');
}
return result.substring(0, result.length()-1);
}
public static String setEtherType(byte[] packet) {
StringBuilder result = new StringBuilder();
for (int i=12; i<14; i++) {
result.append(String.format("%02x", packet[i]));
}
return result.toString();
}
/** IP */
public static int setIPVersion(byte[] packet) {
int ipv = packet[14] >> 4;
return ipv;
}
public static int setHeaderLen(byte[] packet) {
int mask = 0b1111;
int headerw = packet[14] & mask;
return headerw * 4;
}
public static int setTotalLength(byte[] packet) {
// int mask = 0b0000000011111111;
// int res = packet[17] & mask;
// res = packet[16] | res;
// return res;
byte b1 = packet[16];
byte b2 = packet[17];
return ((b1 << 8) | (b2 & 0xFF)) & 0xffff;
}
public static int setIpId(byte[] packet) {
byte b1 = packet[18];
byte b2 = packet[19];
return ((b1 << 8) | (b2 & 0xFF)) & 0xffff;
}
public static int setDSCP(byte[] packet) {
byte b1 = packet[15];
b1 >>= 2;
return b1;
}
public static int setECN(byte[] packet) {
byte b1 = packet[15];
int mask = 0b00000011;
return b1 & mask;
}
public static int setDFflag(byte[] packet) {
return packet[20] >> 6;
}
public static int setMFflag(byte[] packet) {
int mask = 0b00100000;
return packet[20] & mask;
}
public static int setFragOffset(byte[] packet) {
byte b1 = packet[20];
// byte test = 0b01000101;
// byte test2 = 0b01000101;
// b1 <<= 3;
byte b2 = packet[21];
int mask = 0b00011111;
int tmp = (b1 & mask);
return ((tmp << 8 )| (b2 & 0xFF)) & 0xffff;
}
public static int setTTL(byte[] packet) {
return packet[22] & 0xFF;
}
public static int setProtocol(byte[] packet) {
return packet[23] & 0xFF;
}
public static String chooseProtocol(int protocol) {
if (protocol == 6) return protocol + " (TCP)";
else if (protocol == 1) return protocol + " (ICMP)";
else if (protocol == 17) return protocol + " (UDP)";
else return "Unsupported protocol...";
}
public static String setHeaderChecksum(byte[] packet) {
byte b1 = packet[24];
byte b2 = packet[25];
StringBuilder result = new StringBuilder();
result.append(String.format("%02x", ((b1 << 8) | (b2 & 0xFF)) & 0xffff));
return "0x" + result;
}
public static String setSourceIP(byte[] packet) {
String result = "";
for (int i=26; i<30; i++) {
int tmp = packet[i] & 0xFF;
result = result + tmp + ".";
}
return result.substring(0, result.length()-1);
}
public static String setDestinationIP(byte[] packet) {
String result = "";
for (int i=30; i<34; i++) {
int tmp = packet[i] & 0xFF;
result = result + tmp + ".";
}
return result.substring(0, result.length()-1);
}
public static int setOptionsOffset(int headerLength) {
if (headerLength > 20) {
int optionsLen = headerLength - 20;
return 34 + optionsLen;
}
// if IP packet has no options start parsing data from 34th bit
else return 34;
}
public static String setIPv4Options(byte[] packet, int headerLength) {
if (headerLength > 20) {
// StringBuilder result = new StringBuilder();
// result.append("Options: ");
// int optionsLen = headerLength - 20;
// for (int i = 34; i < 34+optionsLen; i++) {
// result.append(String.format("%02x", packet[i]));
// }
// return result.toString();
return "Options present";
}
else {
return "No options";
}
}
/* ICMP Header Methods */
public static int setICMPType(byte[] packet, int offset) {
return packet[offset] & 0xFF;
}
public static int setICMPCode(byte[] packet, int offset) {
return packet[offset+1] & 0xFF;
}
public static String setICMPCheckSum(byte[] packet, int offset) {
byte b1 = packet[offset + 2];
byte b2 = packet[offset + 3];
StringBuilder result = new StringBuilder();
result.append(String.format("%02x", ((b1 << 8) | (b2 & 0xFF)) & 0xffff));
return "0x" + result;
}
/* TCP Header Methods */
public static int setTCPSourcePort(byte[] packet, int offset) {
byte b1 = packet[offset];
byte b2 = packet[offset+1];
return ((b1 << 8) | (b2 & 0xFF)) & 0xffff;
}
public static int setTCPDestPort(byte[] packet, int offset) {
byte b1 = packet[offset+2];
byte b2 = packet[offset+3];
return ((b1 << 8) | (b2 & 0xFF)) & 0xffff;
}
public static int setSequenceNum(byte[] packet, int offset) {
byte b1 = packet[offset+4];
byte b2 = packet[offset+5];
byte b3 = packet[offset+6];
byte b4 = packet[offset+7];
return ((b1&0xFF) << 24) | ((b2 &0xFF)<< 16) | ((b3&0xFF) << 8) | ((b4 & 0xFF));
}
public static int setAckNum(byte[] packet, int offset) {
byte b1 = packet[offset+8];
byte b2 = packet[offset+9];
byte b3 = packet[offset+10];
byte b4 = packet[offset+11];
return ((b1&0xFF) << 24) | ((b2 &0xFF)<< 16) | ((b3&0xFF) << 8) | ((b4 & 0xFF));
}
public static int setTCPDataOffset(byte[] packet, int offset) {
int tmp = ((packet[offset+12] & 0xFF) >> 4) & 0xFF;
return tmp * 4;
}
public static int setTCPUrg(byte[] packet, int offset) {
int mask = 0b00100000;
byte b1 = packet[offset+13];
return (b1&0xFF & mask) >> 5;
}
public static int setTCPAck(byte[] packet, int offset) {
int mask = 0b00010000;
byte b1 = packet[offset+13];
return (b1&0xFF & mask) >> 4;
}
public static int setTCPPush(byte[] packet, int offset) {
int mask = 0b00001000;
byte b1 = packet[offset+13];
return (b1&0xFF & mask) >> 3;
}
public static int setTCPReset(byte[] packet, int offset) {
int mask = 0b00000100;
byte b1 = packet[offset+13];
return (b1&0xFF & mask) >> 2;
}
public static int setTCPSyn(byte[] packet, int offset) {
int mask = 0b00000010;
byte b1 = packet[offset+13];
return (b1&0xFF & mask) >> 1;
}
public static int setTCPFin(byte[] packet, int offset) {
int mask = 0b00000001;
byte b1 = packet[offset+13];
return b1&0xFF & mask;
}
public static int setWindowSize(byte[] packet, int offset) {
byte b1 = packet[offset+14];
byte b2 = packet[offset+15];
return ((b1 << 8) | (b2 & 0xFF)) & 0xffff;
}
public static String setTCPChecksum(byte[] packet, int offset) {
byte b1 = packet[offset + 16];
byte b2 = packet[offset + 17];
StringBuilder result = new StringBuilder();
result.append(String.format("%02x", ((b1 << 8) | (b2 & 0xFF)) & 0xffff));
return "0x" + result;
}
public static int setTCPUrgPointer(byte[] packet, int offset) {
byte b1 = packet[offset+18];
byte b2 = packet[offset+19];
return ((b1 << 8) | (b2 & 0xFF)) & 0xffff;
}
public static String setTCPOptions(int dataOffset) {
if (dataOffset > 20) {
return "Options present";
}
else {
return "No options";
}
}
public static String setTCPData(byte[] packet, int dataOffset, int offset, int IPHeaderLen) {
int start = offset + dataOffset;
int end = packet.length - 14 - IPHeaderLen - dataOffset;
StringBuilder result = new StringBuilder();
// data more than 64 bits;
if ((end - start) > 64){
for (int i = start; i<start+64; i++) {
result.append(String.format("%02x", packet[i]));
}
}
else {
for (int i = start; i<packet.length; i++) {
result.append(String.format("%02x", packet[i]));
}
}
return result + "";
}
/* UDP Methods */
public static int setUDPSourcePort(byte[] packet, int offset) {
byte b1 = packet[offset];
byte b2 = packet[offset+1];
return ((b1 << 8) | (b2 & 0xFF)) & 0xffff;
}
public static int setUDPDestinationPort(byte[] packet, int offset) {
byte b1 = packet[offset+2];
byte b2 = packet[offset+3];
return ((b1 << 8) | (b2 & 0xFF)) & 0xffff;
}
public static int setUDPLength(byte[] packet, int offset) {
byte b1 = packet[offset+4];
byte b2 = packet[offset+5];
return ((b1 << 8) | (b2 & 0xFF)) & 0xffff;
}
public static String setUDPChecksum(byte[] packet, int offset) {
byte b1 = packet[offset+6];
byte b2 = packet[offset+7];
StringBuilder result = new StringBuilder();
result.append(String.format("%02x", ((b1 << 8) | (b2 & 0xFF)) & 0xffff));
return "0x" + result;
}
public static String setUDPData(byte[] packet, int dataOffset, int offset, int IPHeaderLen) {
int start = offset + dataOffset;
int end = packet.length - 14 - IPHeaderLen - dataOffset;
StringBuilder result = new StringBuilder();
// data more than 64 bits;
if ((end - start) > 64){
for (int i = start; i<start+64; i++) {
result.append(String.format("%02x", packet[i]));
}
}
else {
for (int i = start; i<packet.length; i++) {
result.append(String.format("%02x", packet[i]));
}
}
return result + "";
}
}