Created
February 9, 2011 02:36
-
-
Save mattstevens/817784 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var sys = require('sys'), | |
path = require('path'), | |
fs = require('fs'), | |
pcap = require('pcap'), pcap_session, | |
tcp_tracker = new pcap.TCP_tracker(); | |
var device = undefined; | |
var filter = ""; | |
for (var i=2; i < process.argv.length; i++) { | |
var arg = process.argv[i]; | |
if (arg == '-h' || arg == '--help') { | |
console.log('usage: zipline [options] [interface|capture file]\n'); | |
console.log('Options:'); | |
console.log(' -f, --filter'); | |
console.log(' Specify a pcap capture filter. See "man pcap-filter" for syntax.\n'); | |
console.log(' -l, --list'); | |
console.log(' Lists available network interfaces.\n'); | |
console.log('Examples:'); | |
console.log(' zipline'); | |
console.log(' Monitor all packets on the default network interface.\n'); | |
console.log(' zipline en0'); | |
console.log(' Monitor all packets on ethernet interface en0.\n'); | |
console.log(' zipline en0 -f "tcp port 80"'); | |
console.log(' Monitor TCP packets sent to or from port 80 on ethernet interface en0.\n'); | |
console.log(' zipline example.pcap'); | |
console.log(' Replay all packets from the capture file example.pcap.\n'); | |
console.log(' zipline example.pcap -f "tcp port 80"'); | |
console.log(' Replay TCP packets sent to or from port 80 in the capture file example.pcap.\n'); | |
process.exit(0); | |
} else if (arg == '-f' || arg == '--filter') { | |
if (process.argv.length > i + 1) { | |
filter = process.argv[++i]; | |
} else { | |
console.error('No filter specified!'); | |
process.exit(1); | |
} | |
} else if (arg == '-l' || arg == '--list') { | |
var session = pcap.createSession(); | |
// Print all devices, default device prefixed with an asterisk | |
session.findalldevs().forEach(function (dev) { | |
if (session.device_name === dev.name) { | |
sys.print('*'); | |
} else { | |
sys.print(' '); | |
} | |
sys.print(dev.name + ' '); | |
if (dev.addresses.length > 0) { | |
dev.addresses.forEach(function (address) { | |
sys.print(address.addr + ' '); | |
}); | |
} else { | |
sys.print('Not connected'); | |
} | |
sys.print("\n"); | |
}); | |
process.exit(0); | |
} else { | |
if (device === undefined) { | |
device = arg; | |
} else { | |
console.error('Unknown argument "' + arg + '"'); | |
process.exit(1); | |
} | |
} | |
} | |
// Start up pcap | |
if (device !== undefined) { | |
// Use offline mode if the device is a capture file, otherwise start | |
// a live session on the specified device | |
path.exists(device, function (exists) { | |
if (exists) { | |
pcap_session = pcap.createOfflineSession(device, filter); | |
} else { | |
pcap_session = pcap.createSession(device, filter); | |
console.log("Listening on " + pcap_session.device_name); | |
} | |
start(); | |
}); | |
} else { | |
pcap_session = pcap.createSession(device, filter); | |
console.log("Listening on " + pcap_session.device_name); | |
start(); | |
} | |
function start() { | |
// Packets are now flowing whether in online or offline mode | |
pcap_session.addListener('packet', function (raw_packet) { | |
var packet = pcap.decode.packet(raw_packet); | |
sys.puts(pcap.print.packet(packet)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment