securitynetworkraspberry piaws

One of the Nmap’s many usages is for asset management as it is very good at discovering devices in a network. I’m going to use it to develop a simple IDS (Intrusion Detection System). Of course IDS software is much more complex and I hope I will look into installing a proper one, like Snort, when I have the time but for now I’ll just roll out my own. My goals in this project are:

  1. Utilise the idle old Raspberry Pi: I used one to build a media server another one as a security camera. The 3rd one is one of the first releases. It has 256MB memory and failed to run various projects I tried in the past. Looks like it’s at least good enough to run Nmap so it may have a use after all.
  2. Practice more Powershell and discover XML parsing capabilities.
  3. Writing a Python version of the same script so that everything can run on Pi but I’ll defer that to a later date.


So like every Raspberry Pi project, first step is to download a distro suitable for Raspberry Pi and write to an SD/miroSD card. There is a Pi version of the (in)famous security distro Kali Linux. It’s convenient as it comes with all security tools pre-installed but for my purposes I just used a plain Raspbian as I only need Nmap.

Nmap that is installed from Linux repositories was a bit outdated (v6.00) so I decided to download the latest version (v6.47) and build it from source. Even though all I need is a simple command I like to keep my tools current!

How Does It Work

I placed the Pi near the switch so that it can use Ethernet. It gets results much faster so I recommend wired over wireless. So the initial version will work like this:

  1. A cron job runs on Pi every n minutes. Executes Nmap and uploads the results in XML format to AWS S3.
  2. A scheduled task runs on a Windows Server running Powershell. It gets the latest XML from S3 and gets the active hosts on the network.
  3. Compares the results to a device list and sends a notification if an intruder is detected.

Of course in order this to work first I need to assign static IPs to all my devices and record these addresses along with MAC addresses in the configuration.

Let’s get cracking!

I covered Nmap basics here. In this project all I need is host discovery so I’m not interested in the services, machine names, operating systems etc. I’ll just run this command:

sudo nmap -sn -oX output.xml

Also I need my old friend s3cmd so I ran this

sudo apt-get install s3cmd


s3cmd --configure

and entered the credentials for the new IAM user I created who has access only to a single S3 bucket.

So to put it together in a shell script I created the simple script below.


echo "Running Nmap"
sudo nmap -sn -oX /home/pi/output.xml

timestamp=$(date +%Y%m%d_%H%M%S)

echo "Uploading the output to S3"
sudo s3cmd put /home/pi/output.xml s3://{BUCKET}/$s3FileName

sudo rm /home/pi/output.xml

Also to make the script executable so I ran this:

sudo chmod 755 my_script

Analyze and alert

So now I have a list of devices running on the network. The Nmap XML output looks something like this:

<?xml version="1.0"?>
<?xml-stylesheet href="file:///usr/bin/../share/nmap/nmap.xsl" type="text/xsl"?>
<!-- Nmap 6.47 scan initiated Mon Dec 15 13:30:01 2014 as: nmap -sn -oX /home/pi/output.xml -->
<nmaprun scanner="nmap" args="nmap -sn -oX /home/pi/output.xml" start="1418650201" startstr="Mon Dec 15 13:30:01 2014" version="6.47" xmloutputversion="1.04">
  <verbose level="0"/>
  <debugging level="0"/>
    <status state="up" reason="arp-response"/>
    <address addr="" addrtype="ipv4"/>
    <address addr="AA:BB:CC:DD:EE:FF" addrtype="mac"/>
    <times srtt="1142" rttvar="5000" to="100000"/>
    <finished time="1418650208" timestr="Mon Dec 15 13:30:08 2014" elapsed="6.40" summary="Nmap done at Mon Dec 15 13:30:08 2014; 256 IP addresses (11 hosts up) scanned in 6.40 seconds" exit="success"/>
    <hosts up="11" down="245" total="256"/>

And my configuration file looks like this:

<?xml version="1.0" encoding="utf-8"?>
  <host name="Dev" ip="" mac="12:12:12:12:12:12" />
  <host name="Raspberry Pi XBMC" ip="" mac="AA:AA:AA:AA:AA:AA" />
  <host name="Printer" ip="" mac="BB:BB:BB:BB:BB:BB" />
  <host name="iPad" ip="" mac="CC:CC:CC:CC:CC:CC" />

A gem I discovered about Powershell is the Powershell ISE (Integrated Shell Environment). It supports IntelliSense-type discovery so makes it much easier and faster to write and run scripts.

Powershell ISE

Into the Powershell

The script does the following

  1. Load the configuration
  2. Get the latest asset list from S3 and load
  3. Compare and see if there are any unknown devices on the network
  4. If there is send an email notification

Since Powershell is based on .NET framework, working with XML is nothing new. I just used standard XPath queries to match the MAC and IP addresses of the discovered devices to the ones I entered to the configuration file.

Here’s the script:

Time for some action

OK let’s see how we are doing now. After I recorded all the known devices the output of the script was like below:

Script output

One interesting thing to note is Nmap cannot discover its own MAC address. I guess that’s because as it’s using ARP protocol to resolve MAC addresses on the local subnet and it doesn’t have its own MAC in its ARP table it cannot find it. I decided to skip the entry but may be a better choice to compare only the IP address if this is the case. Anyway, I will leave it as is for now.

To test it I turned on my old phone and connected to the network. Within 10 minutes I received the following notification email:

So far so good!


I would never trust such a thing as the ultimate defence mechanism but even so I believe it may come in handy in some situations. More importantly this was a fun little project for me as it involved bash scripting, Powershell, AWS and XML. I’m glad I finally came up with a use for the idle Raspberry Pi also happy to discover Powershell ISE.



What is Nmap?

Nmap (Network Mapper) is a powerful network scanner that lets you discover the hosts and services on a network. It sends specific packets to remote hosts and analyses the responses to map the network. These packets can be standard ICMP/TCP/UDP packets as well as deliberately malformed packets to observe the hosts’ behaviour.

I believe it is very important to keep an eye on what’s going on in your network. As Nmap is one of the basic tools for this kind of job, I decided to spend some time to cover it and harness it in my own projects.

Specifying Target

First you need to specify your targets. Nmap is very flexible and accepts different notations for specifying targets:

  • Single IP: i.e.
  • Name: i.e:
  • List: For example, 192.168.1,2.1,10 will scan,, and Note that the comma separated values are not ranges but single values
  • Range: i.e: For ranges hyphen is used as the separator. The start and end values are inclusive. Also one octet can be omitted such as 192.168.-.1. In this case Nmap will scan all IPs from to
  • CIDR (Classless Inter-Domain Routing): For example will scan 8 IPs from to

You can use any combinations of these values as a list separated with spaces such as: will scan the single LAN IP and Google. You can use -sL parameter to view the target list without scanning them.

Nmap list hosts

In complex scenarios you can use an input file to load the target list by using the -iL flag and providing the file name.

To exclude specific IP addresses –exclude flag is used with the same notations.

Port Scanning

Ports can be specified in 2 ways:

  • Using -p flag: Single value, comma-separated list, or hyphen-separated values as a range. If just hyphen is specified it scans all ports from 1 to 65535. Also protocol can be specified such as T:80 U:53
  • Using nmap-services file: You can refer to a service by name and this file is used to look it up.

Both methods can be used in combinations such as:

nmap -p http*,25,U:53


While a scan is running you can an updated status by hitting enter. Also you can save the output results to a file in different formats with the values below following the -o flag:

* N: Normal
* X: XML
* G: Grepable

such as

nmap -v -oG output.gnmap

Also a helpful flag is -v for verbose output

An added bonus about using output files is that you can resume a scan by using the –resume flag and specifying the output file name such as

nmap --resume output.gnmap

Basic scanning options

  • TCP SYN scan (-sS): This is the default option Nmap uses. It’s very fast and can scan thousands of ports per second. It sends a SYN packet to target and if the port is open target sends back a SYN/ACK packet. Thus far it’s just like a normal 3-way TCP handshake but in the final step instead of sending an ACK Nmap sends RST (Reset) packet and cancels the process. Since it has already acquired the information it’s looking for it doesn’t need to establish an actual connection. If the port is closed the target sends a RST packet. SYN scan is very powerful because it’s fast and quiet as it doesn’t create a session. on Linux, it requires root privileges to run it.

  • TCP connect() Scan (-sT): This one uses a full handshake and opens a session. Then sends a RST packet to close the session. The advantage of this method over SYN scan is that it doesn’t require root privileges. As it opens sessions they are logged so it’s noisies than SYN scan.

  • Ping scan (-sn formerly known as -sP): This is the quickest scan method.For local subnets it uses ARP (Address Resolution Protocol) to identify active hosts. ARP only works on local subnets so for remote subnets, it uses ICMP echo requests. It also sends a TCP ACK packet to port 80 which is completely unexpected for the host as it’s just sent out of the blue. So the host sends a RST packet to end connection (as it’s the right thing to do!) but that helps Nmap to identify there is a host up with that address. This scan is only helpful to identify hosts rather than ports and services.

  • UDP scan (-sU): This is the only scan that can identify open UDP ports. Since there’s no handshake the overhead is lower compared to TCP scan. When a port is closed the target returns ICMP port unreachable packet so this may increase the number of packets. Like SYN scan it requires privileged access.

In total there are lots of scanning options. You can find the full list here

OS and Service Version Detection

To detect version -sV flag is used. An intensity level between 0-9 can be specified. Default is 7

	nmap -sV --version-intensity 9

Versioning can be useful in some cases but also significantly increases the scan time.

For operating system detection -O flag can be used

	nmap -O -v

Nmap OS detection

Timing Categories

If you are concerned about being detected when scanning the network

  1. You might be doing something nasty!
  2. You might consider using timing categories so add some delay between each packets to evade IDSs

There are 6 categories that can be specified by T flag followed by a number from 0 to 5. Alternatively you can use the templates’ names:

  • paranoid (0)
  • sneaky (1)
  • polite (2)
  • normal (3)
  • aggressive (4)
  • insane (5)

With “Paranoid” template Nmap will wait 5 minutes between each probe making the total scan time very very long. “Insane” will speed up the process to a point that the delay is down to 5ms. So be careful which option you use. There are many more flags for tailoring the scans to your performance requirements:

Scripting engine

Nmap comes with an embedded Lua interpreter which is the core of its scripting engine.

By using -sC flag all scripts in the default category can be executed such as

nmap -sC -p

Nmap script output

There are lots of scripts which can be found at NSE(Nmap Scripting Engine) documentation page

For example there is a script to scan OpenSSL Heartbleed vulnerability. It can be executed as follows:

nmap -p 443 --script ssl-heartbleed <target>

On my machine this script was blocked by Norton!

Norton attack block

So be careful which script to run. Your intentions may be misinterpreted if you are running them against systems that you are not authorized.


Nmap is one of the core tools that hackers (white or black hat) use. So it has many more options geared towards attacking and being stealthy. You can spoof your IP address, use idle stations to avoid detection etc. I left out many of those options as my intention for studying Nmap is discovering devices on my network so that I can take action if any unknown devices appear. Based on these notes I will develop a simple script/applicaton to find out if anything fishy is going on in my network. I’ll blog about it when it’s ready. Stay tuned!


raspberry pigadgetawspowershells3security

I know there are very cheap security cameras that you can setup in a few minutes. They may provide security but they cannot provide the satisfaction you get after a DIY project! So let’s dig in just for the fun of it.


Component Price Where to buy?
Raspberry Pi Starter Kit £36 Amazon
Camera module £17 Amazon
Protective case for the camera module £4 Amazon
Wireless adaptor £6 Amazon

Once all put together this is what you are going to end up with:

Raspberry Pi Security Camera

Bring it to life

  1. Download a standard distro for Raspberry Pi. I used Rasbian.
  2. Write the image to the SD card. I use Win32 Disk Imager on Windows.

Main course: Motion

There is a great tutorial here for converting your Pi into a security camera which I mostly followed. Basically you enable WiFi, install Motion software and tweak the configuration a bit (image size, framerate etc) and it’s (hopefully) good to go.

The video didn’t work for me unfortunately. It was recording something but only the first frame was visible so it wasn’t any better than a still image. So I decided to ignore videos completely.

Instead of using a network share I decided to upload footage to AWS S3 directly using Amazon S3 Tools. Also don’t forget to clear old footage. Otherwise you can run out of space very quickly. I added a few cron jobs to carry out these tasks for me:

* * * * * s3cmd sync /var/surv/*.jpg s3://{BUCKET NAME}/
* */2 * * * sudo rm /var/surv/*.avi
* */6 * * *  find /var/surv/* -mtime +1 -exec rm {} \;

It syncs the local folder with S3 bucket, deletes all local video files and files older than a day. I delete the video files more frequently as they take up a lot of space.

Monitoring and Notifications

No system is complete without proper monitoring and notifications. It’s especially important for systems like this that’s supposed to run quietly in the background.

Unfortunately in my case it stopped working a few times which made monitoring even more important. I don’t know what’s causing the issue. Maybe it’s because I’m using an older version of Raspberry Pi and it’s not capable of handling all the motion software and S3 uploads etc.

To keep an eye on it, I decided to create a small PowerShell script to check S3 for incoming files and send me a notification if it seems to have stopped uploading.

PowerShell as the glue

Built on .NET framework PowerShell is a very powerful (no pun intended) tool to write quick and dirty solutions. So first here’s the Send-Mail function:

I created a separate function for it as it’s a general-purpose feature which can be used in many places. To make it even more generic you can take out the from and to email addresses and add them as parameters to the function.

And here’s the actual notification logic:

It finds the latest image by sorting them by LastModified field and compares this date with the current date. If it’s been more than 1 day it sends an email. Depending on how often you expect images to be uploaded you can change the alert condition.

To use these scripts you’ll AWS accounts with S3 and SES privileges. Also you have to change the path of the send-mail.ps1 in the line it’s included.