Wednesday, August 23, 2017

Never Before Seen (NBS)

I don't recall exactly where I first heard of Marcus Ranum's Never Before Seen (NBS) anomaly detector script. It could have been in a SANS class with Paul Henry or in one of the Security Weekly Podcasts, but recently I came across the note to myself to install it and learn how it works.

NBS is a Perl script that uses a Berkeley database to store arbitrary strings of your choosing and will then output when it comes across a string it doesn't have in the database. i.e. a string that is "never before seen."

This can be useful in many situations when looking for anomalies. The man page pdf that downloads with it describes using NBS to detect new access requests to a web server. The web server logs must be parsed and the URL field fed into NBS. If it isn't in the database already, it is added and also written to the output file.  Once enough normal traffic has been seen to have established a baseline in the database, entries into the output would be considered anomalies worthy of review.

Since you provide the strings to NBS, it can be useful in many applications. Here are some that came to mind for me:

  • AppLocker allowed executables pulled out of event logs
  • Bro log data such as file hashes, known hosts, known services, user agent strings
  • DNS queries in the DNS log

Installation:

I wanted to test using NBS with Bro logs so I installed it on my Security Onion Virtual machine. (v14.04.5.2)
  1. Downloaded nbs and extracted it:
    1. mkdir nbs
    2. cd nbs
    3. wget http://www.ranum.com/security/computer_security/code/nbs.tar
    4. tar -xf nbs.tar
  2. Installed BerkelyDB prerequisite. The install file that downloads with NBS is a little dated as far as the link for the libraries from what I could tell. These are the steps I took. (requires you to create an Oracle site login)
    1. wget http://download.oracle.com/otn/berkeley-db/db-6.2.23.NC.tar.gz
    2. gzip -d db-6.2.23.NC.tar.gz
    3. tar -xf db-6.2.23.NC.tar
    4. cd db-6.2.23.NC
    5. sudo apt-get install g++
    6. cd build_unix
    7. ../dist/configure --prefix=/usr/local/berkeleydb --enable-compat185 --enable-cxx --enable-debug_rop --enable-debug_wop  (NOTE: the --enable-rpc is no longer supported)
    8. make
    9. sudo make install
    10. sudo su -
    11. echo '/usr/local/berkeleydb/lib/' >> /etc/ld.so.conf.d/libc.conf
    12. ldconfig
    13. apt-get install libdb-dev
    14. apt-get install libdb++-dev
  3. Installed NBS.  It is an older Perl script and I'm not a Linux guru, so this part might not be perfect. Just using make gave some errors so the following edits had to be made first:
    1. I edited the both nbs.c  and nbspreen.o and made the following changes:
      1. replaced %d with %ld in the line referenced in the error [fprintf( ...);]
      2. added #include "errno.h"
      3. commented out //extern int errno;
    2. I then ran make in the NBS directory.
      1. I did keep getting a "count variable unused" error but as far as I could tell NBS was functioning properly.
  4. Configured NBS for use with Bro dns.log data. Bro is running on this system monitoring traffic.
    1. Created the database
      1. sudo ./nbsmk -d ~/nbs/nbs_dnsDB
    2. Established the baseline for DNS data from the Bro dns.log for the db to monitor
      1. sudo cat /nsm/bro/logs/current/dns.log | bro-cut query answers | sudo nbs -d nbs_dnsDB -o new_dns_entries
    3. Viewed the new_dns_entries file. Since this was the first run, all the domain names from the dns.log file are listed.
      1. cat new_dns_entries
    4. Did a domain name lookup for a new domain for Bro to catch and add to the dns.log
      1. nslookup www.rit.edu
    5. Manually ran the updated dns.log file through NBS again
      1. sudo cat /nsm/bro/logs/current/dns.log | bro-cut query answers | sudo nbs -d nbs_dnsDB -o new_dns_entries
    6. Viewed the new_dns_entries file which was overwritten with just this new "never before seen" domain.
      1. cat new_dns_entries   //shows www.rit.edu and the answer received

Next Steps:

So the next steps would be to set up a cron job to send batches of the data to NBS instead of the manual cat/bro-cut command.  Theoretically, you'd want to set this up so that only truly worrisome "never before seen" items are logged. Another mechanism would be necessary to monitor the output file for any activity and either email you or alert in some way.  

References: