5

node.js v0.8.0 、XP / WIN7 (Cygwin ではない)

google で node_pcap を見つけました ( https://github.com/mranney/node_pcap )

ただし、osx と linux のみをサポートしています。

Windows用のモジュールはありますか?

ありがとう。

.

4

2 に答える 2

5

WindowsマシンでAMFリクエストをキャプチャ、デコード、監視しようとしていたところ、node.jsedge.js、およびpcap.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/

于 2013-11-26T07:25:32.623 に答える