5

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

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

但它只支持 osx 和 linux。

有没有适用于 windows 的模块?

谢谢。

.

4

2 回答 2

8

如果你想要更跨平台的东西(例如通过 WinPcap 与 Windows 兼容),我写了cap一会儿:https ://github.com/mscdex/cap

于 2013-08-11T19:32:17.587 回答
5

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

确保您拥有正确的 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/

于 2013-11-26T07:25:32.623 回答