暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

9.2 重新编译 Linux 内核

NetHunterPark 2019-03-29
974

题图:初春·长城 @西大庄科


# 第九章 高阶应用

《Kali Linux解密》不完全翻译手册


## 9.2 重新编译 Linux 内核


Kali 提供的内核包含了尽可能多的功能数量,当然也包含了最大数量的驱动支持,以尽可能的覆盖当前可用的硬件配置。这也是一些用户要重新编译内核的原因,他们只想合入自己需要的内容。选择重新编译内核还有两个原因,首先是优化内存消耗,因为所有内核代码都会占用物理内存,即使它们从来都没有被使用过。由于内核中静态编译的部分永远都不会被移动到交换分区,因此编译进内核但又从不使用的功能和驱动将会使得系统的整体性能下降。其次,减少驱动和内核功能的数量就是减少系统的安全风险,因为只有一小部分的内核代码会运行。


重要提醒

---------------------------------------

如果你选择编译自己的内核,你就必须接受一个结果:Kali 不能提供针对你定制的内核安全补丁。使用 Kali 提供的内核就不会有这个问题,Debian 项目会维护好内核补丁。


如果你想要使用某个特定的功能,而这个功能仅仅在补丁(不在标准的内核版本里)上可用时,重新编译内核也就是必要的了。


The Debian Kernel Handbook

---------------------------------------

Debian 内核团队维护了一个《Debian 内核指南》,里面非常全面的介绍了绝大多数内核相关的任务以及官方版本内核包的处理过程。如果对于内核你需要了解超出本节所讲的内容,建议你首先看一下这个手册。


http://kernel-handbook.alioth.debian.org/


### 9.2.1 介绍和准备


毫不意外,Debian 和 Kali 都是以软件包的形式来管理内核的,而不是传统的编译和安装方式。由于内核也在软件包系统的管理之下,所以它也可以被很干净的移除掉或者部署到多台机器上。而且,和这些包关联的脚本还可以自动和 bootloader 及 initrd generator 进行交互。


上游的 Linux 源码已经包含了编译内核软件包所需要的一切,但是你还需要安装 build-essential 软件包,以保证拥有构建 Debian 软件包所需的工具。而且,内核的配置还需要 libncurses5-dev 软件包。最后,fakeroot 软件包会让你无需管理员权限就可以创建 Debian 软件包。


Black13eard@kali:~$ sudo apt install build-essential libncurses5-dev fakeroot


### 9.2.2 获取源码


由于 Linux 内核源码以软件包的形式管理,因此,你可以通过安装 linux-source-version 包来获取源码。通过 apt-cache search ^linux-source 命令可以查看由 Kali 提供的最新内核版本。注意,这些软件包的源码不会和 Linus Torvalds 及内核开发者发布的完全相同。和其他的发行版本一样,Debian 和 Kali 也会更新一系列的补丁,有些会(或不会)合入上游的 Linux 版本。这些修改一般包含新内核版本中对已修复/功能层面/驱动层面的 backport 支持、还未合入(或完全合入)上游 Linux tree 的新功能以及某些时候针对 Debian 和 Kali 的特殊修改等。


本节主要关注 4.14 版本的 Linux 内核,当然,这个例子的过程也适用于你想要编译的那个内核版本。


在这个例子中,我们假设 linux-source-4.17 二进制包已经安装好了。注意,我们安装的二进制包包含了上有的源码,但是没有获取名为 linux 的 Kali 源码包。


Black13eard@kali:~$ sudo apt install linux-source-4.17

[sudo] password for Black13eard:

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following packages were automatically installed and are no longer required:

libargon2-0 libfreefare0 libgeoclue-2-0 libqgis-analysis2.18.21 libqgis-core2.18.21 libqgis-gui2.18.21 libqgis-networkanalysis2.18.21 libqgis-server2.18.21

libqgispython2.18.21 libradare2-2.7 libsndio6.1 python-ipy python-ply python-pycryptodome python-pysmi python-pysnmp4 python-pysnmp4-apps python-pysnmp4-mibs smitools

Use 'sudo apt autoremove' to remove them.

The following additional packages will be installed:

bc

The following NEW packages will be installed:

bc linux-source-4.17

0 upgraded, 2 newly installed, 0 to remove and 116 not upgraded.

Need to get 107 MB of archives.

libargon2-0 libfreefare0 libgeoclue-2-0 libqgis-analysis2.18.21 libqgis-core2.18.21 libqgis-gui2.18.21 libqgis-networkanalysis2.18.21 libqgis-server2.18.21

libqgispython2.18.21 libradare2-2.7 libsndio6.1 python-ipy python-ply python-pycryptodome python-pysmi python-pysnmp4 python-pysnmp4-apps python-pysnmp4-mibs smitools

Use 'sudo apt autoremove' to remove them.

The following additional packages will be installed:

bc

The following NEW packages will be installed:

bc linux-source-4.17

0 upgraded, 2 newly installed, 0 to remove and 116 not upgraded.

Need to get 107 MB of archives.

After this operation, 107 MB of additional disk space will be used.

Do you want to continue? [Y/n]

[...]

Black13eard@kali:~$ ls /usr/src/

kernellinux-config-4.17  linux-source-4.17.tar.xz  volatility-tools

Black13eard@kali:~$


### 9.2.3 配置内核


接下来就是根据你的需求来进行内核的配置了。这个具体的过程和你目标有关。


内核的构建依赖于一个内核配置文件。在大多数情况下,和其他所有 Linux 发行版本一样,内核文件会被安装到 /boot 目录托尔斯泰。在本例中,我们不必从头开始配置所有内容,拷贝一份当前系统的配置( /boot/config- version)更为快捷(此处的版本号应该和当前运行的内核版本一致,可以通过 uname -r 查看)。可以直接将文件放置在包含内核源码的目录下,保存为 .conf 。


同样,因为内核源码在 arch/ arch /configs/*_defconfig 里也提供了默认配置,你也可以通过 make x8664defconfig 或 make bcm2835defconfig ( Raspberry Pi Zero W )来将默认配置放置就位。


Black13eard@kali:/usr/src/linux-source-4.17$ sudo make bcm2835_defconfig

YACC    scripts/kconfig/zconf.tab.c

LEX     scripts/kconfig/zconf.lex.c

HOSTCC  scripts/kconfig/zconf.tab.o

In file included from scripts/kconfig/zconf.tab.c:2485:

scripts/kconfig/confdata.c: In function 'conf_write':

scripts/kconfig/confdata.c:773:22: warning: '%s' directive writing likely 7 or more bytes into a region of size between 1 and 4097 [-Wformat-overflow=]

sprintf(newname, "%s%s", dirname, basename);

^~

scripts/kconfig/confdata.c:773:19: note: assuming directive output of 7 bytes

sprintf(newname, "%s%s", dirname, basename);

^~~~~~

scripts/kconfig/confdata.c:773:2: note: 'sprintf' output 1 or more bytes (assuming 4104) into a destination of size 4097

sprintf(newname, "%s%s", dirname, basename);

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

scripts/kconfig/confdata.c:776:23: warning: '.tmpconfig.' directive writing 11 bytes into a region of size between 1 and 4097 [-Wformat-overflow=]

sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());

^~~~~~~~~~~

scripts/kconfig/confdata.c:776:3: note: 'sprintf' output between 13 and 4119 bytes into a destination of size 4097

sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

HOSTLD  scripts/kconfig/conf

#

# configuration written to .config

#

Black13eard@kali:/usr/src/linux-source-4.17$

Black13eard@kali:/usr/src/linux-source-4.17$ ls -a

.   .clang-format  .get_maintainer.ignore  .mailmap  Documentation  LICENSESREADMEcerts firmware  init    libsamples   sound  virt

..   .cocciconfig   .gitattributes  COPYING   Kbuild   MAINTAINERSarchcrypto fs   ipc   mmscripts   tools

.cache.mk  .config  .gitignore  CREDITS   Kconfig   Makefileblockdrivers  include   kernel  netsecurity  usr

Black13eard@kali:/usr/src/linux-source-4.17$


除非你需要更改内核配置,否则你就可以就此打住直接跳到下一节进行编译了。如果你需要做一些修改或全部重新配置,那你就必须要花点时间来配置内核了。在内核的源码路下有很多特定的配置接口,可以通过 make target 来进行调用(当前也要在源码目录下 linux-source-version )。


make menuconfig 会启动文本模式的内核配置界面(也就是这里需要 libncurses5-dev 软件包),在这里可以通过结构化的目录来对各种配置进行导航。提供按“空格”键来改变选择状态;“回车”键是选中并确认;Select 进入子菜单;Exit 和按两次 Esc 键都是返回上一层菜单;Help 则会显示关于当前所选内容的详细信息。方向键可在列表选项和按钮之间切换。要想退出配置程序,选择主菜单的 Exit ,程序会提示是否保存当前的修改,如果你对本次的修改满意则可以进行保存。


其他的接口也都是类似的功能,但是使用的可能是更现代一点的图形界面,比如,make xconfig 使用的是 Qt 图形界面,make gconfig 使用的是 GTK+ 。前者需要安装 libqt4-dev,后者则依赖于 libglade2-dev 和 libgtk2.0-dev 。


处理旧的 .config 文件

---------------------------------------

当你提供由其他内核版本(通常是老版本)生成的 .config 文件的时候,你需要去更新它的一些内容。你可以通过 make oldconfig 来进行更新,它会就新配置选项的一些内容进行交互提问。如果你想对所有问题都使用默认答案,你可以使用 make olddefconfig。如果使用 make oldnoconfig 则所有的问题都使用否定答案。



### 9.2.4 编译和构建包


构建之前的环境清理

---------------------------------------

如果你之前已经在这个目录下编译过内核,并且想从头开始重新编译(比如,上次你改了非常多的内核配置,不想一一改回来)的话,就需要运行 make clean 来清除已编译的文件。make distclean 甚至会移除已生成的文件,包括 .config 文件,所以如果运行此命令,记得要先备份相关文件。


一旦内核配置完成,只要一个简单的命令 make deb-pkg 就可以生成五个标准的 Debian .deb 格式的软件包:


  • linux-image-version - 包含内核映像和关联的模块文件;

  • linux-headers-version - 包含了构建外部模块所需要的头文件;

  • linux-firmware-image-version - 包含了某些驱动需要的固件文件(当使用从 Debian 或 Kali 获取的内核源码来构建内核的时候,这个软件包可能不会生成);

  • linux-image-version-dbg - 包含了内核映像和模块的调试符号;

  • linux-libc-dev - 包含了和某些用户空间库(比如,GNU 的 C 库 - glibc )相关的头文件;


version 是由⼀一系列列的因素决定的,包括上游版本(由 Makefile ⽂文件⾥里里的 VERSION、PATCH-LEVEL、SUBLEVEL 和EXTRAVERSION 变量量定义)、LOCALVERSION 配置参数以及 LOCALVERSI-ON 的环境变量量。内核软件包的版本会使⽤用以上相同的版本字符串串,并且添加⼀一个⾃自增⻓长的数字(存储在 ../kernel/.version ),你也可以通过 KDEB_PKGVERSION 环境变量量来覆盖它。


Black13eard@kali:/usr/src/linux-source-4.17$ sudo make deb-pkg LOCALVERSION=-black13eard KDEB_PKGVERSION=$(make kernelversion)-3


scripts/kconfig/conf  --syncconfig Kconfig

CHK     include/config/kernel.release

UPD     include/config/kernel.release

make clean

/bin/bash ./scripts/package/mkdebian

TAR     linux-4.17.17-black13eard.tar.gz

[...]

dpkg-deb: building package 'linux-headers-4.17.17-black13eard' in '../linux-headers-4.17.17-black13eard_4.17.17-3_armhf.deb'.

dpkg-deb: building package 'linux-libc-dev' in '../linux-libc-dev_4.17.17-3_armhf.deb'.

dpkg-deb: building package 'linux-image-4.17.17-black13eard' in '../linux-image-4.17.17-black13eard_4.17.17-3_armhf.deb'.

dpkg-deb: building package 'linux-image-4.17.17-black13eard-dbg' in '../linux-image-4.17.17-black13eard-dbg_4.17.17-3_armhf.deb'.

dpkg-genbuildinfo

dpkg-genchanges  >../linux-4.17.17-black13eard_4.17.17-3_armhf.changes

[...]

Black13eard@kali:/usr/src/linux-source-4.17$

Black13eard@kali:/usr/src/linux-source-4.17$ ls -l ../

total 319028

drwxr-xr-x 26 root root        4096 Sep  7 15:43 kernel

-rw-r--r--  1 root root      213322 Sep  2 06:15 linux-4.17.17-black13eard_4.17.17-3.diff.gz

-rw-r--r--  1 root root        1213 Sep  2 06:16 linux-4.17.17-black13eard_4.17.17-3.dsc

-rw-r--r--  1 root root        4991 Sep  2 14:26 linux-4.17.17-black13eard_4.17.17-3_armhf.buildinfo

-rw-r--r--  1 root root        3433 Sep  2 14:26 linux-4.17.17-black13eard_4.17.17-3_armhf.changes

-rw-r--r--  1 root root   163481719 Sep  2 06:02 linux-4.17.17-black13eard_4.17.17.orig.tar.gz

drwxr-xr-x  2 root root        4096 Sep  5 12:22 linux-config-4.17

-rw-r--r--  1 root root    10451724 Sep  2 14:01 linux-headers-4.17.17-black13eard_4.17.17-3_armhf.deb

-rw-r--r--  1 root root    40910988 Sep  2 14:25 linux-image-4.17.17-black13eard-dbg_4.17.17-3_armhf.deb

-rw-r--r--  1 root root     5571072 Sep  2 14:03 linux-image-4.17.17-black13eard_4.17.17-3_armhf.deb

-rw-r--r--  1 root root      962460 Sep  2 14:02 linux-libc-dev_4.17.17-3_armhf.deb

drwxr-xr-x 27  999 nobody      4096 Sep 11 02:01 linux-source-4.17

-rw-r--r--  1 root root   105035692 Aug 21 08:33 linux-source-4.17.tar.xz

drwxr-xr-x  5 root root        4096 Dec 15  2017 volatility-tools


要使用新构建的这个内核,还差最后一步:使用 dpkg -i file.deb 来安装必要的软件包。“linux-image” 是其中一个。另外如果你有一些外部的内核模块需要构建,还要安装 “linux-headers” ,此时你应该已经安装了一些 *-dkms 包(可以通过 dpkg -l “*-dkms” | grep ^ii 来查看)。其他的软件包一般情况下是不需要安装了(除非你知道为什么需要)。



关于《Kali Linux 解密》

---------------------------------------

不管你是信息安全的新手,还是安全老手,这本 Kali Linux 官方出品 的指导手册都可以给你一些惊喜。古语云“柢固则生长,根深则视久”,这句话也适用于信息安全领域。如果你想要拥有使用 Kali Linux 的专家级能力,你必须熟知各种渗透测试姿势的内在原理。而这就是本书所要向你讲述的内容 —- 如何让你成为一个 Kali Linux 资深玩家。


为了能够让大家边学习边实践,达到一起读书的目的,公众号将尽量保持每周一到两篇的速度更新,欢迎大家持续关注。



破阵子

醉里挑灯看剑

梦回吹角连营

八百里分麾下炙

五十弦翻塞外声











猎客

·

公元




公众号:NetHunterPark

关注移动安全及渗透测试

文章转载自NetHunterPark,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论