打开cmd
ipconfig/all
设置中打开有线网络,查看物理地址
apr -a
局域网同一子网段内直接发包、以及给服务器发包,目的MAC的选择,要理解清楚,服务器是在其它的网段,那么目的MAC应该是发送者所在网段的网关MAC,网关为192.168.181.254,因此MAC地址为58-69-6c-c9-bd-a2。
ConfigDialog.java
package chapter11;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
public class ConfigDialog {
private JpcapCaptor jpcapCaptor;
private String keyData;
private NetworkInterface[] devices = JpcapCaptor.getDeviceList();
private Stage stage = new Stage();
private TextField tfKeywords = new TextField();
public ConfigDialog(Stage parentStage) {
this.stage.initOwner(parentStage);
this.stage.initModality(Modality.WINDOW_MODAL);
this.stage.setResizable(false);
this.stage.setTitle("选择网卡并设置参数");
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER_LEFT);
vBox.setSpacing(10.0D);
vBox.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
ComboBox<String> comboBox = new ComboBox();
comboBox.setMaxWidth(800.0D);
for(int i = 0; i < this.devices.length; ++i) {
comboBox.getItems().add(i + " : " + this.devices[i].description);
}
comboBox.getSelectionModel().selectFirst();
TextField tfFilter = new TextField();
TextField tfSize = new TextField("1514");
CheckBox checkBox = new CheckBox("是否设置为混杂模式");
checkBox.setSelected(true);
HBox hBoxBottom = new HBox();
hBoxBottom.setAlignment(Pos.CENTER_RIGHT);
Button btnConfirm = new Button("确定");
Button btnCancel = new Button("取消");
hBoxBottom.getChildren().addAll(new Node[]{btnConfirm, btnCancel});
Hyperlink link = new Hyperlink("设置抓包过滤器(例如 ip and tcp):");
String url = "https://cosy.univ-reims.fr/~lsteffenel/cours/Master1/Reseaux/0910/captureFilters.htm";
link.setOnAction((event) -> {
try {
Desktop.getDesktop().browse(new URI(url));
} catch (URISyntaxException | IOException var3) {
var3.printStackTrace();
}
});
vBox.getChildren().addAll(new Node[]{new Label("请选择网卡:"), comboBox, link, tfFilter, new Label("包中数据包含的关键字,匹配则显示数据内容(多个关键字为or关系,用空格隔开)"), this.tfKeywords, new Label("设置抓包大小(建议介于68~1514之间):"), tfSize, checkBox, new Separator(), hBoxBottom});
Scene scene = new Scene(vBox);
this.stage.setScene(scene);
btnConfirm.setOnAction((event) -> {
try {
int index = comboBox.getSelectionModel().getSelectedIndex();
NetworkInterface networkInterface = this.devices[index];
int snapLen = Integer.parseInt(tfSize.getText().trim());
boolean promisc = checkBox.isSelected();
this.jpcapCaptor = JpcapCaptor.openDevice(networkInterface, snapLen, promisc, 20);
this.jpcapCaptor.setFilter(tfFilter.getText().trim(), true);
this.keyData = this.tfKeywords.getText();
this.stage.hide();
} catch (Exception var10) {
(new Alert(Alert.AlertType.ERROR, var10.getMessage(), new ButtonType[0])).showAndWait();
}
});
btnCancel.setOnAction((event) -> {
this.stage.hide();
});
}
public JpcapCaptor getJpcapCaptor() {
return this.jpcapCaptor;
}
public String getKeyData() {
return this.keyData;
}
public void showAndWait() {
this.stage.showAndWait();
}
}
PacketCaptureFX.java
package chapter11;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import jpcap.JpcapCaptor;
import jpcap.PacketReceiver;
import jpcap.packet.Packet;
public class PacketCaptureFX extends Application {
private TextArea taDisplay = new TextArea();
private Button btnStart = new Button("开始抓包");
private Button btnStop = new Button("停止抓包");
private Button btnClear = new Button("清空");
private Button btnSetting = new Button("设置");
private Button btnExit = new Button("退出");
private ConfigDialog configDialog;
private JpcapCaptor jpcapCaptor;
private String keyData;
public PacketCaptureFX() {
}
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
BorderPane mainPane = new BorderPane();
VBox display = new VBox();
display.setSpacing(10.0D);
display.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
this.taDisplay.setWrapText(true);
this.taDisplay.setEditable(false);
this.taDisplay.setPrefHeight(250.0D);
display.getChildren().addAll(new Node[]{new Label("抓包信息:"), this.taDisplay});
VBox.setVgrow(this.taDisplay, Priority.ALWAYS);
mainPane.setCenter(display);
this.btnSetting.setOnAction((event) -> {
if (this.configDialog == null) {
this.configDialog = new ConfigDialog(primaryStage);
}
this.configDialog.showAndWait();
this.jpcapCaptor = this.configDialog.getJpcapCaptor();
this.keyData = this.configDialog.getKeyData();
});
this.btnStart.setOnAction((event) -> {
if (this.jpcapCaptor == null) {
if (this.configDialog == null) {
this.configDialog = new ConfigDialog(primaryStage);
}
this.configDialog.showAndWait();
this.jpcapCaptor = this.configDialog.getJpcapCaptor();
this.keyData = this.configDialog.getKeyData();
}
this.interrupt("captureThread");
(new Thread(() -> {
while(!Thread.currentThread().isInterrupted()) {
this.jpcapCaptor.processPacket(1, new PacketCaptureFX.PacketHandler());
}
}, "captureThread")).start();
});
this.btnStop.setOnAction((event) -> {
this.interrupt("captureThread");
});
this.btnClear.setOnAction((event) -> {
this.taDisplay.clear();
});
this.btnExit.setOnAction((event) -> {
this.exit();
});
HBox buttons = new HBox();
buttons.setAlignment(Pos.CENTER);
buttons.setSpacing(10.0D);
buttons.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
buttons.getChildren().addAll(new Node[]{this.btnStart, this.btnStop, this.btnClear, this.btnSetting, this.btnExit});
mainPane.setBottom(buttons);
Scene scene = new Scene(mainPane, 700.0D, 400.0D);
primaryStage.setOnCloseRequest((event) -> {
this.exit();
});
primaryStage.setScene(scene);
primaryStage.show();
}
private void interrupt(String threadName) {
ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
int noThreads = currentGroup.activeCount();
Thread[] lstThreads = new Thread[noThreads];
currentGroup.enumerate(lstThreads);
for(int i = 0; i < noThreads; ++i) {
if (lstThreads[i].getName().equals(threadName)) {
lstThreads[i].interrupt();
}
}
}
private void exit() {
this.interrupt("captureThread");
System.exit(0);
}
class PacketHandler implements PacketReceiver {
PacketHandler() {
}
public void receivePacket(Packet packet) {
Platform.runLater(() -> {
PacketCaptureFX.this.taDisplay.appendText(packet.toString() + "\n");
});
System.out.println(PacketCaptureFX.this.keyData);
if (PacketCaptureFX.this.keyData != null && !PacketCaptureFX.this.keyData.trim().equalsIgnoreCase("")) {
try {
String[] keyList = PacketCaptureFX.this.keyData.split(" ");
String msg = new String(packet.data, 0, packet.data.length, "utf-8");
String[] var4 = keyList;
int var5 = keyList.length;
for(int var6 = 0; var6 < var5; ++var6) {
String key = var4[var6];
if (msg.toUpperCase().contains(key.toUpperCase())) {
Platform.runLater(() -> {
PacketCaptureFX.this.taDisplay.appendText("数据部分:" + msg + "\n\n");
});
break;
}
}
} catch (Exception var8) {
System.err.println(var8.getMessage());
}
}
}
}
}
PacketSender.java
package chapter11;
import java.net.InetAddress;
import jpcap.JpcapSender;
import jpcap.packet.EthernetPacket;
import jpcap.packet.TCPPacket;
public class PacketSender {
public PacketSender() {
}
public static void sendTCPPacket(JpcapSender sender, int srcPort, int dstPort, String srcHost, String dstHost, String data, String srcMAC, String dstMAC, boolean syn, boolean ack, boolean rst, boolean fin) {
try {
TCPPacket tcp = new TCPPacket(srcPort, dstPort, 56L, 78L, false, ack, false, rst, syn, fin, true, true, 200, 10);
tcp.setIPv4Parameter(0, false, false, false, 0, false, false, false, 0, 1010101, 100, 6, InetAddress.getByName(srcHost), InetAddress.getByName(dstHost));
tcp.data = data.getBytes("UTF-8");
EthernetPacket ethernetPacket = new EthernetPacket();
ethernetPacket.frametype = 2048;
tcp.datalink = ethernetPacket;
ethernetPacket.src_mac = convertMacFormat(srcMAC);
ethernetPacket.dst_mac = convertMacFormat(dstMAC);
if (ethernetPacket.src_mac != null && ethernetPacket.dst_mac != null) {
sender.sendPacket(tcp);
System.out.println("发包成功!");
} else {
throw new Exception("MAC地址输入错误");
}
} catch (Exception var14) {
System.err.println(var14.getMessage());
throw new RuntimeException(var14);
}
}
public static byte[] convertMacFormat(String MAC) {
String[] macs = new String[6];
if (MAC.contains("-")) {
macs = doSplit(MAC, "-");
} else {
if (!MAC.contains(":")) {
return null;
}
macs = doSplit(MAC, ":");
}
byte[] result = new byte[6];
for(int i = 0; i < macs.length; ++i) {
result[i] = (byte)Integer.parseInt(macs[i], 16);
}
return result;
}
private static String[] doSplit(String MAC, String splitter) {
return MAC.split(splitter);
}
}
SendPacketFX.java
package chapter11;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import jpcap.JpcapSender;
public class SendPacketFX extends Application {
private CheckBox cbSYN = new CheckBox("SYN");
private CheckBox cbACK = new CheckBox("ACK");
private CheckBox cbRST = new CheckBox("RST");
private CheckBox cbFIN = new CheckBox("FIN");
private TextField tfSrcPort = new TextField();
private TextField tfDstPort = new TextField();
private TextField tfSrcHost = new TextField();
private TextField tfDstHost = new TextField();
private TextField tfSrcMAC = new TextField();
private TextField tfDstMAC = new TextField();
private TextField tfData = new TextField();
private Button btnSend = new Button("发送TCP包");
private Button btnSetting = new Button("选择网卡");
private Button btnExit = new Button("退出");
private NetworkChoiceDialog dialog;
private JpcapSender sender;
public SendPacketFX() {
}
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
BorderPane mainPain = new BorderPane();
HBox ports = new HBox();
ports.setSpacing(10.0D);
ports.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
ports.getChildren().addAll(new Node[]{new Label("源端口:"), this.tfSrcPort, new Label("目的端口:"), this.tfDstPort});
mainPain.setTop(ports);
HBox tags = new HBox();
tags.setSpacing(10.0D);
tags.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
tags.getChildren().addAll(new Node[]{new Label("TCP标识位"), this.cbSYN, this.cbACK, this.cbRST, this.cbFIN});
VBox params = new VBox();
params.setSpacing(10.0D);
params.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
params.getChildren().addAll(new Node[]{tags, new Label("源主机地址"), this.tfSrcHost, new Label("目的主机地址"), this.tfDstHost, new Label("源MAC地址"), this.tfSrcMAC, new Label("目的MAC地址"), this.tfDstMAC, new Label("发送的数据"), this.tfData});
mainPain.setCenter(params);
HBox buttons = new HBox();
buttons.setAlignment(Pos.CENTER);
buttons.setSpacing(10.0D);
buttons.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
buttons.getChildren().addAll(new Node[]{this.btnSend, this.btnSetting, this.btnExit});
mainPain.setBottom(buttons);
this.btnSend.setOnAction((event) -> {
try {
int srcPort = Integer.parseInt(this.tfSrcPort.getText().trim());
int dstPort = Integer.parseInt(this.tfDstPort.getText().trim());
String srcHost = this.tfSrcHost.getText().trim();
String dstHost = this.tfDstHost.getText().trim();
String srcMAC = this.tfSrcMAC.getText().trim();
String dstMAC = this.tfDstMAC.getText().trim();
String data = this.tfData.getText();
PacketSender.sendTCPPacket(this.sender, srcPort, dstPort, srcHost, dstHost, data, srcMAC, dstMAC, this.cbSYN.isSelected(), this.cbACK.isSelected(), this.cbRST.isSelected(), this.cbFIN.isSelected());
(new Alert(AlertType.INFORMATION, "已发送!", new ButtonType[0])).showAndWait();
} catch (Exception var9) {
(new Alert(AlertType.ERROR, var9.getMessage(), new ButtonType[0])).showAndWait();
}
});
this.btnSetting.setOnAction((event) -> {
if (this.dialog == null) {
this.dialog = new NetworkChoiceDialog(primaryStage);
}
this.dialog.showAndWait();
this.sender = this.dialog.getSender();
});
this.btnExit.setOnAction((event) -> {
this.exit();
});
Scene scene = new Scene(mainPain);
primaryStage.setScene(scene);
primaryStage.setTitle("发送自构包");
primaryStage.setWidth(500.0D);
this.dialog = new NetworkChoiceDialog(primaryStage);
this.dialog.showAndWait();
this.sender = this.dialog.getSender();
primaryStage.show();
primaryStage.setOnCloseRequest((event) -> {
this.exit();
});
}
private void exit() {
System.exit(0);
}
}
NetworkChoiceDialog.java
package chapter11;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import jpcap.JpcapCaptor;
import jpcap.JpcapSender;
import jpcap.NetworkInterface;
public class NetworkChoiceDialog {
private JpcapSender sender;
private NetworkInterface[] devices = JpcapCaptor.getDeviceList();
private Stage stage = new Stage();
public NetworkChoiceDialog(Stage parentStage) {
this.stage.initOwner(parentStage);
this.stage.initModality(Modality.WINDOW_MODAL);
this.stage.setResizable(false);
this.stage.setTitle("选择网卡");
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER_LEFT);
vBox.setSpacing(10.0D);
vBox.setPadding(new Insets(10.0D, 20.0D, 10.0D, 20.0D));
ComboBox<String> comboBox = new ComboBox();
comboBox.setMaxWidth(800.0D);
for(int i = 0; i < this.devices.length; ++i) {
comboBox.getItems().add(i + " : " + this.devices[i].description);
}
comboBox.getSelectionModel().selectFirst();
HBox hBoxBottom = new HBox();
hBoxBottom.setAlignment(Pos.CENTER_RIGHT);
Button btnConfirm = new Button("确定");
hBoxBottom.getChildren().addAll(new Node[]{btnConfirm});
vBox.getChildren().addAll(new Node[]{new Label("请选择网卡:"), comboBox, new Separator(), hBoxBottom});
Scene scene = new Scene(vBox);
this.stage.setScene(scene);
btnConfirm.setOnAction((event) -> {
try {
int index = comboBox.getSelectionModel().getSelectedIndex();
NetworkInterface networkInterface = this.devices[index];
this.sender = JpcapSender.openDevice(networkInterface);
this.stage.hide();
} catch (Exception var5) {
(new Alert(AlertType.ERROR, var5.getMessage(), new ButtonType[0])).showAndWait();
}
});
}
public JpcapSender getSender() {
return this.sender;
}
public void showAndWait() {
this.stage.showAndWait();
}
}