我正在尝试使用 Pcap4j 库在 java 中发送 TCP SYN 数据包,但我在“ipv4Builder.build();”线上收到 NullPointerException

问题描述 投票:0回答:1

代码和错误应该解释我的问题(如果没有,我可以解释更多)

代码

import org.pcap4j.core.*;
import org.pcap4j.packet.*;
import org.pcap4j.packet.namednumber.TcpPort;
import org.pcap4j.util.MacAddress;
import org.pcap4j.packet.namednumber.EtherType;
import org.pcap4j.packet.namednumber.IpNumber;
import org.pcap4j.packet.namednumber.IpVersion;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Collections;
import java.util.List;

public class SYN {

    public static void send() {
        String targetIp = "8.8.8.8";
        int targetPort = 80;

        try {
            List<PcapNetworkInterface> allDevs = Pcaps.findAllDevs();
            Collections.shuffle(allDevs);
            PcapNetworkInterface nif = null;
            Inet4Address sourceIp = null;
            
            for (PcapNetworkInterface dev : allDevs) {
                System.out.println("Interface: " + dev.getName() + " (" + dev.getDescription() + ")");
                
                for (PcapAddress pcapAddress : dev.getAddresses()) {
                    InetAddress addr = pcapAddress.getAddress();
                    if (addr instanceof Inet4Address) {
                        System.out.println("Found IPv4 Address: " + addr.getHostAddress());
                        if (nif == null) {
                            nif = dev;
                            sourceIp = (Inet4Address) addr;
                            break;
                        }
                    }
                }
                if (nif != null) {
                    break;
                }
            }

            if (nif == null || sourceIp == null) {
                System.out.println("No suitable network interface found.");
                return;
            }
            
            System.out.println("Selected Interface: " + nif.getName());
            System.out.println("Source IP: " + sourceIp.getHostAddress());

            PcapHandle handle = nif.openLive(65536, PcapNetworkInterface.PromiscuousMode.PROMISCUOUS, 10);

            TcpPacket.Builder tcpBuilder = new TcpPacket.Builder();
            tcpBuilder
                    .srcPort(TcpPort.getInstance((short) 80))
                    .dstPort(TcpPort.getInstance((short) targetPort))
                    .sequenceNumber(100)
                    .acknowledgmentNumber(0)
                    .dataOffset((byte) 5)
                    .syn(true)
                    .window((short) 65535)
                    .correctChecksumAtBuild(true)
                    .correctLengthAtBuild(true);

            Inet4Address destIp = (Inet4Address) InetAddress.getByName(targetIp);

            IpV4Packet.Builder ipv4Builder = new IpV4Packet.Builder();
            ipv4Builder
                    .version(IpVersion.IPV4)
                    .tos(IpV4Rfc791Tos.newInstance((byte) 0))
                    .ttl((byte) 64)
                    .protocol(IpNumber.TCP)
                    .srcAddr(sourceIp)
                    .dstAddr(destIp)
                    .payloadBuilder(tcpBuilder)
                    .correctChecksumAtBuild(true)
                    .correctLengthAtBuild(true);
            
            System.out.println(sourceIp == null);
            System.out.println(destIp == null);
            Packet p1 = ipv4Builder.build(); // Error

            handle.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

错误

false // sourceIp is not null
false // destIp is not null
java.lang.NullPointerException: builder.srcAddr: null builder.dstAddr: null
    at org.pcap4j.packet.TcpPacket.<init>(TcpPacket.java:100)
    at org.pcap4j.packet.TcpPacket.<init>(TcpPacket.java:33)
    at org.pcap4j.packet.TcpPacket$Builder.build(TcpPacket.java:432)
    at org.pcap4j.packet.TcpPacket$Builder.build(TcpPacket.java:169)
    at org.pcap4j.packet.IpV4Packet.<init>(IpV4Packet.java:121)
    at org.pcap4j.packet.IpV4Packet.<init>(IpV4Packet.java:32)
    at org.pcap4j.packet.IpV4Packet$Builder.build(IpV4Packet.java:370)

我已经尝试过的事情

我调试了代码没有成功,之后我尝试在线搜索错误。 (我还没有找到任何东西)。 询问 AI 也没有帮助(它只是生成了一些编译错误)

java tcp nullpointerexception packet pcap4j
1个回答
1
投票

当您使用

TcpPacket.Builder.correctChecksumAtBuild()
时,稍后将使用
srcAddr
字段和
dstAddr
字段来计算校验和。因此,在使用
correctChecksumAtBuild
时必须设置这些字段,请参阅
org.pcap4j.packet.TcpPacket
的源代码:

if (builder.correctChecksumAtBuild) {
  if (builder.srcAddr == null || builder.dstAddr == null) {
    StringBuilder sb = new StringBuilder();
    sb.append("builder.srcAddr: ")
        .append(builder.srcAddr)
        .append(" builder.dstAddr: ")
        .append(builder.dstAddr);
    throw new NullPointerException(sb.toString());
  }

根据您想要执行的操作,同时为

srcAddr
对象设置
dstAddr
字段和
TcpPacket
字段,或者不使用
correctChecksumAtBuild
设置。

© www.soinside.com 2019 - 2024. All rights reserved.