-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathPacketCapture.java
More file actions
348 lines (309 loc) · 10.1 KB
/
Copy pathPacketCapture.java
File metadata and controls
348 lines (309 loc) · 10.1 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
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
package javaforce.net;
import java.io.*;
import java.net.*;
import java.util.*;
import javaforce.*;
import javaforce.api.*;
import javaforce.ffm.*;
/**
* Packet Capture API (pcap)
*
*/
public class PacketCapture {
public static boolean debug = true;
private static byte[] local_mac;
private static byte[] local_ip;
public static int TYPE_IP4 = 0x0800;
public static int TYPE_ARP = 0x0806;
public static int TYPE_IP6 = 0x86dd;
public PacketCapture() {
init();
}
private static boolean inited = false;
/** Load native libraries. */
private boolean init() {
if (inited) return true;
if (JF.isWindows()) {
String windir = System.getenv("windir").replaceAll("\\\\", "/");
{
//npcap
String dll1 = windir + "/system32/npcap/packet.dll";
String dll2 = windir + "/system32/npcap/wpcap.dll";
if (new File(dll1).exists() && new File(dll2).exists()) {
inited = PCapAPI.getInstance().pcapInit(dll1, dll2);
return inited;
}
}
{
//pcap
String dll1 = windir + "/system32/packet.dll";
String dll2 = windir + "/system32/wpcap.dll";
if (new File(dll1).exists() && new File(dll2).exists()) {
inited = PCapAPI.getInstance().pcapInit(dll1, dll2);
return inited;
}
}
return inited;
}
if (JF.isUnix()) {
Library so = new Library("pcap");
if (!Library.findLibraries(new File[] {new File("/usr/lib"), new File(Library.getArchLibFolder())}, new Library[] {so})) {
return false;
}
inited = PCapAPI.getInstance().pcapInit(null, so.getPath());
return inited;
}
return inited;
}
/** List local interfaces.
* Return is array of strings, each is comma delimited list.
* DeviceName,IP/MAC,IP/MAC,...
*/
public static String[] listLocalInterfaces() {
return PCapAPI.getInstance().pcapListLocalInterfaces();
}
/** Find interface that contains IP address. */
public static String findInterface(String ip) {
String[] ifs = listLocalInterfaces();
if (debug) {
JFLog.log("local interfaces:" + ifs.length + " found");
}
for(int a=0;a<ifs.length;a++) {
String[] dev_ips = ifs[a].split("[,]");
if (debug) {
JFLog.log("local interface:" + dev_ips[0]);
}
for(int b=1;b<dev_ips.length;b++) {
if (dev_ips[b].equals(ip)) {
return dev_ips[0];
}
}
}
return null;
}
/** Start process on local interface. */
public long start(String local_interface, String local_ip, boolean nonblocking) {
this.local_ip = PacketCapture.decode_ip(local_ip);
this.local_mac = PacketCapture.get_mac(local_ip);
return PCapAPI.getInstance().pcapStart(local_interface, nonblocking);
}
/** Start process on local interface with blocking mode enabled. */
public long start(String local_interface, String local_ip) {
return start(local_interface, local_ip, true);
}
/** Stop processing. */
public void stop(long id) {
PCapAPI.getInstance().pcapStop(id);
}
/** Compile program. */
public boolean compile(long handle, String program) {
return PCapAPI.getInstance().pcapCompile(handle, program);
}
/** Read packet. */
public byte[] read(long handle) {
return PCapAPI.getInstance().pcapRead(handle);
}
/** Write packet. */
public boolean write(long handle, byte[] packet, int offset, int length) {
return PCapAPI.getInstance().pcapWrite(handle, packet, offset, length);
}
/** mac address to String */
public static String toString(byte[] mac) {
if (mac == null) {
return "null";
}
StringBuilder str = new StringBuilder();
for(int a=0;a<mac.length;a++) {
if (a > 0) str.append(":");
str.append(String.format("%02x", mac[a]));
}
return str.toString();
}
public static byte[] get_mac(String ip) {
try {
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
while (nics.hasMoreElements()) {
NetworkInterface interf = nics.nextElement();
if (interf.isLoopback()) continue;
Enumeration<InetAddress> nic_ips = interf.getInetAddresses();
while (nic_ips.hasMoreElements()) {
String nic_ip = nic_ips.nextElement().getHostAddress();
if (nic_ip.equals(ip)) {
byte[] mac = interf.getHardwareAddress();
if (debug) {
JFLog.log("IP=" + ip);
toString(mac);
}
return mac;
}
}
}
JFLog.log("Error:mac not found:" + ip);
return mac_zero;
} catch (Exception e) {
JFLog.log("Exception:" + e);
return null;
}
}
public static String build_mac(byte[] mac) {
StringBuilder sb = new StringBuilder();
for(int a=0;a<mac.length;a++) {
if (a > 0) sb.append(':');
sb.append(String.format("%02x", mac[a] & 0xff));
}
return sb.toString();
}
public static boolean valid_ip(String ip) {
return IP4.isIP(ip);
}
public static byte[] decode_ip(String ip) {
String[] ips = ip.split("[.]");
byte[] ret = new byte[ips.length];
for(int a=0;a<ips.length;a++) {
ret[a] = (byte)(int)Integer.valueOf(ips[a]);
}
return ret;
}
public static String build_ip(byte[] ip) {
StringBuilder sb = new StringBuilder();
for(int a=0;a<ip.length;a++) {
if (a > 0) sb.append('.');
sb.append(String.format("%d", ip[a] & 0xff));
}
return sb.toString();
}
public static boolean compare_ip(byte[] ip1, byte[] ip2) {
if (ip1.length != ip2.length) return false;
for(int a=0;a<ip1.length;a++) {
if (ip1[a] != ip2[a]) return false;
}
return true;
}
public static void increment_ip(byte[] ip) {
int pos = ip.length - 1;
while ((ip[pos] & 0xff) == 255) {
ip[pos] = 0;
pos--;
if (pos == -1) pos = ip.length - 1;
}
ip[pos]++;
}
public static int get_ip_range_length(byte[] ip_start, byte[] ip_end) {
int start32 = BE.getuint32(ip_start, 0);
int end32 = BE.getuint32(ip_end, 0);
if (end32 < start32) return -1;
return end32 - start32 + 1;
}
/** Build ethernet header. (14 bytes) */
public static void build_ethernet(byte[] pkt, byte[] dest, byte[] src, int type) {
//dest MAC (6)
//src MAC (6)
//type (2)
int offset = 0;
System.arraycopy(dest, 0, pkt, offset, 6); offset += 6;
System.arraycopy(src, 0, pkt, offset, 6); offset += 6;
BE.setuint16(pkt, offset, type); offset += 2;
}
public static int ethernet_size = 14;
public static int get_ethernet_type(byte[] pkt) {
return BE.getuint16(pkt, 12);
}
public static byte[] mac_broadcast = {-1,-1,-1,-1,-1,-1};
public static byte[] mac_zero = {0,0,0,0,0,0};
public static byte[] ip_broadcast = {-1,-1,-1,-1};
public static byte[] ip_zero = {0,0,0,0};
public static int ARP_REQUEST = 0x0001;
public static int ARP_REPLY = 0x0002;
/** Build ARP header. (28 bytes) */
public static void build_arp(byte[] pkt, byte[] src_mac, byte[] src_ip, byte[] request_ip) {
//hw_type (2) = 0x0001
//proto (2) = 0x0800 (IP4)
//hw_size (1) = 6
//pt_size (1) = 4
//opcode (2) = req=0x0001 reply=0x0002
//src MAC (6)
//src IP (4)
//dst MAC (6) = all zeros
//dst IP (4) = requested IP ?
int offset = ethernet_size;
BE.setuint16(pkt, offset, 0x0001); offset += 2; //hw_type
BE.setuint16(pkt, offset, 0x0800); offset += 2; //proto (IP4)
pkt[offset] = 6; offset++; //hw_size
pkt[offset] = 4; offset++; //pt_size
BE.setuint16(pkt, offset, ARP_REQUEST); offset += 2; //request
System.arraycopy(src_mac, 0, pkt, offset, 6); offset += 6; //src MAC
System.arraycopy(src_ip, 0, pkt, offset, 4); offset += 4; //src IP
System.arraycopy(mac_zero, 0, pkt, offset, 6); offset += 6; //request MAC
System.arraycopy(request_ip, 0, pkt, offset, 4); offset += 4; //request IP
}
public static int arp_size = 28;
public static int get_arp_opcode(byte[] pkt) {
return BE.getuint16(pkt, ethernet_size + 6);
}
public static boolean arp_ip_equals(byte[] pkt, byte[] ip) {
int pkt_offset = ethernet_size + arp_size - 14;
for(int a=0;a<ip.length;a++) {
if (pkt[pkt_offset++] != ip[a]) return false;
}
return true;
}
public static byte[] get_arp_mac(byte[] pkt) {
int pkt_offset = ethernet_size + arp_size - 20; //src_mac
byte[] ret = new byte[6];
System.arraycopy(pkt, pkt_offset, ret, 0, 6);
return ret;
}
/** Returns MAC address for IP address. */
public static byte[] arp(long handle, String target_ip, int ms) {
PacketCapture pcap = new PacketCapture();
//padding (packet must not be < 52 bytes)
if (debug) {
JFLog.log("arp.timeout=" + ms);
}
byte[] ip = decode_ip(target_ip);
byte[] pkt = new byte[ethernet_size + arp_size + 18]; //18 = padding
build_ethernet(pkt, mac_broadcast, local_mac, TYPE_ARP);
build_arp(pkt, local_mac, local_ip, ip);
if (debug) {
JFLog.log("arp.write()");
}
pcap.write(handle, pkt, 0, pkt.length);
int time = 0;
int pkt_length = ethernet_size + arp_size; //min size
while (time < ms) {
do {
if (debug) {
JFLog.log("arp.read()");
}
pkt = pcap.read(handle);
if (debug) {
JFLog.log("arp.pkt=" + pkt);
}
if (pkt != null) {
if (pkt.length >= pkt_length) {
int type = get_ethernet_type(pkt);
if (type == TYPE_ARP) {
int opcode = get_arp_opcode(pkt);
if (opcode == ARP_REPLY) {
if (arp_ip_equals(pkt, ip)) {
return get_arp_mac(pkt);
} else {
if (debug) JFLog.log("packet.ip is unknown!");
}
} else {
if (debug) JFLog.log("packet.opcode=" + opcode + ":expected=" + ARP_REPLY);
}
} else {
if (debug) JFLog.log("packet.type=" + type + ":expected=" + TYPE_ARP);
}
} else {
if (debug) JFLog.log("packet too short:" + pkt.length + ":min=" + pkt_length);
}
}
} while (pkt != null);
JF.sleep(100);
time += 100;
}
return null;
}
}