mscdex / cap

A cross-platform binding for performing packet capturing with node.js
MIT License
361 stars 45 forks source link

findDevice return undefined for existing ip #61

Closed JaLe29 closed 6 years ago

JaLe29 commented 6 years ago

I am using example code for sniffing and function findDevice return undefined for existing ip (ITS GOOGLE), why? Other ips doenst work too, list of devices working fine, where is a problem? I am using windows 10 - on debian it work fine.

var Cap = require('cap').Cap;
var decoders = require('cap').decoders;
var PROTOCOL = decoders.PROTOCOL;

var c = new Cap();
var device = Cap.findDevice('172.217.19.68');
console.log(device); // UNDEFINED
var filter = 'tcp and dst port 80';
var bufSize = 10 * 1024 * 1024;
var buffer = Buffer.alloc(65535);

var linkType = c.open(device, filter, bufSize, buffer);
mscdex commented 6 years ago

Do you see the IP shown in the output for console.dir(Cap.deviceList()); ? If not, then it may be a permission issue?

JaLe29 commented 6 years ago

What IP?

For console.dir(Cap.deviceList()); i am getting:

node index.js
[ { name: '\\Device\\NPF_{5ED444A2-8F8C-4A3D-AD5F-xxxxxxxxxxxxxxxxxxx}',
    description: 'MS NDIS 6.0 LoopBack Driver',
    addresses: [ [Object], [Object] ] },
  { name: '\\Device\\NPF_{FB665FB8-F265-40D2-B92B-xxxxxxxxxxxxxxxxxxxxxx}',
    description: 'Microsoft',
    addresses: [ [Object], [Object] ] },
  { name: '\\Device\\NPF_{CFA0983B-D663-46C6-A807-xxxxxxxxxxxxxxxxx}',
    description: 'Realtek Ethernet Controller',
    addresses: [ [Object], [Object] ] },
  { name: '\\Device\\NPF_{44876CCE-0D1C-4A28-B442-xxxxxxxxxxxxxxxxx}',
    description: 'TAP-Windows Adapter V9',
    addresses: [ [Object], [Object] ] } ]
undefined
C:\Users\acc\Desktop\dsasda\index.js:16
var linkType = c.open(device, filter, bufSize, buffer);
                 ^

TypeError: device must be a string
    at Object.<anonymous> (C:\Users\acc\Desktop\dsasda\index.js:16:18)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3
mscdex commented 6 years ago

Try this instead, the output is just truncated: console.log(require('util').inspect(Cap.deviceList(), false, Infinity))

JaLe29 commented 6 years ago

Output is:

[ { name: '\Device\NPF{5ED444A2-8F8C-4A3D-AD5F-xxxxxxxxxxxxxxxxxxxx}', description: 'MS NDIS 6.0 LoopBack Driver', addresses: [ { addr: 'fe80::792f:bb3e:f0d5:44a2', netmask: undefined, broadaddr: undefined }, { addr: '0.0.0.0', netmask: '255.0.0.0', broadaddr: '255.255.255.255' } ] }, { name: '\Device\NPF{FB665FB8-F265-40D2-B92B-xxxxxxxxxxxxxx}', description: 'Microsoft', addresses: [ { addr: 'fe80::b893:6bf9:e30:cb35', netmask: undefined, broadaddr: undefined }, { addr: 'fe80::b893:6bf9:e30:cb35', netmask: undefined, broadaddr: undefined } ] }, { name: '\Device\NPF{CFA0983B-D663-46C6-A807-xxxxxxxxxxxxxxxxxxxx}', description: 'Realtek Ethernet Controller', addresses: [ { addr: 'fe80::b506:f877:94c:7343', netmask: undefined, broadaddr: undefined }, { addr: '192.168.1.5', netmask: '255.255.255.0', broadaddr: '0.0.0.0' } ] }, { name: '\Device\NPF{44876CCE-0D1C-4A28-B442-xxxxxxxxxxxxxxxxxxxxxxxxxx}', description: 'TAP-Windows Adapter V9', addresses: [ { addr: 'fe80::b965:4ccf:2ab1:67c8', netmask: undefined, broadaddr: undefined }, { addr: '0.0.0.0', netmask: '255.0.0.0', broadaddr: '255.255.255.255' } ] } ]

mscdex commented 6 years ago

Ok, that would be why it couldn't be found. Those are the only devices/addresses available to cap. Are you sure it's not the 192.168.1.5 address? If not, try running your script/node as administrator and see if it finds the device/address that way.

JaLe29 commented 6 years ago

Oh, administrator mode helped me.

Jean-Baptiste-Lasselle commented 2 years ago

Hello Everyone, I just care to mention for future reader :

    var linkType = c.open(device, filter, bufSize, buffer);
                     ^

    TypeError: device must be a string
        at Object.<anonymous> (C:\Users\Utilisateur\packer_virtualbox\.npm.scripts\ops\utils\arp\scanner.js:51:18)
        at Module._compile (node:internal/modules/cjs/loader:1112:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
        at Module.load (node:internal/modules/cjs/loader:988:32)
        at Module._load (node:internal/modules/cjs/loader:834:12)
        at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
        at node:internal/main/run_main_module:17:47

    Node.js v18.4.0

Now I also care on giving what made it possible for me to use cap :


/**
 * ----------------------------------
 *   VERY IMPORTANT !!!!!
 * ----------------------------------
 * To run this you MUST : 
 * 
 * Install Python3 on Windows
 * 
 * npm i -g node-gyp
 * 
 * install npcap version 0.991 , using the MSI installer [curl -LO https://npcap.com/dist/npcap-0.991.exe]
 * 
 * npmi --save cap
 * 
 * Then rebuild the cap dependencies in 'node_modules/' so that it picks up the npcap you just installed : 
 * 
 *   cd node_modules/cap 
 *   node-gyp configure rebuild
 *   cd ../../
 * 
 * And finaly run this script : 
 * 
 * $ npm run packer:net:arp:dev
 * 
 */