📜 ⬆️ ⬇️

Kali Linux: packages modification, kernel build, and ISO image creation exercises

Today we publish the translation of the completion of the ninth chapter of the book " Kali Linux Revealed ". There will be summed up such topics as modifying packages, building the kernel and creating your own ISO-images. In addition, here you will find exercises for this chapter, in which, we hope, it will be interesting to try your hand both to those who are already familiar with Kali, and to those who have just begun to master this distribution.

image

9.5. Results


In this chapter, you learned how to modify source packages, which are the basic building blocks of all applications in Kali. In addition, we talked about how to configure, build, and install our own Kali kernels. Next, we talked about the live-build environment and how to create your own Kali ISO images. Also in this chapter we talked about the preparation of bootable flash media with support for permanent storage of information that can be either encrypted or unencrypted. Now we will systematize the main provisions of this chapter and offer you a set of exercises.

9.5.1. Package modification


Modifying Kali packages is usually done by developers, and those who are responsible for maintaining and developing the system, however, it may happen that you will not have enough opportunities provided by the official package assemblies.
')
Therefore, knowing how to build a modified package can be very useful, especially if you want to share a new package with someone, deploy it on multiple machines, or, after installing a new package, easily return the system to its previous state.

When you need to modify a certain program, it is very tempting to download the source code, make changes to it and use this modified version. However, if the program needs an installation, after which it is available throughout the system (that is, using make install ), then such installation will contaminate the file system with files unknown to dpkg , which will soon lead to problems that cannot be detected based on packet dependency analysis. In addition, with this approach to modifying packages, what happened is more difficult to share with other users.

During the creation of a modified package, the overall sequence of actions is always the same: downloading the package with the source code, extracting its contents, making changes, and then building the package. There are many tools that can solve each of these problems.

In order to start rebuilding Kali packages, you first need to download the source package, which includes the main * .dsc file (Debian Source Control) and additional files referenced in the main file.

Source packages are stored on HTTP mirrors. The most effective way to download them is to use a command like apt source source-package-name . To succeed, you need to add the line deb-src to the /etc/apt/sources.list file and update the index files with the apt update command.

In addition, you can use the dge t command (from the devscripts package) to directly download the .dsc file and related materials. In the case of packages prepared specifically for Kali, the source code of which is stored in the Git repository at git.kali.org , you can download the source code using a command like git clone git://git.kali.org/packages/source-package (if after executing such a command nothing is loaded, try switching to the kali/master branch with the git checkout kali/master command git checkout kali/master ).

After downloading the source code, install the packages listed in the build dependencies of the source package using the apt build-dep ./ command. It must be run from the package directory where the source code is located.

Making changes to the source package is to perform the necessary actions from the following list:


After modifying the source package, a binary package can be built using the dpkg-buildpackage -us -uc -b , which is called from the source directory. This command will create an unsigned binary package. The resulting package can then be installed using this command: dpkg -i package-name_version_arch.deb .

9.5.2. Kernel build


Advanced system users sometimes need to recompile the Kali kernel. You may need this to reduce the size of the standard kernel, which, by default, contains many features and drivers. Building a new kernel may be needed to add non-standard drivers or features to it, or to install kernel patches. It’s worth knowing that an incorrectly configured kernel can make the system unstable, and you should understand that the Kali team cannot provide security updates for kernels that users build on their own.
In most cases, to make changes to the kernel, you will need to install some packages using the apt install build-essential libncurses5-dev fakeroot .

The apt-cache search ^linux-source command should list the latest kernel versions created by the Kali support team. A command like apt install linux-source-version-number installs a compressed archive with kernel source code in /usr/src .

Files with source code should be unpacked with the tar -xaf in a directory other than /usr/src (for example, in ~kernel ).

When it comes time to tune the kernel, keep in mind the following considerations:


If you run the command make clean from the directory with the source code of the kernel, this will delete the previously compiled files. The make deb-pkg command will create up to five Debian packages. A file whose name looks like linux-image-version.deb contains a kernel image and related modules.

In order to use the new kernel, you need to install the necessary packages using a command like dpkg -i file.deb . You can’t do without the linux-image package. The “linux-headers” package needs to be installed only if there are external kernel modules for building, which happens if you have installed * -dkms-packages (you can check if this is the case with the dpkg -l "*-dkms" | grep ^ii command dpkg -l "*-dkms" | grep ^ii ). Other packages, in most cases, are not used.

9.5.3. Build your own Kali ISOs


Kali's official ISO images are compiled using a set of live-build scripts, which allows you to fully automate the work and customize all aspects of creating ISO images.

To use live-build you need the system to be updated to the latest version.

Kali live-build configuration data can be downloaded from Kali Git repositories using the apt install curl git live-build , followed by the git clone git://git.kali.org/live-build-config.git .

To create an updated, but unmodified Kali ISO image, just use the ./build.sh --verbose command. The assembly will take a lot of time, since during the execution of this operation all the necessary packages will be loaded. After the build is complete, a new ISO image will be found in the images directory. If you use the option --variant variant when executing this command, the specified version of the image will be assembled. Different variants are defined by their configuration directories, whose names look like config/variant-* . The main image is created using the gnome variant.

There are several approaches to setting up an ISO image, which are to make changes to the live-build configuration directory:


Deploying the Kali ISO image on a USB stick is pretty simple. At the same time on such media, you can configure a permanent storage of information. Although this process may seem a bit complicated, upon closer inspection, it turns out that it is quite easy to create encrypted and unencrypted repositories on portable media. This greatly expands the functionality of such bootable media.

Now we present to your attention the exercises for this chapter.

Exercises on working with packages and on building the Kali core


Exercise number 1: creating forks Kali packages


  1. Create a fork for the kali-meta package.
  2. Include a new meta package that contains 3 of your favorite tools and nothing more.
  3. Create a binary .deb file suitable for future use.

▍ Answers to exercise number 1


1. First, configure the system to work with source code packages:

 apt-get install devscripts #  dch nano /etc/apt/sources.list #   : deb-src http://http.kali.org/kali kali-rolling main non-free contrib 

Now download the source code. Please note that if you follow these instructions, you may encounter other versions of the packages:

 apt-get update apt source kali-meta #    kali-linux-*. cd kali-meta-2017.2.0/ ls -l nano debian/control 

2. Change the control file, include its own packages in it:

 Package: kali-linux-muts Architecture: any Depends: ${misc:Depends}, kali-linux, aircrack-ng, nmap, sqlmap, Description: Kali Linux Custom tools for muts This is Kali Linux, the most advanced penetration testing and security auditing distribution. . This metapackage depends on few of muts' favorites. 

Let's change the version number of the package so that the reassembled packages can be distinguished from the original ones:

 root@kali:~/kali-meta-2017.2.0# head -1 debian/changelog root@kali:~/kali-meta-2017.2.0# dch --local muts -m "Added a new metapackage" root@kali:~/kali-meta-2017.2.0# head -1 debian/changelog #  ,     

And finally, we will assemble the package:

 root@kali:~/kali-meta-2017.2.0# dpkg-buildpackage -us -uc -b # Disable signatures (-us -uc), binary-only build (-b) root@kali:~/kali-meta-2017.2.0# ls -l ../*muts* -rw-r--r-- 1 root root  6804 Aug 28 13:42 gqrx_2017.2.0muts1_all.deb -rw-r--r-- 1 root root  6948 Aug 28 13:42 kali-desktop-common_2017.2.0muts1_all.deb -rw-r--r-- 1 root root  6972 Aug 28 13:42 kali-desktop-gnome_2017.2.0muts1_all.deb -rw-r--r-- 1 root root  6796 Aug 28 13:42 kali-desktop-kde_2017.2.0muts1_all.deb -rw-r--r-- 1 root root  6920 Aug 28 13:42 kali-desktop-live_2017.2.0muts1_all.deb -rw-r--r-- 1 root root  6812 Aug 28 13:42 kali-desktop-lxde_2017.2.0muts1_all.deb -rw-r--r-- 1 root root  6868 Aug 28 13:42 kali-desktop-xfce_2017.2.0muts1_all.deb -rw-r--r-- 1 root root  7068 Aug 28 13:42 kali-linux_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  7062 Aug 28 13:42 kali-linux-all_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  7290 Aug 28 13:42 kali-linux-forensic_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  8732 Aug 28 13:42 kali-linux-full_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  6850 Aug 28 13:42 kali-linux-gpu_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  6844 Aug 28 13:42 kali-linux-muts_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  7298 Aug 28 13:42 kali-linux-nethunter_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  7088 Aug 28 13:42 kali-linux-pwtools_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  6864 Aug 28 13:42 kali-linux-rfid_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  6908 Aug 28 13:42 kali-linux-sdr_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  6924 Aug 28 13:42 kali-linux-top10_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  6942 Aug 28 13:42 kali-linux-voip_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  7360 Aug 28 13:42 kali-linux-web_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root  7058 Aug 28 13:42 kali-linux-wireless_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root 10312 Aug 28 13:42 kali-meta_2017.2.0muts1_amd64.buildinfo -rw-r--r-- 1 root root  8343 Aug 28 13:42 kali-meta_2017.2.0muts1_amd64.changes root@kali:~/kali-meta-2017.2.0# ls -l ../kali-linux-muts_2017.2.0muts1_amd64.deb -rw-r--r-- 1 root root 6852 Aug 28 14:05 ../kali-linux-muts_2017.2.0muts1_amd64.deb 

Here is the Asciinema solution (the text from the video cannot be copied).

Exercise 2: Kali Pack Update


  1. Prepare the most recent version of the SET toolkit for use with Kali.
  2. Create a binary file for future use.
  3. Can the aircrack-ng package be updated using the same approach?

▍ Answers to exercise number 2


1. First, download the Kali version of SET:

 apt source set 

Next, download the latest version of SET. Here, as in the previous exercise, you may encounter other version numbers:

 wget https://github.com/trustedsec/social-engineer-toolkit/archive/master.tar.gz -O set_7.7.1.orig.tar.gz tar xvf set_7.7.1.orig.tar.gz 

In order not to get confused, rename the latest version:

 mv social-engineer-toolkit-master social-engineer-toolkit-7.7.1 

Copy everything related to Debian:

 cp -a set-7.7/debian social-engineer-toolkit-7.7.1/debian rm -rf social-engineer-toolkit-7.7.1/.git 

Update the version number:

 cd social-engineer-toolkit-7.7.1 head -1 debian/changelog dch -v 7.7.1-0muts1 "New upstream release" head -1 debian/changelog 

2. We will assemble the package and check its performance:

 dpkg-buildpackage -us -uc -b ls -l ../*muts* #    dpkg -i ../set_7.7.1-0muts1_all.deb #    

3. In the case of aircrack-ng everything is done in a similar way:

 nano /etc/apt/sources.list apt-get update apt source aircrack-ng wget https://github.com/aircrack-ng/aircrack-ng/archive/master.tar.gz tar zxpf master.tar.gz mv aircrack-ng-master aircrack-ng-1.3 cp -rf aircrack-ng-1.2-0~rc4/debian/ aircrack-ng-1.3/ cd aircrack-ng-1.3/ head -1 debian/changelog dch -v 1:1.3 -m "Upstream update" nano debian/changelog dpkg-checkbuilddeps apt-get install libgcrypt-dev libgcrypt11-dev libnl-genl-3-dev libpcap0.8-dev libpcre3-dev libsqlite3-dev pkg-config zlib1g-dev dpkg-buildpackage -us -uc -b 

Exercise number 3: rebuilding the kernel


When building a standard Kali kernel, the principle of including the maximum functionality in it works. This is needed to support the widest possible range of hardware platforms.

  1. Install a performance measurement tool like likwid and run a quick test likwid-bench .
  2. Install the graysky2 "Kernel GCC patch" patch in order to optimize the kernel for your processor.
  3. Recompile the kernel after installing the patch and choosing your processor model.

▍ Answers to exercise number 3


1. Install likwid , run the test:

 apt-get install likwid likwid-topology -g | head #      likwid-bench -t copy -w S0:100kB:1 #    socket 0 

2. A patch for optimizing the kernel for a specific processor can be found here . Install the dependencies needed to perform a kernel recompilation. Here, as usual, you may encounter other versions:

 apt install build-essential libncurses5-dev fakeroot apt-cache search ^linux-source #     linux-source package apt install linux-source-4.9 #   ls /usr/src # which gets extracted to /usr/src 

3. Extract the source code of the kernel and copy where it will be, the current configuration file:

 mkdir ~/kernel; cd ~/kernel tar -xaf /usr/src/linux-source-4.9.tar.xz cp /boot/config-4.9.0-kali3-amd64 ~/kernel/linux-source-4.9/.config # Copy existing config 

Run menuconfig on unpatched kernel configuration:

 cd linux-source-4.9 make menuconfig 

Go through the following menu items and see the type of processor:

 Processor type and features  ---> Processor family (Generic-x86-64)  ---> 

Exit configuration without saving. Download the optimizing patch and patch the kernel:

 cd ~/kernel wget https://raw.githubusercontent.com/graysky2/kernel_gcc_patch/master/enable_additional_cpu_optimizations_for_gcc_v4.9%2B_kernel_v3.15%2B.patch cd linux-source-4.9/ patch -p1 < ../enable_additional_cpu_optimizations_for_gcc_v4.9+_kernel_v3.15+.patch make menuconfig 

Now again we will enter the section of the configuration program that allows you to customize the type of processor. New items should appear here:

 Processor type and features  ---> Processor family (Generic-x86-64)  ---> 



Select the desired processor type, save the configuration and exit the program. Now we will assemble the core:

 export CONCURRENCY_LEVEL=5 #  CPU +1 make deb-pkg LOCALVERSION=-custom KDEB_PKGVERSION=$(make kernelversion)-1 

Let's start testing:

 likwid-topology -g | head #      likwid-bench -t copy -w S0:100kB:1 #    socket 0 

Have test scores improved? How do you feel? Maybe something like this happened?


Exercises for assembling live images


Exercise # 1: Kali Special Live Build


In one of the series Mr. Robot Angela needs to boot from the Kali USB disk and, in order to hack the system, enter several commands. Can you put together an ISO image of Kali that will allow her to solve the problem faster? Automate your work so that after loading Kali, Angela would not have to touch the keyboard.

▍ Answers to exercise number 1


To solve this problem you need to collect Live-ISO for Angela and give it to her.

 #  ! apt-get update apt-get dist-upgrade #    apt install -y git live-build cdebootstrap curl #    Kali live build git clone git://git.kali.org/live-build-config.git #    ,       cd live-build-config/ cat kali-config/variant-default/package-lists/kali.list.chroot echo cryptsetup > kali-config/variant-default/package-lists/kali.list.chroot echo openssh-server >> kali-config/variant-default/package-lists/kali.list.chroot echo nmap >> kali-config/variant-default/package-lists/kali.list.chroot #    live-  ( ,   ) mkdir -p kali-config/common/includes.chroot/lib/systemd/system/ #    "Angela"   /usr/bin/startssh cat << EOF > kali-config/common/includes.chroot/lib/systemd/system/angela.service [Unit] Description=Start Custom Script After=multi-user.target [Service] Type=idle ExecStart=/bin/bash /usr/bin/startssh [Install] WantedBy=multi-user.target EOF #  /usr/bin (  )  Live-  mkdir -p kali-config/common/includes.chroot/usr/bin/ #   "startssh",      cat << EOF > kali-config/common/includes.chroot/usr/bin/startssh #!/bin/sh echo hola > /root/test.txt EOF #  ,   cat << EOF > kali-config/common/hooks/live/angela.chroot #!/bin/sh systemctl enable angela.service || true EOF #    chmod 755 kali-config/common/hooks/live/angela.chroot #    ,  , -, ,    cat << EOF > kali-config/common/includes.binary/isolinux/isolinux.cfg include menu.cfg default vesamenu.c32 prompt 0 timeout 20 ONTIMEOUT live-amd64 EOF #  ISO! ./build.sh --verbose 

Exercise # 2: The automatic installer of Kali minimal live build


  1. Create a self-installing ISO image containing the minimum possible number of packages, and include only openssh-server and salt-minion .
  2. Add your public SSH key to this image to facilitate subsequent access to it.
  3. Check the performance of the image.

▍ Answers to exercise number 2


A live-build uses the entire directory structure as input to its configuration. We store this configuration and some supporting scripts in the live-build-config Git repository. We will use this repository as a base for building specially customized images. Install the necessary packages and load the Git repository with the Kali live-build configuration:

 apt install curl git live-build git clone git://git.kali.org/live-build-config.git cd live-build-config 

Create a list of packages to include in the image, starting with salt-minion :

 echo salt-minion > kali-config/variant-default/package-lists/kali.list.chroot 

:

 echo openssh-server >> kali-config/variant-default/package-lists/kali.list.chroot 

preseed.cfg :

 mkdir -p kali-config/common/includes.installer wget https://www.kali.org/dojo/preseed.cfg -O ./kali-config/common/includes.installer/preseed.cfg 

ISO- , includes.binary . , install.cfg ( ):

 cat << EOF > kali-config/common/includes.binary/isolinux/install.cfg label install menu label ^Install linux /install/vmlinuz initrd /install/initrd.gz append vga=788 --quiet file=/preseed.cfg locale=en_US keymap=us hostname=kali domain=local.lan EOF 

isolinux.cfg :

 cat << EOF > kali-config/common/includes.binary/isolinux/isolinux.cfg include menu.cfg ui vesamenu.c32 default install prompt 0 timeout 5 EOF echo 'systemctl enable ssh' >  kali-config/common/hooks/live/01-start-ssh.chroot 

ISO- SSH-:

 ssh-keygen -f /root/.ssh/id_rsa -t rsa -N '' #    ,     mkdir -p kali-config/common/includes.chroot/root/.ssh/ cat /root/.ssh/id_rsa.pub > kali-config/common/includes.chroot/root/.ssh/authorized_keys 

:

 ./build.sh --verbose 

№3: LIve-USB


Live- LUKS.

▍ №3


, USB-, , 2.1.4., « DVD-ROM USB- ». , , ISO- ( 3 ) , . , , Linux - /dev/sdb , , ISO- ( /dev/sdb1 /dev/sdb2 ). , , , , .

( ) dmesg fdisk . , , /dev/sdb . - , . :

 umount /dev/sdb1 umount /dev/sdb2 parted /dev/sdb 

. , — .

 (parted) print                                                           Model: SanDisk Ultra USB 3.0 (scsi) Disk /dev/sdb: 124GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number  Start   End     Size    Type     File system  Flags 1      32.8kB  2794MB  2794MB  primary               boot, hidden 2      2794MB  2794MB  721kB   primary (parted) mkpart primary 2794 5000 (parted) mkpart primary 5000 100%                                        (parted) quit                                                            Information: You may need to update /etc/fstab. 

mkpart primary 2794 5000 ( , ), 2794 , 5000 . , Kali 2794 . , , ISO- -.

, Kali. , sdb3 . , persistence . , . , !

 mkfs.ext3 /dev/sdb3 e2label /dev/sdb3 persistence 

persistence.conf , , . :

 mkdir -p /mnt/usb mount /dev/sdb3 /mnt/usb echo "/ union" > /mnt/usb/persistence.conf umount /mnt/usb 

, . cryptsetup , , , , persistence.conf , :

 cryptsetup --verbose --verify-passphrase luksFormat /dev/sdb4 cryptsetup luksOpen /dev/sdb4 my_usb mkfs.ext3 /dev/mapper/my_usb e2label /dev/mapper/my_usb persistence ls -l /dev/disk/by-label mkdir -p /mnt/my_usb mount /dev/mapper/my_usb /mnt/my_usb echo "/ union" > /mnt/my_usb/persistence.conf umount /dev/mapper/my_usb cryptsetup luksClose /dev/mapper/my_usb 

That's all! USB- , : , , .

, (LUKS Nuke) , :

 cryptsetup luksAddNuke /dev/sdb4 


9- « Kali Linux Revealed ». , Kali, ISO- .

Dear readers! , ?

:

→ 1. Kali Linux: ,
→ 2. Kali Linux: netfilter
→ 3. Kali Linux:
→ 4. Kali Linux:
→ 5. Kali Linux:
→ 6. Kali Linux:
→ 7. Kali Linux:
→ 8. Kali Linux:
→ Part 9. Kali Linux: modifying packages
→ Part 10. Kali Linux: building the kernel
→ Part 11. Kali Linux: setting up and building ISO images

Source: https://habr.com/ru/post/342406/


All Articles