From charlesreid1

Building my own Widy the Hacker Gadget.

The Background

What we'll be doing is installing OpenWrt, an open-source Linux distribution that can be installed onto router firmware, onto a TP Link 3040 battery-powered router.

This router was selected becase it has a USB port, which will enable us to extend the filesystem of the OpenWrt device.

The Procedure

Our steps will be as follows:

Install OpenWrt on Router

Start Up the Router

Turn on the router.

You'll want to connect to the router via the ethernet port, so make sure it is in 3G or WISP mode (where it expects clients to connect via ethernet), not in AP mode (where it expects to be able to connect to a modem via ethernet).

Navigate to the built-in router control page at 192.168.0.1. From here you can change the settings of the router, but you can also install firmware updates. This is how we'll install OpenWrt on our router.

Go to System and Firmware Update. We'll download an OpenWrt image and point the router to this image as the firmware update.

Download OpenWrt

I downloaded Barrier Breaker for the router. After you've got it installed, you'll see this banner at each login:

root@OpenWrt:~# cat /etc/banner 
  _______                     ________        __
 |       |.-----.-----.-----.|  |  |  |.----.|  |_
 |   -   ||  _  |  -__|     ||  |  |  ||   _||   _|
 |_______||   __|_____|__|__||________||__|  |____|
          |__| W I R E L E S S   F R E E D O M
 -----------------------------------------------------
 BARRIER BREAKER (14.07, r42625)
 -----------------------------------------------------
  * 1/2 oz Galliano         Pour all ingredients into
  * 4 oz cold Coffee        an irish coffee mug filled
  * 1 1/2 oz Dark Rum       with crushed ice. Stir.
  * 2 tsp. Creme de Cacao
 -----------------------------------------------------

I first found my specific router on the OpenWrt wiki, and followed the instructions contained there.

Note that version 1 (v1) will not work, you must use version 2 (v2).

Also note that there is no version 2 available for Attitude Adjustment.

Firmware Update

Now update the firmware by going to the router control panel at 192.168.0.1

Go to System Tools > Firmware Upgrade.

Find the OpenWrt .bin image file you downloaded, and hit go.

IMPORTANT: You are flashing the hardware of the router. Anytime you flash the hardware, you have the potential of bricking the device. Make sure you're ready, you don't have a low battery, you don't accidentally close the browser, etc etc.

Wait for the progress bar to reach 100%. Eventually the router will turn off and turn back on, and your browser won't be able to reload the page. That's because your router is now running OpenWrt. Congratulations!!!

OpenWrt on the Router

To use OpenWrt, navigate to 192.168.1.1 in the browser and you should see the OpenWrt web interface.The first thing you'll be asked to do is set up a password for the router. Once you do that, you'll have an all-in-one Linux box, like a trimmed-down Raspberry Pi. You can control all sorts of stuff from the web interface - things you can control when you're running Linux. It's become a very souped-up router! You can also SSH to the router.


Extend OpenWrt Filesystem

To use an external filesystem like a USB thumbdrive with our OpenWrt system, we'll want to add some software to tell OpenWrt how to use a USB drive. That's because to make OpenWrt small enough to fit on a tiny battery-powered router's firmware, you have to make OpenWrt pretty stupid.

But before we can install modules, we have to connect OpenWrt to the internet.

Connect OpenWrt to the Internet

You can turn this tiny router from an access point (Master mode) into a wireless client (Client mode), a really cool feature that makes this doubly valuable as a gadet.

Go to Wifi > Network.

Turn on scan, and wait for it to present a list of wifi networks.

Connect to your desired wifi network.

Boom. If you ssh into the box, you should be able to reach the outside world:

$ ifconfig
$ ping microsoft.com

Install USB Kernel Modules

Let's install some modules, so OpenWrt knows how to talk to an external mounted drive.

We'll use opkg, the package manager for OpenWrt.

Start by taking a look at all the installed software:

$ opkg list-installed

Now we'll need to install some kernel modules:

$ opkg install kmod-usb-storage kmod-fs-ext4 block-mount kmod-scsi-core

Let's break that down:

kmod-scsi-core installs kernel modules required for communicating with external USB devices.

kmod-usb-storage installs kernel modules required to use USB storage devices. This is a hardware thing.

kmod-fs-ext4 installs kernel modules required to use ext4 filesystems. This is a software thing.

block-mount installs kernel modules required to mount block devices, like file systems. This is a hardware+software thing.

Got it? Good.

The space on the device is limited, so it's gonna be tight, but I had no problem with the above packages. If you do get into a jam, you may be able to uninstall some packages with opkg remove. There are other options, too. One less than optimal solution is to use a USB device mounted on the router over a network connection (like plugging a thumb drive into a desktop, and being able to use it from the router).

Hopefully everything works out. Remember, first things first! Don't install too much stuff!

Verify Support for Device

Now you should be able to plug the USB device into the router, and use the dmesg command to see that it was recognized as a USB storage device. You should see the manufacturer and size show up, if everything goes correctly.

Now you need to format the flash drive, but you'll need to do that somewhere else - there isn't enough space to install anything, and Gparted is a nice tool to use on the desktop anyway.

Format USB Thumb Drive

Now we'll format our USB thumb drive for use onboard the Widy hacker gadget. Plug it in to any Linux computer, and format that puppy. I created one single partition using the whole disk, and it was an ext4 partition.

Eject the drive when you're finished.

Mount USB Thumb Drive on Router

Now is the moment youv'e been waiting for - we break the bonds of tiny storage and have a vast, brand-new 4 GB partition to play with!

Plug your thumb drive into your router.

SSH into the router.

Now look for activity by running the dmesg command:

$ dmesg
[ 1066.400000] usb 1-1: new high-speed USB device number 3 using ehci-platform
[ 1066.560000] usb-storage 1-1:1.0: USB Mass Storage device detected
[ 1066.560000] scsi1 : usb-storage 1-1:1.0
[ 1067.600000] scsi 1:0:0:0: Direct-Access     Kingston DataTraveler G3  PMAP PQ: 0 ANSI: 0 CCS
[ 1069.170000] sd 1:0:0:0: [sda] 7827456 512-byte logical blocks: (4.00 GB/3.73 GiB)
[ 1069.180000] sd 1:0:0:0: [sda] Write Protect is off
[ 1069.180000] sd 1:0:0:0: [sda] Mode Sense: 23 00 00 00
[ 1069.180000] sd 1:0:0:0: [sda] No Caching mode page found
[ 1069.190000] sd 1:0:0:0: [sda] Assuming drive cache: write through
[ 1069.200000] sd 1:0:0:0: [sda] No Caching mode page found
[ 1069.200000] sd 1:0:0:0: [sda] Assuming drive cache: write through
[ 1069.230000]  sda: sda1
[ 1069.240000] sd 1:0:0:0: [sda] No Caching mode page found
[ 1069.240000] sd 1:0:0:0: [sda] Assuming drive cache: write through
[ 1069.250000] sd 1:0:0:0: [sda] Attached SCSI removable disk

Yippeeee! Now mount it:

$ mkdir usb/
$ mount /dev/sda1 usb/
$ 

Woo hoooooooooooooo!!!!

Pivot to USB Drive

Now that we've successfully gotten a USB drive mounted on the router, we can pivot the entire filesystem onto the USB drive.

First, we'll copy everything in the root filesystem / to the USB drive.

Copy Everything to USB Drive

$ mkdir -p usb
$ mount /dev/sda1 usb/

$ mkdir -p rootcopy/
$ mount --bind / rootcopy/

Now connect a tar compression stream (cvf) with a tar expansion stream (xf) to copy everything onto the USB drive:

$ tar -C rootcopy/ -cvf - . | tar -C usb/ -xf -

Now you can unmount the filesystems:

umount rootcopy/
umount usb/

Edit Fstab

Now we can edit the fstab file to tell the operating system which partitions to mount where.

Change the fstab entry for /dev/sda1 so that it mounts as the root filesystem, and turn on the boot flag.

My fstab file was basically emtpy.

Add an entry that looks like this:

config mount
        option target /    
        option device /dev/sda1
        option fstype ext4
        option options rw,sync
        option enabled 1
        option enabled_fsck 0
 

Reboot

Now reboot the device.

Confirm You Have A Metric Ton Of Storage Space

Once you log in, you can check the size of all your partitions, and that should show you how big your / root filesystem is:

$ df -h
Filesystem                Size      Used Available Use% Mounted on
rootfs                    3.6G     16.7M      3.4G   0% /
/dev/root                 2.3M      2.3M         0 100% /rom
tmpfs                    14.1M     76.0K     14.0M   1% /tmp
/dev/sda1                 3.6G     16.7M      3.4G   0% /
tmpfs                   512.0K         0    512.0K   0% /dev

Baaaaziiiiiiiiiiiiiinggggg!!! Give yourself a raise. You just upgraded from a dozen MB to 3.5 GBs.

Now Comes The Fun Part

Building a wireless attack aircraft carrier: Wireless Attack Aircraft Carrier