Pcap4J
Pcap4J 是一个 Java 库,用于通过网络接口捕获和发送数据包。它提供了一个简单的 API,使开发人员能够在 Java 应用程序中轻松实现网络数据包捕获、分析和生成。Pcap4J 基于 libpcap 库,这是一种广泛使用的 C 库,用于网络数据包捕获和分析。
主要特点和功能:
1. 数据包捕获:Pcap4J 可以通过网络接口捕获数据包,并提供对数据包内容的访问和分析功能。
2. 数据包发送:Pcap4J 允许用户通过网络接口发送自定义构造的数据包。
3. 跨平台支持:Pcap4J 支持在多个操作系统上运行,包括 Windows、Linux 和 macOS,只要系统上安装了 libpcap 或 WinPcap/Npcap。
4. 丰富的数据包类型支持:Pcap4J 支持多种类型的数据包,包括以太网、IP、TCP、UDP、ARP 等,用户可以轻松地创建和解析这些数据包。
5. 过滤器:Pcap4J 支持使用 Berkeley Packet Filter (BPF) 语法定义捕获过滤器,以便只捕获感兴趣的数据包。
6. 多线程支持:Pcap4J 设计为线程安全,允许在多线程环境中使用。
JSch
1. SSH 连接:通过密码或公钥认证进行 SSH 连接。
2. SFTP:通过 SSH 连接进行文件传输。
3. 端口转发:支持本地和远程端口转发。
4. 执行命令:在远程服务器上执行命令并获取输出。
5. 交互式 shell:打开交互式 shell 会话。
arp扫描
public class scan {
private String interfaceName;
private String subnet;
public void startScan() {
try {
PcapNetworkInterface nif = Pcaps.getDevByName(interfaceName);
if (nif == null) {
System.out.println("No such network interface: " + interfaceName);
return;
}
List<PcapAddress> addresses = nif.getAddresses();
InetAddress srcIpAddr = null;
for (PcapAddress address : addresses) {
if (address.getAddress() instanceof InetAddress) {
srcIpAddr = address.getAddress();
break;
}
}
if (srcIpAddr == null) {
System.out.println("未查到指定接口: " + interfaceName);
return;
}
byte[] srcMacAddr = nif.getLinkLayerAddresses().get(0).getAddress();
int snapLen = 65536;
int timeout = 10;
PcapHandle handle = nif.openLive(snapLen, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, timeout);
for (int i = 1; i < 255; i++) {
String targetIp = subnet + "." + i;
InetAddress targetAddress = InetAddress.getByName(targetIp);
System.out.println("发送 ARP : " + targetIp);
sendArpRequest(handle, srcMacAddr, srcIpAddr, targetAddress);
}
try {
handle.loop(0, new PacketListener() {
@Override
public void gotPacket(Packet packet) {
if (packet.contains(ArpPacket.class)) {
ArpPacket arpPacket = packet.get(ArpPacket.class);
System.out.println("ARP reply from: " + arpPacket.getHeader().getSrcProtocolAddr());
System.out.println("MAC : " + arpPacket.getHeader().getSrcHardwareAddr());
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
handle.close();
} catch (PcapNativeException | NotOpenException | UnknownHostException e) {
e.printStackTrace();
}
}
private void sendArpRequest(PcapHandle handle, byte[] srcMacAddr, InetAddress srcIpAddr, InetAddress targetIpAddr) throws PcapNativeException, NotOpenException {
EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
etherBuilder
.dstAddr(MacAddress.ETHER_BROADCAST_ADDRESS)
.srcAddr(MacAddress.getByAddress(srcMacAddr))
.type(EtherType.ARP);
final byte IPV4_ADDRESS_LENGTH = 4;
ArpPacket.Builder arpBuilder = new ArpPacket.Builder();
arpBuilder
.hardwareType(ArpHardwareType.ETHERNET)
.protocolType(EtherType.IPV4)
.hardwareAddrLength((byte) MacAddress.SIZE_IN_BYTES)
.protocolAddrLength(IPV4_ADDRESS_LENGTH)
.operation(ArpOperation.REQUEST)
.srcHardwareAddr(MacAddress.getByAddress(srcMacAddr))
.srcProtocolAddr(srcIpAddr)
.dstHardwareAddr(MacAddress.ETHER_BROADCAST_ADDRESS)
.dstProtocolAddr(targetIpAddr);
etherBuilder.payloadBuilder(arpBuilder).paddingAtBuild(true);
Packet packet = etherBuilder.build();
handle.sendPacket(packet);
}
icmp扫描
public class ICMPScanner {
public void startScan() {
try {
PcapNetworkInterface nif = Pcaps.getDevByName(interfaceName);
if (nif == null) {
System.out.println("No such network interface: " + interfaceName);
return;
}
List<PcapAddress> addresses = nif.getAddresses();
InetAddress srcIpAddr = null;
for (PcapAddress address : addresses) {
if (address.getAddress() instanceof InetAddress && address.getAddress() instanceof Inet4Address) {
srcIpAddr = address.getAddress();
break;
}
}
if (srcIpAddr == null) {
System.out.println("未查到指定接口: " + interfaceName);
return;
}
int snapLen = 65536;
int timeout = 10;
PcapHandle handle = nif.openLive(snapLen, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, timeout);
for (int i = 1; i < 255; i++) {
String targetIp = subnet + "." + i;
InetAddress targetAddress = InetAddress.getByName(targetIp);
if (targetAddress instanceof Inet4Address) {
System.out.println("发送 ICMP Echo : " + targetIp);
sendIcmpEchoRequest(handle, nif, srcIpAddr, targetAddress);
} else {
System.out.println("IP: " + targetIp + " (不是IPV4)");
}
}
try {
handle.loop(0, new PacketListener() {
@Override
public void gotPacket(Packet packet) {
if (packet.contains(IcmpV4CommonPacket.class)) {
IcmpV4CommonPacket icmpPacket = packet.get(IcmpV4CommonPacket.class);
InetAddress srcAddr = packet.get(IpV4Packet.class).getHeader().getSrcAddr();
System.out.println("收到icmp回复 来自: " + srcAddr);
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
handle.close();
} catch (PcapNativeException | NotOpenException | UnknownHostException e) {
e.printStackTrace();
}
}
private void sendIcmpEchoRequest(PcapHandle handle, PcapNetworkInterface nif, InetAddress srcIpAddr, InetAddress targetIpAddr) throws PcapNativeException, NotOpenException {
try {
byte[] srcMacAddr = nif.getLinkLayerAddresses().get(0).getAddress(); // 获取源 MAC
IcmpV4EchoPacket.Builder icmpEchoBuilder = new IcmpV4EchoPacket.Builder();
icmpEchoBuilder
.identifier((short) 1)
.sequenceNumber((short) 1)
.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[] { 0 }));
IcmpV4CommonPacket.Builder icmpBuilder = new IcmpV4CommonPacket.Builder();
icmpBuilder
.type(IcmpV4Type.ECHO)
.code(IcmpV4Code.NO_CODE)
.payloadBuilder(icmpEchoBuilder)
.correctChecksumAtBuild(true);
IpV4Packet.Builder ipv4Builder = new IpV4Packet.Builder();
ipv4Builder
.version(IpVersion.IPV4)
.tos(IpV4Rfc791Tos.newInstance((byte) 0))
.identification((short) 100)
.ttl((byte) 64)
.protocol(IpNumber.ICMPV4)
.srcAddr((Inet4Address) srcIpAddr)
.dstAddr((Inet4Address) targetIpAddr)
.payloadBuilder(icmpBuilder)
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true);
EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
etherBuilder
.dstAddr(MacAddress.ETHER_BROADCAST_ADDRESS)
.srcAddr(MacAddress.getByAddress(srcMacAddr))
.type(EtherType.IPV4)
.payloadBuilder(ipv4Builder)
.paddingAtBuild(true);
Packet packet = etherBuilder.build();
handle.sendPacket(packet);
} catch (PcapNativeException | NotOpenException e) {
e.printStackTrace();
}
}
}
TCP端口扫描
public class PortScanner {
public void startScan() {
try {
PcapNetworkInterface nif = Pcaps.getDevByName(interfaceName);
if (nif == null) {
System.out.println("未查找到指定接口: " + interfaceName);
return;
}
List<PcapAddress> addresses = nif.getAddresses();
InetAddress srcIpAddr = null;
for (PcapAddress address : addresses) {
if (address.getAddress() instanceof InetAddress && address.getAddress() instanceof Inet4Address) {
srcIpAddr = address.getAddress();
break;
}
}
if (srcIpAddr == null) {
System.out.println(" IPv4 地址错误 : " + interfaceName);
return;
}
int snapLen = 65536;
int timeout = 10;
PcapHandle handle = nif.openLive(snapLen, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, timeout);
for (int port : ports) {
System.out.println("扫描" + port + " -- " + targetIp);
sendTcpSynRequest(handle, nif, srcIpAddr, targetIp, port);
}
try {
handle.loop(0, new PacketListener() {
@Override
public void gotPacket(Packet packet) {
if (packet.contains(TcpPacket.class)) {
TcpPacket tcpPacket = packet.get(TcpPacket.class);
TcpPacket.TcpHeader tcpHeader = tcpPacket.getHeader();
if (tcpHeader.getSyn() && tcpHeader.getAck()) {
System.out.println("端口" + tcpHeader.getDstPort().valueAsInt() + " 已开启");
}
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
handle.close();
} catch (PcapNativeException | NotOpenException e) {
e.printStackTrace();
}
}
private void sendTcpSynRequest(PcapHandle handle, PcapNetworkInterface nif, InetAddress srcIpAddr, String targetIp, int port) throws PcapNativeException, NotOpenException {
try {
byte[] srcMacAddr = nif.getLinkLayerAddresses().get(0).getAddress();
TcpPacket.Builder tcpBuilder = new TcpPacket.Builder();
tcpBuilder
.srcAddr((Inet4Address) srcIpAddr)
.dstAddr((Inet4Address) InetAddress.getByName(targetIp))
.srcPort(TcpPort.getInstance((short) 12345)) // Random
.dstPort(TcpPort.getInstance((short) port))
.sequenceNumber(123)
.acknowledgmentNumber(0)
.window((short) 8192)
.syn(true)
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true);
IpV4Packet.Builder ipv4Builder = new IpV4Packet.Builder();
ipv4Builder
.version(IpVersion.IPV4)
.tos(IpV4Rfc791Tos.newInstance((byte) 0))
.ttl((byte) 64)
.protocol(IpNumber.TCP)
.srcAddr((Inet4Address) srcIpAddr)
.dstAddr((Inet4Address) InetAddress.getByName(targetIp))
.payloadBuilder(tcpBuilder)
.correctChecksumAtBuild(true)
.correctLengthAtBuild(true);
EthernetPacket.Builder etherBuilder = new EthernetPacket.Builder();
etherBuilder
.dstAddr(MacAddress.ETHER_BROADCAST_ADDRESS)
.srcAddr(MacAddress.getByAddress(srcMacAddr))
.type(EtherType.IPV4)
.payloadBuilder(ipv4Builder)
.paddingAtBuild(true);
Packet packet = etherBuilder.build();
handle.sendPacket(packet);
} catch (PcapNativeException | NotOpenException | UnknownHostException e) {
e.printStackTrace();
}
}
}
SSH爆破
public class SSHUtil {
public void sshLogin() {
ConcurrentHashMap<Integer, String> loginResults = new ConcurrentHashMap<>();
ExecutorService executor = Executors.newFixedThreadPool(numLogins);
for (int i = 0; i < numLogins; i++) {
int loginIndex = i + 1;
executor.submit(() -> {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no"); //不使用严格检查
session.connect(1000); // 超时时间 S
if (session.isConnected()) {
loginResults.put(loginIndex, "登录 " + loginIndex + ": 成功");
session.disconnect();
} else {
loginResults.put(loginIndex, "登录 " + loginIndex + ": 失败");
}
} catch (Exception e) {
loginResults.put(loginIndex, "登录 " + loginIndex + ": 异常 - " + e.getMessage());
}
});
}
executor.shutdown();
try {
if (!executor.awaitTermination(1, TimeUnit.MINUTES)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
}
// 打印登录结果
loginResults.forEach((key, value) -> System.out.println(value));
}
}
注意事项
•合法性:仅应在合法授权的网络环境中进行,本代码仅用于网络安全测试或学习目的。
•后果:未经授权的操作可能会导致网络中断、数据泄露或法律后果。
暂无评论
要发表评论,您必须先 登录