Ejemplo n.º 1
0
        /// <summary>
        ///  Initialize a new instance of NMCapture class.
        /// </summary>
        /// <param name="traceFile"></param>
        public NMCapture2(string traceFile)
            : base(traceFile)
        {
            uint errno;

            errno = ConfigureCaptureEngine(0); //try to configure threading model to multi-threaded.
            if (errno == 0x80010106)
            {
                errno = ConfigureCaptureEngine(2);
            }

            if (errno != 0)
            {
                throw new Exception(FormatErrMsg("Unable to Open Capture engine.", errno));
            }

            //CaptureCallback handle.
            this.CaptureCb = new CaptureCallbackDelegate(this.CaptureCallBack);

            // Create a caputre file for storing trace
            errno = NetmonAPI.NmCreateCaptureFile(
                traceFile,
                CaptureFileSize,
                NmCaptureFileFlag.WrapAround,
                out this.captureFileHandle,
                out this.captureFileSize);

            if (errno != 0)
            {
                throw new Exception(FormatErrMsg("NmCreateCaptureFile() failed", errno));
            }
        }
Ejemplo n.º 2
0
        public void Start()
        {
            uint size;
            uint errno;

            // Creates a capture file which will store the last 10MB of traffic captured
            errno = NetmonAPI.NmCreateCaptureFile(this.filename, 10000000, NmCaptureFileFlag.WrapAround, out this.capFile, out size);

            if (errno != 0)
            {
                Console.Write("Error Creating File");
            }


            errno = NetmonAPI.NmOpenCaptureEngine(out this.capEngine);
            if (errno != 0)
            {
                Console.Write("Error Creating Engine");
            }

            // The "1" below represents the index of the adapter to capture on, this is just a simple example which captures on one fixed adapter.
            errno = NetmonAPI.NmConfigAdapter(this.capEngine, 1, this.pCaptureCallBack, this.capFile, NmCaptureCallbackExitMode.DiscardRemainFrames);
            if (errno != 0)
            {
                Console.Write("Error Configuring Capture");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Start the NMCapture engine to collect traffic data
        /// </summary>
        /// <param name="adapterIndex">index of the target adapter.</param>
        /// <returns></returns>
        public bool StartCapture(List <uint> adapters, string captureFile)
        {
            uint errno;

            // Create a caputre file for storing trace
            errno = NetmonAPI.NmCreateCaptureFile(
                captureFile,
                CaptureFileSize,
                NmCaptureFileFlag.WrapAround,
                out this.captureFileHandle,
                out this.captureFileSize);

            if (errno != 0)
            {
                throw new Exception(FormatErrMsg("NmCreateCaptureFile() failed", errno));
            }

            return(StartCapture(adapters));
        }
Ejemplo n.º 4
0
        public void Test()
        {
            bool             isElevated;
            WindowsIdentity  identity  = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);

            isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);

            Console.WriteLine($"Elevated permissions: {isElevated}");


            capHandler = new CaptureCallbackDelegate(CapHandlerCallback);
            uint ret;

            IntPtr myCapEng;

            ret = NetmonAPI.NmOpenCaptureEngine(out myCapEng);
            if (ret != 0)
            {
                Console.WriteLine("Error {0}\n", ret);
            }
            else
            {
                uint AdptCount;
                ret = NetmonAPI.NmGetAdapterCount(myCapEng, out AdptCount);
                if (ret != 0)
                {
                    NetmonAPI.NmCloseHandle(myCapEng);
                    Console.WriteLine("Error {0}\n", ret);
                }
                else
                {
                    // Creates a capture file which will store the last 10MB of traffic captured
                    ret = NetmonAPI.NmCreateCaptureFile(this.filename, 10000000, NmCaptureFileFlag.WrapAround, out this.capFile, out this.size);

                    if (ret != 0)
                    {
                        Console.Write("Error Creating File");
                    }

                    Console.WriteLine($"Adapters avalable: {AdptCount}");
                    for (uint i = 0; i < AdptCount; i++)
                    {
                        ret = NetmonAPI.NmConfigAdapter(myCapEng, i, capHandler, IntPtr.Zero, NmCaptureCallbackExitMode.ReturnRemainFrames);
                        if (ret != 0)
                        {
                            Console.WriteLine("Could not config {0}, error {1}", i, ret);
                        }
                        else
                        {
                            Console.WriteLine("Configured Adpt {0}", i);
                        }

                        ret = NetmonAPI.NmStartCapture(myCapEng, i, NmCaptureMode.Promiscuous);
                        if (ret != 0)
                        {
                            Console.WriteLine("Could not Start Capture on {0}, error {1}", i, ret);
                        }
                        else
                        {
                            Console.WriteLine("Started Adpt {0}", i);
                        }
                    }

                    System.Threading.Thread.Sleep(5000);

                    for (uint i = 0; i < AdptCount; i++)
                    {
                        ret = NetmonAPI.NmConfigAdapter(myCapEng, i, capHandler, IntPtr.Zero, NmCaptureCallbackExitMode.ReturnRemainFrames);
                        if (ret != 0)
                        {
                            Console.WriteLine("Could not config {0}, error {1}", i, ret);
                        }
                        else
                        {
                            Console.WriteLine("Configured Adpt {0}", i);
                        }
                        Console.WriteLine("Starting Adpt {0} again", i);

                        ret = NetmonAPI.NmStartCapture(myCapEng, i, NmCaptureMode.Promiscuous);
                        if (ret != 0)
                        {
                            Console.WriteLine("Could not Start Capture again on {0}, error {1}", i, ret);
                        }
                        else
                        {
                            Console.WriteLine("Started Adpt {0} again", i);
                        }
                    }

                    for (uint i = 0; i < AdptCount; i++)
                    {
                        ret = NetmonAPI.NmStopCapture(myCapEng, i);
                        if (ret != 0)
                        {
                            Console.WriteLine("Could not Stop Capture on {0}, error {1}", i, ret);
                        }
                        else
                        {
                            Console.WriteLine("Stopped Adpt {0}", i);
                        }
                    }
                }

                NetmonAPI.NmCloseHandle(myCapEng);
            }
        }