ffalcinelli / jdivert

A Java binding for WinDivert driver
GNU Lesser General Public License v3.0
24 stars 10 forks source link

Exception in thread "main" always occuring #5

Open RaktimRay opened 5 years ago

RaktimRay commented 5 years ago

Hi, this is my code:

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.logging.Logger;
import java.nio.ByteOrder;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;

import com.github.ffalcinelli.jdivert.Packet;
import com.github.ffalcinelli.jdivert.WinDivert;

import com.github.ffalcinelli.jdivert.exceptions.WinDivertException;
import jdk.internal.org.objectweb.asm.Handle;
import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import org.jnetpcap.packet.JMemoryPacket;
import org.jnetpcap.packet.JPacket;
import org.jnetpcap.packet.PcapPacket;
import org.jnetpcap.packet.PcapPacketHandler;
import org.jnetpcap.protocol.lan.Ethernet;
import org.jnetpcap.protocol.network.Arp;
import org.jnetpcap.protocol.network.Ip4;
import org.jnetpcap.protocol.tcpip.Udp;
import org.jnetpcap.protocol.tcpip.Tcp;
import org.jnetpcap.protocol.JProtocol;

public class fuzzer {

    private static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static void main(String[] args) throws InterruptedException, UnknownHostException, IOException, WinDivertException {
        //Step 1: Creating a list of NICs
        StringBuilder errbuf = new StringBuilder();
        List<PcapIf> listOfDevices = new ArrayList<PcapIf>(); // Will hold list of devices
        int statusCode = Pcap.findAllDevs(listOfDevices, errbuf);
        if (statusCode != Pcap.OK) {
            System.out.println("Error occured: " + errbuf.toString());
            return;
        }
        System.out.println("list of NICs created");
        // We have a list of PcapIf devices to work with now.

        //Step 2: Printing NICs List
        for (int i = 0; i < listOfDevices.size(); i++) {
            System.out.println("#" + i + ": " + listOfDevices.get(i).getName() + " - " + listOfDevices.get(i).getDescription());
        }

        //Step 3: Selecting the desired device and printing it out
        System.out.println("Please select a device by entering it's number: ");
        Scanner deviceSelected = new Scanner(System.in);
        int selection = deviceSelected.nextInt();
        System.out.println("You selected: #" + selection + ": " + listOfDevices.get(selection).getName() + " - " + listOfDevices.get(selection).getDescription());
        PcapIf chosenDevice = listOfDevices.get(selection);

        //Step 4: Opening device
        int snaplen = 64 * 1024; // Capture all packets, no truncation
        int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
        int timeout = 20 * 1000; // 10 seconds in millis
        Pcap pcap = Pcap.openLive(chosenDevice.getName(), snaplen, flags, timeout, errbuf);
        if (pcap == null) {
            System.err.printf("Error while opening device for capture: "
                    + errbuf.toString());
            return;
        }
        System.out.println("Step 4 Completed");

        //Step 5: creating a packet and sending
        WinDivert w = new WinDivert("tcp.DstPort == 102"); //I ideally need                                     //to run an                                         //outbound Tcp                                      //filter on                                         //Tcp.Flag ==                                          //RST, this is just                                                  //for Intial                                        //testing
        w.open(); //Problem here

        InetSocketAddress host = new InetSocketAddress("192.168.0.10", 102);
        Socket socket = new Socket();
        socket.connect(host, 3000);

        Packet packetRST = w.recv();
        System.out.println(packetRST);
        w.send(packetRST);
        w.close();

    }

}

No matter what filter I use I get error either code 87 or 5 and message = "null" I'm trying to do a TCP handshake and the operating system is sending an outgoing ACK, RST right after it send the ACK packet for thr TCP handshake. I need to block this outgoing ACK, RST and since i'm on java thought of using JDivert but then encountered this issue.

hg5134021 commented 3 years ago

try to run as admin, see if it helps.