编译内核添加socketcan支持(其他内核编译也同理)
最近玩到车联网安全,其实虚拟机已经完全够了,但是我想着在自己主机能更加方便,就搞了个wsl下kali的子系统,但是wsl下的kali没有socketcan支持,就要重新编译内核添加支持
更新apt包
sudo apt update -y && sudo apt upgrade -y
把编译所需要的库装好
sudo apt-get install -y autoconf bison build-essential flex libelf-dev libncurses-dev libssl-dev libtool libudev-dev bc
使用uname -r
查看内核版本
然后使用wget下载对应的版本内核包
sudo wget https://github.com/microsoft/WSL2-Linux-Kernel/archive/refs/tags/linux-msft-wsl-5.15.90.1.tar.gz
使用tar解压缩
tar -zxf linux-msft-wsl-5.15.90.1.tar.gz
cd
进入目录后,使用
sudo make menuconfig KCONFIG_CONFIG=Microsoft/config-wsl
其中KCONFIG_CONFIG=Microsoft/config-wsl
是包里面自带的.config文件,实际上使用自动生成的.config文件也可以,但是还是使用包里面的靠谱。
然后选择Networking support->CAN BUS subsystem support开启所需要的相关CAN模块,保存的时候就会自动保存在Microsoft/config-wsl
中。然后后续使用编译的时候加多一个KCONFIG_CONFIG=Microsoft/config-wsl
就行。
接下来就是枯燥的make
环节
sudo make prepare modules_prepare -j $(expr $(nproc) - 1) KCONFIG_CONFIG=Microsoft/config-wsl
sudo make modules -j $(expr $(nproc) - 1) KCONFIG_CONFIG=Microsoft/config-wsl
sudo make modules -j $(expr $(nproc) - 1) KCONFIG_CONFIG=Microsoft/config-wsl
sudo make install KCONFIG_CONFIG=Microsoft/config-wsl
复制vmlinux
到本机Users目录下,这个目录可变,只要后面的.wslconfig里的路径kernel指向目录就行
# copy it to windows drive
cp vmlinux /mnt/c/Users/<yourwindowsloginname>/
创建.wslconfig
#create this file, change /<yourwindowsloginname> to your windows users directory name
vi /mnt/c/Users/<yourwindowsloginname>/.wslconfig
填上刚刚vmlinux移动的路径
#with these contents
[wsl2]
kernel=C:\\Users\\<yourwindowsloginname>\\vmlinux
保存后退出wsl
exit
退出后使用shutdown停止再启动
wsl --shutdown
wsl
然后就可以使用socketcan模块了~
sudo modprobe can
sudo modprobe vcan
sudo modprobe can_raw
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0