Package: netgrep
=======netgrep=======Netgrep is a command line tool which tells you which lines in a text filecontain network resources related to a particular country or AutonomousNetwork (AS).Given input, it will:- locate domain names and IP addresses- resolve domains to IP addresses- geo-locate IP addresses to country codes and ASNs- extract country codes from domain names- output each line matching at least one country code or ASN specified.Use case--------You have a big list of infected botnet clients, URLs, or IP addresses, and youwant to find out which ones are related to your country code or AS.Example usage-------------# a simple log file$ cat mylog.txtabc.net.au,Australian Broadcasting Corporationbbc.co.uk,British Broadcasting Corporation203.2.218.214,Australian Broadcasting Corporation IP addressxs4all.com,Hosted in Netherlands with .com extension# match anything hosted in .nl$ netgrep NL mylog.txtxs4all.com,Hosted in Netherlands with .com extension# match Australian IPs and domain names$ netgrep AU mylog.txtabc.net.au,Australian Broadcasting Corporation203.2.218.214,Australian Broadcasting Corporation IP address# match IPs resolving to Autonomous System 2818, owned by BBC$ netgrep AS2818 mylog.txtbbc.co.uk,British Broadcasting Corporation# combination filter with both Australian IPs / domains and AS2818$ netgrep AU,AS2818 mylog.txtabc.net.au,Australian Broadcasting Corporationbbc.co.uk,British Broadcasting Corporation203.2.218.214,Australian Broadcasting Corporation IP addressAdvanced usage--------------* Multiple filesYou can use wildcards or pass in multiple files:$ netgrep AS444 logs/firstlog.txt logs/secondlog.txt...$ netgrep AS444 logs/*.txt...(Note that netgrep can't handle recursive subdirectories as yet.)* Piping standard inputNetgrep supports piping from standard input like this:$ cat input1.txt | netgrep BRYou can use netgrep as a quick little assertion tool. For example, doesakamai.com resolve to any boxes in Singapore?$ echo "akamai.com" | netgrep SGakamai.com$# got output - assertion proven$ echo "akamai.com" | netgrep FI$# no output - assertion failedInstallation============Quick install:--------------Method 11. Make sure you have adns installed.2. $ sudo easy_install netgrepMethod 21. Make sure you have adns installed2. Extract the tarball fromhttps://github.com/csirtfoundry/netgrep/tarball/master3. cd [directory it was unpacked to]4. $ sudo python setup.py installDetailed install:-----------------If the above didn't work out of the box, here's what you can do.You'll need:adns (http://www.chiark.greenend.org.uk/~ian/adns/)Python libraries:BulkWhoispublicsuffixadns-pythonHere's some OS-specific ways to install the prerequisites and netgrep,assuming a clean system:* 1: Linux install with apt-get:$ sudo apt-get install python-setuptools gcc python-dev python-adns$ sudo easy_install netgrep* 2: OS X install:Using homebrew (or your choice of package manager):Install Xcode (needed for building lots of Homebrew packages)$ brew install adns$ sudo easy_install pip$ sudo pip install netgrep* 3: Download and extract the tarball and then (with apt-get):$ sudo apt-get install gcc python-dev python-setuptools python-adns$ sudo python setup.py install* WindowsUntested, and suspect it may not work. If you like to report how it did ordidn't work, please let me know.Installation issues:--------------------When installing adns-python, you may receive:adnsmodule.c:8:20: fatal error: Python.h: No such file or directorysudo apt-get install python-devImplementation notes====================1. Netgrep makes one pass of the logs, extracting any candidate domain name andIP addresses it finds.2. Domain names are checked to see if they resolve to a TLD present in theMozilla Public Suffix List. Anything not matching is ignored.3. IP addresses are checked to ensure they're valid IPv4. IPv6 is currentlynot supported, but there are plans to do add this later.4. Domains are resolved to IP addresses asynchronously. This should be quitefast for hundreds of IP addresses, but may take a little time if you havethousands.5. All IPs gathered both directly from the log and via DNS resolution aresubmitted via bulk query to Team Cymru's bulk whois service, retrievingcountry code and ASN.6. The file is scanned, the country code and ASN filters applied, and matchinglines are output.Other notes-----------* This initial release is focusing on functionality rather than performance formatching. Because of the network lookups it uses, it's not going to be anywherenear as fast as mighty grep:http://lists.freebsd.org/pipermail/freebsd-current/2010-August/019310.html?* Pains have been taken to keep the memory footprint low and network callsasynchronous / bulk where possible.* netgrep only handles one record per line for now, so that means no multilineXML parsing. If this might be useful, let me know[chris.horsley at csirtfoundry dot com].Acknowledgements================Rob Lowe for the conceptPeteris Krumins (http://www.catonmat.net/blog/asynchronous-dns-resolution/)for his no-fuss interface to adns. Made one very slight tweak to returnmultiple A records rather than just the first.