Git Product home page Git Product logo

Comments (8)

mscdex avatar mscdex commented on August 27, 2024

Can you provide the exact code you're trying and any errors you get?

from cap.

majimboo avatar majimboo commented on August 27, 2024

I used the exact first example code on the readme and just changed the ip to 127.0.0.1 then got the error device must be string. If I change the ip to my network IP it works well.

When I try to console log the deviceList() I only get 1 and that is my AMD PCNET Ethernet Adapter using the network IP and that is it.

Further more I tried

device = Cap.findDevice('127.0.0.1')
console.log(device)

Is undefined. (still using the example)

from cap.

mscdex avatar mscdex commented on August 27, 2024

After some more research it seems that you can't listen on loopback addresses on Windows with WinPCap as those packets typically do not get sent to any network drivers and are managed internally by Windows instead. However *nix handles loopback a little differently so you can capture that kind of traffic on those platforms.

However according to this mailing list post, you may be able to capture (higher-level) traffic using some other low level Windows APIs.

from cap.

majimboo avatar majimboo commented on August 27, 2024

I did try some things from microsoft. This wiki on wireshark is very helpful. But it's still no good for me as I still cant get the loopback adapter.

What could rawcap be using, they can capture loopback adapter packets.

from cap.

mscdex avatar mscdex commented on August 27, 2024

It's hard to say what exactly rawcap is using because it doesn't seem to be open source, but it's probably using one of the methods mentioned in the mailing list post I linked to.

Someone wrote a .NET library that uses the Windows Packet Filtering API underneath, so if you're comfortable with .NET you might give that a try.

from cap.

freeall avatar freeall commented on August 27, 2024

@mscdex If I run the first example from the readme on osx and change ip to 127.0.0.1 I get the same issue as mentioned here.

$ sudo node foo.js
/private/tmp/foo.js:11
var linkType = c.open(device, filter, bufSize, buffer);
                 ^

TypeError: device must be a string
    at TypeError (native)
    at Object.<anonymous> (/private/tmp/foo.js:11:18)
    at Module._compile (module.js:434:26)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3

The code is the same as the one in the readme, just added here for clarity:

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

var c = new Cap(),
    device = Cap.findDevice('127.0.0.1'),
    filter = 'tcp and dst port 80',
    bufSize = 10 * 1024 * 1024,
    buffer = new Buffer(65535);

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

c.setMinBytes && c.setMinBytes(0);

c.on('packet', function(nbytes, trunc) {
  console.log('packet: length ' + nbytes + ' bytes, truncated? '
              + (trunc ? 'yes' : 'no'));

  // raw packet data === buffer.slice(0, nbytes)

  if (linkType === 'ETHERNET') {
    var ret = decoders.Ethernet(buffer);

    if (ret.info.type === PROTOCOL.ETHERNET.IPV4) {
      console.log('Decoding IPv4 ...');

      ret = decoders.IPV4(buffer, ret.offset);
      console.log('from: ' + ret.info.srcaddr + ' to ' + ret.info.dstaddr);

      if (ret.info.protocol === PROTOCOL.IP.TCP) {
        var datalen = ret.info.totallen - ret.hdrlen;

        console.log('Decoding TCP ...');

        ret = decoders.TCP(buffer, ret.offset);
        console.log(' from port: ' + ret.info.srcport + ' to port: ' + ret.info.dstport);
        datalen -= ret.hdrlen;
        console.log(buffer.toString('binary', ret.offset, ret.offset + datalen));
      } else if (ret.info.protocol === PROTOCOL.IP.UDP) {
        console.log('Decoding UDP ...');

        ret = decoders.UDP(buffer, ret.offset);
        console.log(' from port: ' + ret.info.srcport + ' to port: ' + ret.info.dstport);
        console.log(buffer.toString('binary', ret.offset, ret.offset + ret.info.length));
      } else
        console.log('Unsupported IPv4 protocol: ' + PROTOCOL.IP[ret.info.protocol]);
    } else
      console.log('Unsupported Ethertype: ' + PROTOCOL.ETHERNET[ret.info.type]);
  }
});

from cap.

mscdex avatar mscdex commented on August 27, 2024

@freeall This should be fixed in 544c65c but you will need to also make sure you run with sudo to make sure you can get available network interface information (and start actual packet capturing). Although I know on linux you can avoid sudo by using setcap, but it needs to be done on the node executable IIRC which may not be desirable. On OS X I think you can just change the permissions on the /dev/bpf* devices (but this is only temporary)?

from cap.

freeall avatar freeall commented on August 27, 2024

@mscdex Thanks a lot for fixing really fast! Now we can continue working on this module we're doing where we want to read all streams. Will probably come out later tonight if it works well.

from cap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.