【问题标题】:Capturing packets with nodejs on windows在 Windows 上使用 nodejs 捕获数据包
【发布时间】:2012-07-01 14:39:21
【问题描述】:

node.js v0.8.0 , XP / WIN7 (不是 Cygwin)

google 发现 node_pcap (https://github.com/mranney/node_pcap)

但它只支持osx和linux。

有没有适用于 windows 的模块?

谢谢。

.

【问题讨论】:

  • 我认为这在 Windows 上是不可能的,因为内核不像 Linux 那样是开源的。
  • 内核与此无关; node_pcap 在 libpcap 上运行,这是一个用户模式库,在开源和非开源内核上的各种操作系统内核机制上运行。 (对于 Windows,libpcap 的 WinPcap 端口有自己的开源内核模块,可以插入到非开源内核中。)

标签: node.js pcap libpcap winpcap


【解决方案1】:

如果您想要更跨平台的东西(例如,通过 WinPcap 与 Windows 兼容),我在不久前写了 caphttps://github.com/mscdex/cap

【讨论】:

  • 可爱!我可以用它来收听 ARP 广播吗?
  • 我不明白为什么不这样做。您应该能够使用 tcpdump 执行相同类型的过滤。
  • 你能写几句关于我如何用你的库做到这一点吗?
【解决方案2】:

我试图在 Windows 机器上捕获、解码和监视 AMF 请求,并提出了以下解决方案,用于使用 node.jsedge.jspcap.net library 捕获数据包。

确保您拥有正确的 node.js 版本(32 位或 64 位)以及 edge.js 的要求

还要确保在代码中的第 64 行附近更改/删除数据包过滤器。

var edge = require('edge');

var PacketCap = edge.func('cs', function () {/*
    #r "PcapDotNet.Base.dll"
    #r "PcapDotNet.Core.dll"
    #r "PcapDotNet.Core.Extensions.dll"
    #r "PcapDotNet.Packets.dll"
    #r "System.Xml.dll"
    #r "System.Xml.Linq.dll"

    using System.Collections.Generic;
    using System.Linq;
    using PcapDotNet.Core;
    using PcapDotNet.Packets;
    using PcapDotNet.Packets.IpV4;
    using PcapDotNet.Packets.Transport;
    using PcapDotNet.Packets.Http;
    using System.Text;
    using System.Collections;

    async (dynamic data) => {
        var NodeOut = (Func<object,Task<object>>)data.NodeOut;
        IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine;
        if (allDevices.Count == 0)
        {
            Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
        }

        // Print the list
        for (int i = 0; i != allDevices.Count; ++i)
        {
            LivePacketDevice device = allDevices[i];
            Console.Write((i + 1) + ". " + device.Name);
            if (device.Description != null)
                Console.WriteLine(" (" + device.Description + ")");
            else
                Console.WriteLine(" (No description available)");
        }

        int deviceIndex = 0;
        do
        {
            Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
            string deviceIndexString = Console.ReadLine();
            if (!int.TryParse(deviceIndexString, out deviceIndex) ||
                deviceIndex < 1 || deviceIndex > allDevices.Count)
            {
                deviceIndex = 0;
            }
        } while (deviceIndex == 0);

        // Take the selected adapter
        PacketDevice selectedDevice = allDevices[deviceIndex - 1];

        // Open the device
        using (PacketCommunicator communicator = 
            selectedDevice.Open(65536,                                  // portion of the packet to capture
                                                                        // 65536 guarantees that the whole packet will be captured on all the link layers
                                PacketDeviceOpenAttributes.None, // promiscuous mode
                                1000))                                  // read timeout
        {
            Console.WriteLine("Listening on " + selectedDevice.Description + "...");

            using (BerkeleyPacketFilter filter = communicator.CreateFilter("src host 127.0.0.1 and port 80"))
            {
                // Set the filter
                communicator.SetFilter(filter);
            }

            // Retrieve the packets
            Packet packet;
            do
            {
                var encoding = Encoding.Default;
                PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);
                if (packet == null) { continue; }
                if (packet.Ethernet == null) { continue; }
                if (packet.Ethernet.IpV4 == null) { continue; }
                if (packet.Ethernet.IpV4.Tcp == null) { continue; }
                if (packet.Ethernet.IpV4.Tcp.Http == null) { continue; }

                int sourcePort = packet.Ethernet.IpV4.Tcp.SourcePort;
                int destinationPort = packet.Ethernet.IpV4.Tcp.DestinationPort;
                IpV4Address sourceAddress = packet.Ethernet.IpV4.Source;
                IpV4Address destinationAddress = packet.Ethernet.IpV4.Destination;

                IpV4Datagram ip = packet.Ethernet.IpV4;
                TcpDatagram tcp = ip.Tcp;
                HttpDatagram http = tcp.Http;
                string httpBody = "";
                string httpHeader = "";

                try
                {
                    // parse packet
                    await NodeOut(System.Convert.ToBase64String(packet.Buffer));
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                }
            } while (true);
        }
        return "Program Exit!";
    }
*/});

var payload = {
NodeOut: function(input, callback) {
        //console.log("base64 -> " + input)
        var data = new Buffer(input, 'base64');
        try {
            strPacket = data.toString('binary');
            console.log(strPacket + "\r\n");
        }
        catch(error) {
          console.log(error.stack);
        }
        callback(null, "test");
    }
}

PacketCap(payload, function (error, result) {
    if (error) throw error;
    console.log(result);
});

我的来源:http://www.techresx.com/programming/packet-capture-nodejs-edgejs/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 2012-01-05
    • 1970-01-01
    • 2016-08-17
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多