欺骗本地应用程序之间的udp数据包的源ip和端口

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

我的服务器和客户端之间有一个白名单。我希望我的客户通过我的白名单连接到我的服务器。我的白名单的要点是忽略任何没有白名单源的 UDP 数据包。如果他们在白名单中,我想将带有原始源 ip 和源端口的 udp 数据包传递到我的服务器,该服务器也托管在同一台机器上。然而,当我这样做时,源 ip 仍然是本地主机,我能够欺骗端口但是 ip 仍然保持不变。

我的白名单侦听 udp4 消息,当它收到一个带有刚刚向它发送消息的远程客户端的源和端口的消息时,从头开始创建一个新数据包,最后在将它传递到服务器之前附加它刚刚收到的消息,因为正在从头开始创建数据包我希望正确设置 ip 和端口(即欺骗!)。但是据我所知,ip 仍然是 127.0.0.1。下面您将看到我的 dgram 侦听器、数据包创建和使用原始套接字库发送。

//Udp proxy
const dgram = require('dgram');
const client = dgram.createSocket('udp4');
const raw = require ("raw-socket");
const socket = raw.createSocket({ protocol: 17,addressFamily:1 });

client.on('message', function(message, remote) {
    const whiteListedClientPort = remote.port;
    const whiteListedClientIP = remote.address;
    console.log(`Received ${message.length} bytes from ${remote.address}:${remote.port}`);
    // Create the UDP packet buffer
    const packet = Buffer.alloc(20 + message.length);
    packet.writeUInt16BE(whiteListedClientPort, 0); // Source port
    packet.writeUInt16BE(targetPort, 2); // Destination port
    packet.writeUInt16BE(8 + message.length, 4); // Length (UDP header + message)
    packet.writeUInt16BE(0x0000, 6); // Checksum (disabled)
    const sourceIpParts = whiteListedClientIP.split('.').map(part => parseInt(part, 10));
    packet.writeUInt8(sourceIpParts[0], 12);
    packet.writeUInt8(sourceIpParts[1], 13);
    packet.writeUInt8(sourceIpParts[2], 14);
    packet.writeUInt8(sourceIpParts[3], 15);
    const destinationIpParts = targethostIp.split('.').map(part => parseInt(part, 10));
    packet.writeUInt8(destinationIpParts[0], 16);
    packet.writeUInt8(destinationIpParts[1], 17);
    packet.writeUInt8(destinationIpParts[2], 18);
    packet.writeUInt8(destinationIpParts[3], 19);
    message.copy(packet, 20); // Message data

    console.log(" source ip is ",whiteListedClientIP," source port is ",whiteListedClientPort)
    socket.send(packet, 0, packet.length, targethostIp, (error) => {
      if (error) {
        console.error(`Error sending IPv4 packet: ${error.message}`);
      } else {
        console.log(`Sent packet to ${targethostIp} with a length of ${packet.length}`);
      }
  });
});
client.on('error', function (err) {
    console.log("something went wrong!")
    this.close();
})
client.bind(3000);

我还设置了一个侦听器来检查我的工作,这基本上输出从我的白名单应用程序获得的任何内容。

var dgram = require('dgram');
var s = dgram.createSocket('udp4');
s.on('message', function(msg, rinfo) {
  console.log('I got this message: ' + msg.toString());
  console.log(rinfo)
});
s.bind(3002);
console.log("listening");

下面是示例输出之一,在这种情况下,我能够欺骗源端口,但不能欺骗仍然保留为本地主机的源 ip。

{ address: '127.0.0.1', family: 'IPv4', port: 3169, size: 14 }

我完全坚持这一点,所以我很感激任何意见!

node.js raw-sockets dgrams
© www.soinside.com 2019 - 2024. All rights reserved.