1

我用java写了一个服务器,端口是4444。然后我用终端连接到端口4444(我的mysql真实端口是3306)然后用jpcap捕获数据包并将src端口和ip更改为我的服务器端口和ip 并且我将目标端口和 id 更改为 mysql 端口和 ip。我用 jpcapSender 发送新数据包。问题是,当我发送数据包时,它的长度是 74。但是当我捕获该数据包时,它是 60 字节。我猜这是因为 dont_frag 是假的。但改变它并没有帮助。封装连接;

    public class Bridge implements PacketReceiver {
NetworkInterface device;
static int DeviceName;
public Bridge(NetworkInterface device,int deviceName) throws IOException {
ServerSocket ss = new ServerSocket(4444);
    this.device = device;
    this.DeviceName=deviceName;
    System.err.println(DeviceName);
    JpcapCaptor jpcap;
    jpcap = JpcapCaptor.openDevice(device, 2000, false, 20);
      //        System.err.println(jpcap.isNonBlockinMode());
    jpcap.loopPacket(-1, new Bridge());
}

public Bridge() {

}

@Override
public void receivePacket(Packet p) {


    if (p instanceof TCPPacket) {

        TCPPacket myPacket = (TCPPacket) p;

        if(myPacket.dst_port==3306 && myPacket.src_port==4444){

            System.err.println("don't frag"+myPacket.len);

        }
        if (myPacket.dst_port == 4444) {
            if(myPacket.syn==true){
                System.err.println("connecting"+myPacket.len);
                DatalinkPacket dlp=myPacket.datalink;
                System.err.println(dlp.toString());
                try {
                    Server.src_ip = myPacket.src_ip;
                    Server.src_port = myPacket.src_port;
        myPacket.src_ip = InetAddress.getByName("127.0.0.1");// TODO
                                                                            // variable
                    myPacket.src_port = 4444;
                    myPacket.dst_ip = InetAddress.getByName("127.0.0.1");//                         System.err.println("device"+this.DeviceName);
                    myPacket.dst_port = 3306;
                    sendPacket(myPacket);
                } catch (UnknownHostException e) {
                    // Auto-generated catch block
                    e.printStackTrace();
                }//
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (myPacket.syn = false ) {//
                System.err.println("query request");
                MySqlRequestParser parser = new MySqlRequestParser(
                        myPacket.data);
                String query = parser.getQuery();
                System.err.println(query);
                // TODO get rules from db
                // TODO check for rules
                try {
                    Server.src_ip = myPacket.src_ip;
                    Server.src_port = myPacket.src_port;
                //  myPacket.src_ip = InetAddress.getByName("127.0.0.1");// TODO
                                                                            // variable
                    //myPacket.src_port = 4444;
                    myPacket.dst_ip = InetAddress.getByName("127.0.0.1");// TODO
                    myPacket.dst_port = 3306;
                    sendPacket(myPacket);
                } catch (UnknownHostException e) {
                    // Auto-generated catch block
                    e.printStackTrace();
                }//
                catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        if (myPacket.src_port == 3306) {
            System.err.println("response"+myPacket.sequence);
            if (myPacket.syn == false || myPacket.syn == true) {
                System.err.println("response to query");
                myPacket.dst_ip = Server.src_ip;
                myPacket.dst_port = Server.src_port;
                myPacket.src_port = 4444;// TODO about ip it's kinda
                                            // fishiiii
                try {
                    sendPacket(myPacket);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

}

public void sendPacket(TCPPacket packet) throws IOException {
    NetworkInterface[] devices = JpcapCaptor.getDeviceList();
    System.err.println("dev number"+DeviceName);
    NetworkInterface dev=devices[DeviceName];
    System.err.println("in sender packet src ip"
            + packet.src_ip.getHostAddress() + " dst ip "
            + packet.dst_ip.getHostAddress() + " src port "
            + packet.src_port + " dst port " + packet.dst_port+" len"+packet.len+" datalink:"+packet.datalink.toString());
    JpcapSender sender = JpcapSender.openDevice(dev);
    //TCPPacket tp=new TCPPacket(packet.src_port,packet.dst_port, packet.sequence, packet.ack_num, packet.urg, packet.ack, packet.psh, packet.rst, packet.syn, packet.fin, packet.rsv1, packet.rsv2, packet.window, packet.urgent_pointer);
    System.err.println("open device"+packet.len);

    boolean df=packet.dont_frag;
    boolean mf=packet.more_frag;
    boolean rf=packet.rsv_frag;
    packet.dont_frag=true;
    packet.offset=(short)(((packet.offset&0xFF)<<8)|((packet.offset&0xF00)>>8));
    packet.offset=(short)((packet.offset|((mf?0x0020:0)|(df?0x0040:0)|(rf?0x0080:0))));
    System.err.println("don't frag"+packet.dont_frag);
    sender.sendPacket(packet);
    System.err.println("packet sent");
    sender.close();
}

public static void main(String[] arg) throws IOException {
    NetworkInterface[] devices = JpcapCaptor.getDeviceList();
    int localIndex = 0;
    for (int i = 0; i < devices.length; i++) {
        if (devices[i].name.equals("lo")) {
            localIndex = i;
        }
    }
    System.err.println("lo:" + localIndex);
    Bridge br = new Bridge(devices[localIndex],localIndex);

}

}

4

0 回答 0