- Compiling Kernel With Kali Linux
- Install Dependencies
- Download the Kernel Source
- Configure the Kernel
- Write Some Code
- Build the Kernel
- Trouble Shooting
- Install/Remove the Kernel
- Comments
- About Me
- Recent Posts
- Tag Cloud
- Сборка ядра kali linux
- Install Build Dependencies
- Download the Kali Linux Kernel Source Code
- Configure Your Kernel
- Build the Kernel
- Install the Modified Kernel
Compiling Kernel With Kali Linux
With the instructions in Recompiling the Kali Linux Kernel, we can recompile the whole linux kernel of our kali linux.
Install Dependencies
We need to start by installing several build dependency packages, some of them may have already been in installed.
apt install build-essential libncurses5-dev fakeroot bison libssl-dev libelf-dev
Download the Kernel Source
For my system, the kernel version is 4.18, which will be used in following example. Of course, the workflow of other version is just the same.
apt install linux-source-4.18 [. ] ls /usr/src linux-config-4.18 linux-patch-4.18-rt.patch.xz linux-source-4.18.tar.xz
Then we get the compressed archive of the kernel sources, and we’ll extract these files in our working directory, (no special permission need for compiling the kernel). In our example, we use /opt/kernel, and the ~/kernel is also an appropriate place.
mkdir /opt/kernel; cd /opt/kernel tar -xaf /usr/src/linux-source-4.18.tar.xz
Optionally, we may also apply the rt patch, which is for real-time os features.
cd /opt/kernel/linux-source-4.18 xzcat /usr/src/linux-patch-4.18-rt.patch.xz | patch -p1
Configure the Kernel
When building a more recent version of kernel (possibly with an specific patch), the configuration should at first be kept as close as possible to the current running kernel, shown by uname -r. It is sufficient to just copy the currently-used kernel config to the source directory.
cd /opt/kernel/linux-source-4.18 cp /boot/config-4.18.0-kali2-amd64 .config
If you need to make some changes or decide to reconfigure all things from scratch, just call make menuconfig command and inspect all the details. Note: we can tweak a lot in this phase.
Write Some Code
Add one line of code for test(fun), in file init/main.c, start_kernel function
pr_notice("Brooke's customized kernel starting: %s %d\n", __FUNCTION__, __LINE__); pr_notice("%s", linux_banner);
Build the Kernel
Once configured, we can make the kernel. Rather than invoking make deb-pkg as the official doc suggested, we use make bindeb-pkg here, which will not generate Debian source package, or invoke make clean.
time make -j4 bindeb-pkg LOCALVERSION=-custom KDEB_PKGVERSION=$(make kernelversion)-$(date +%Y%m%d)
After a while, we get following package in the parent directory
linux-headers-4.18.10-custom_4.18.10-20181021_amd64.deb # headers linux-image-4.18.10-custom_4.18.10-20181021_amd64.deb # kernel image linux-image-4.18.10-custom-dbg_4.18.10-20181021_amd64.deb # kernel image with debugging symbols linux-libc-dev_4.18.10-20181021_amd64.deb # headers of user-space library
Trouble Shooting
No rule to make target 'debian/certs/test-signing-certs.pem', needed by 'certs/x509_certificate_list'. Stop
Solve: comment/delete the corresponding config line.
Install/Remove the Kernel
dpkg -i ../linux-image-4.18.10-custom_4.18.10-custom_4.18.10-20181021_amd64.deb reboot
Once booted, we can use dmesg to verify our printk message. Removing kernel can also be done with dpkg.
dpkg -r linux-image-4.18.10-custom
Posted by BrookeYang(杨阳) Oct 22 nd , 2018 9:57 am linux
Comments
About Me
I’m Brooke, a software developer interested in system programming as well as web service design.
Recent Posts
Tag Cloud
Copyright © 2020 — BrookeYang(杨阳) Powered by Octopress —>
Сборка ядра kali linux
The customizability of Kali Linux extends all the way down into the Linux kernel.
Depending on your requirements, you might want to add drivers, patches, or kernel features that are not included in the stock Kali Linux kernel. The following guide will describe how the Kali Linux kernel can be quickly modified and recompiled for your needs. Note that global wireless injection patches are already present by default in the Kali Linux kernel.
Install Build Dependencies
Start by installing all the build dependencies for recompiling the kernel:
[email protected]:~$ sudo apt install -y build-essential libncurses5-dev fakeroot xz-utils
Download the Kali Linux Kernel Source Code
The remainder of this section focuses on the 4.9 version of the Linux kernel, but the examples can, of course, be adapted to the particular version of the kernel that you want. We assume that the linux-source-4.9 binary package has been installed. Note that we install a binary package containing the upstream sources, we do not retrieve the Kali source package named linux:
[email protected]:~$ sudo apt install -y linux-source-4.9 Reading package lists. Done Building dependency tree Reading state information. Done The following additional packages will be installed: bc libreadline7 Suggested packages: libncurses-dev | ncurses-dev libqt4-dev The following NEW packages will be installed: bc libreadline7 linux-source-4.9 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Need to get 95.4 MB of archives. After this operation, 95.8 MB of additional disk space will be used. [. ] [email protected]:~$ ls /usr/src linux-config-4.9 linux-patch-4.9-rt.patch.xz linux-source-4.9.tar.xz
Notice that the package contains /usr/src/linux-source-4.9.tar.xz, a compressed archive of the kernel sources. You must extract these files in a new directory (not directly under /usr/src/, since there is no need for special permissions to compile a Linux kernel). Instead, ~/kernel/ is more appropriate:
[email protected]:~$ mkdir -p ~/kernel/ [email protected]:~$ cd ~/kernel/ [email protected]:~/kernel$ tar -xaf /usr/src/linux-source-4.9.tar.xz
Configure Your Kernel
When recompiling a more recent version of the kernel (possibly with an additional patch), the configuration will most likely be kept as close as possible to that proposed by Kali Linux. In this case, and rather than reconfiguring everything from scratch, it is sufficient to copy the /boot/config-version file (the version is that of the kernel currently used, which can be found with the uname -r command) into a .config file in the directory containing the kernel sources:
[email protected]:~/kernel$ cp /boot/config-4.9.0-kali1-amd64 ~/kernel/linux-source-4.9/.config
If you need to make changes or if you decide to reconfigure everything from scratch, you must take the time to configure your kernel. This can be done by calling the make menuconfig command:
The details of using menuconfig to set up a kernel build are beyond the scope of this guide. There is a detailed tutorial on configuring a kernel build on Linux.org.
Build the Kernel
Once the kernel configuration is ready, a simple make deb-pkg will generate up to 5 Debian packages: linux-image-version that contains the kernel image and the associated modules, linux-headers-version, which contains the header files required to build external modules, linux-firmware-image-version, which contains the firmware files needed by some drivers (this package might be missing when you build from the kernel sources provided by Debian or Kali), linux-image-version-dbg, which contains the debugging symbols for the kernel image and its modules, and linux-libc-dev, which contains headers relevant to some user-space libraries like GNU glibc. The Linux kernel image is a big build, expect it to take a while to complete:
[email protected]:~/kernel$ make clean [email protected]:~/kernel$ make deb-pkg LOCALVERSION=-custom KDEB_PKGVERSION=$(make kernelversion)-1 [. ] [email protected]:~/kernel$ ls ../*.deb ../linux-headers-4.9.0-kali1-custom_4.9.2-1_amd64.deb ../linux-image-4.9.0-kali1-custom_4.9.2-1_amd64.deb ../linux-image-4.9.0-kali1-custom-dbg_4.9.2-1_amd64.deb ../linux-libc-dev_4.9.2-1_amd64.deb
Install the Modified Kernel
When the build has successfully completed, you can go ahead and install the new custom kernel and reboot your system. Please note that the specific kernel version numbers will vary — in our example, done on a Kali 2016.2 system, it was 4.9.2. Depending on the current kernel version you’re building, you will need to adjust your commands accordingly:
[email protected]:~/kernel$ sudo dpkg -i ../linux-image-4.9.0-kali1-custom_4.9.2-1_amd64.deb [email protected]:~/kernel$ reboot
Once your system has rebooted, your new kernel should be running. If things go wrong and your kernel fails to boot successfully, you can still use the Grub menu to boot from the original stock Kali kernel and fix your issues.
Updated on: 2023-Mar-06
Author: g0tmi1k