This is a set of bindings from
libpcap to node as well as some useful libraries to decode, print, and
libpcap is a packet capture library used by programs like
It has been tested on OSX and Linux.
Writing servers that capture packets, process them somehow, and then serve the processed data up in some way is very straightforward in node.
Node has a very good HTTP parser that is used to progressively decode HTTP sessions.
You will need
libpcap installed. Most OSX machines seem to have it. All major Linux distributions have it available
either by default or with a package like
The easiest way to get
node-pcap and its tools is with
npm install pcap2
If you want to hack on the source code, you can get it from github. Clone the repo like this:
git clone git://github.com/andygreenegrass/node-pcap.git
To compile the native code bindings, do this:
cd node-pcapnode-gyp configure build
Assuming it built without errors, you should be able to run the examples and then write your own packet capture programs.
There are several example programs that show how to use
node-pcap. These examples are best documentation.
Try them out and see what they do.
To use this library in your own program,
pcap_binding.node must be in
takes care of this automatically.
To start a capture session, create a new
pcap.Session object with an interface name and desired options:
var pcap =pcapSession = interface options;
interface is the name of the interface on which to capture packets. If passed an empty string,
will try to pick a "default" interface, which is often just the first one in some list and not what you want.
node-pcap always opens the interface in promiscuous mode, which generally requires running as root.
Unless you are recklessly roaming about as root already, you'll probably want to start your node program like this:
sudo node test.js
pcap.Session is an
EventEmitter that emits a
packet event. The only argument to the callback will be a
Buffer object with the raw bytes returned by
Listening for packets:
var packet = pcapdecode;
The protocol stack is exposed as a nested set of objects. For example, the TCP destination port is part of TCP which is encapsulated within IP, which is encapsulated within a link layer. Access it like this:
This structure is easy to explore with
TCP can be analyzed by feeding the packets into a
TCPTracker and then listening for
var pcap =tcpTracker =pcapSession = 'en0'filter: 'ip proto \\tcp';tcpTracker;pcapSession;
You must only send IPv4 TCP packets to the TCP tracker. Explore the
session object with
see the wonderful things it can do for you. Hopefully the names of the properties are self-explanatory:
TSO is a technique that modern operating systems use to offload the burden of IP/TCP header computation to the network hardware. It also reduces the number of times that data is moved data between the kernel and the network hardware. TSO saves CPU when sending data that is larger than a single IP packet.
This is amazing and wonderful, but it does make some kinds of packet sniffing more difficult. In many cases,
it is important to see the exact packets that are sent, but if the network hardware is sending the packets,
these are not available to
libpcap. The solution is to disable TSO.
sudo sysctl -w net.inet.tcp.tso=0
Linux (substitute correct interface name):
sudo ethtool -K eth0 tso off
The symptoms of needing to disable TSO are messages like, "Received ACK for packet we didn't see get sent".
node-pcap does not know how to decode IPv6 packets yet. Often when capturing traffic to
localhost, IPv6 traffic
will arrive surprisingly, even though you were expecting IPv4. A common case is the hostname
localhost, which many client programs will
resolve to the IPv6 address
::1 and then will try
127.0.0.1. Until we get IPv6 decode support, a
libpcap filter can be
set to only see IPv4 traffic:
sudo http_trace lo0 "ip proto \tcp"
The backslash is important. The pcap filter language has an ambiguity with the word "tcp", so by escaping it, you'll get the correct interpretation for this case.
There are several levels of buffering involved in capturing packets. Sometimes these buffers fill up, and you'll drop packets. If this happens, it becomes difficult to reconstruct higher level protocols. The best way to keep the buffers from filling up is to use pcap filters to only consider traffic that you need to decode. The pcap filters are very efficient and run close to the kernel where they can process high packet rates.
If the pcap filters are set correctly and
libpcap still drops packets, it is possible to increase
buffer size. At the moment, this requires changing
pcap_binding.cc. Look for
set to a larger value.
Copyright (c) 2010 Matthew Ranney, http://ranney.com/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.