September 6, 2015

KaliPI custom build: how-to cross-compile ARM (Rpi2) on a Kali 2.0 host VM or PC, and gain 256MB RAM on your Rpi2

When I started to build my custom Kali ARM image for the Raspberry PI 2, I spent a lot of time looking for info and failing lengthy builds. So I kept these notes as an exercise to understand the process and speed up further builds.

I like cut-paste recipes for my experiments. By executing every step manually I gain better knowledge and control over what I'm doing, so I can manage over time the evolution of my needs, computing environment, external tools.
Only if and when it will be worthwhile, I will eventually automate the process with a script, however most of the time cut-paste is enough.

The updated version of the how-to and related files are on github: https://github.com/cybersec2k/kalipi-kernel 

The described recipe has been tested using the following build environment:
  • Kali 2.0 stock i686 VM with at least 20GB free hard disk space and 2GB RAM. I tested it also on my Kali 2.0 notebook (I7, 8GB RAM), but with an i686 VM it's easier for more people to reproduce the whole process.
  • A Raspberry PI 2 model B (1GB RAM), an empty 8GB+ microSD card, a microSD to SD adapter to write the microSD on the notebook.
On github you will find mainly:
  • a guided tutorial (pdf)
  • the modified KaliPI build script: rpi2-KaliPI.sh
  • the modified KaliPI kernel config: rpi2-KaliPI-3.18.config
  • a sample build script configuration: rpi2-KaliPI.config
  • a sample build log produced following this guide, which has been fundamental to debug failed builds: rpi2-XXXXXX.log
rpi2-KaliPI.sh is based on Kali rpi2.sh as of Sep 5, 2015: https://github.com/offensive-security/kali-arm-build-scripts/blob/master/rpi2.sh

Main changes over Kali stock build script are:
  • Load the build script configuration rpi2-KaliPI.config 
  • Make sure that the cross compiler can be found in the path.
  • Completely remove the previous build directory.
  • Use apt-cacher-ng depending on the build script configuration.
  • Added some sync to solve some pesky time-dependent issues.
  • Some chenges to the kernel build section.
  • Create the boot partition config.txt file.
  • Other minor changes, look at the differences with a nice gui using:
    meld rpi2.sh rpi2-KaliPI.sh
Kernel config differs only in HIGHMEM and related parameters. This way the CPU RAM grows from 750MB to 976MB.

446c446,447
< # CONFIG_HIGHMEM is not set
---
> CONFIG_HIGHMEM=y
> # CONFIG_HIGHPTE is not set
460a462
> CONFIG_BOUNCE=y
3516d3517
< CONFIG_MMC_SPI=m
4154a4156
> # CONFIG_DEBUG_HIGHMEM is not set

WARNING: while 256MB more memory is a big deal especially for text-mode apps, it may break SonicPI and few other tricky apps.

The final result will be:
root@KaliPI-247ADC:~# free
             total       used       free     shared    buffers     cached
Mem:        998136     152244     845892      13168      13464      84704

instead of:
root@KaliPI-247ADC:~# free
             total       used       free     shared    buffers     cached
Mem:        762468      97792     664676       5416      10276      44072

Comments are welcome, please use the dedicated post on the Kali forum: #####