编译内核源代码时,您可以选择使用选项对内核模块进行签名CONFIG_MODULE_SIG*
。该modinfo
工具应该处理验证模块签名的任务,但多年来它一直存在一些错误,该工具根本无法再完成这项工作。我得到的只是以下内容:
sig_id: PKCS#7
signer:
sig_key:
sig_hashalgo: md4
signature: 30:82:02:F4:06:09:2A:86:48:86:F7:0D:01:07:02:A0:82:02:E5:30:
...
所以没有密钥,哈希算法是 md4,它甚至没有在内核中编译。
那么如何手动检查和验证模块签名呢?这可能吗?
答案1
是的,这是可能的,但它非常复杂。
首先你必须提取模块签名——你可以使用extract-module.sig.pl
来自内核源代码的脚本:
$ scripts/extract-module-sig.pl -s MODULE.ko >/tmp/modsig.
Read 789006 bytes from module file
Found magic number at 789006
Found PKCS#7/CMS encapsulation
Found 670 bytes of signature [3082029a06092a864886f70d010702a0]
其次,您必须从内核中提取证书和公钥;你可以使用extract-sys-certs.pl
脚本:
$ scripts/extract-sys-certs.pl /PATH/TO/vmlinux /tmp/cert.x509
Have 32 sections
Have 28167 symbols
Have 1346 bytes of certs at VMA 0xffffffff81be6db8
Certificate list in section .init.data
Certificate list at file offset 0xde6db8
$ openssl x509 -pubkey -noout -inform der -in /tmp/cert.x509 -out /tmp/pubkey
您还可以从 Linux 内核的构建目录中的certs/signing_key.x509
或文件中提取公钥。certs/signing_key.pem
完成此操作后,您就拥有了所需的所有数据/tmp/modsig
,并且/tmp/cert.x509
可以继续执行验证 PKCS#7 签名所需的十几个步骤。
你可以看看这个博客邮政整个食谱。
我试图将整个过程(除了该extract-certs.pl
步骤)放在一个 perl 脚本中。
你可以这样使用它:
perl checkmodsig.pl /path/to/cert.x509 mod1.ko mod2.ko ...
青年MMV。我只尝试过使用 sha512 签名的自定义构建内核。当然,最好直接使用 openssl 库来完成,而不是将缓慢且脆弱的调用混在一起openssl x509
。asn1parse
rsautl
checkmodsig.pl
use strict;
sub through {
my ($cmd, $data, $cb) = @_;
use IPC::Open2;
my $pid = open2 my $from, my $to, ref $cmd ? @$cmd : $cmd;
print $to $data; close $to; my $out;
if($cb){ while(<$from>){ last if $out = $cb->($_) } }
else { local $/; $out = <$from>; }
waitpid ($pid, 0);
die "status $?" if $? != 0;
$out;
}
sub gethash {
my ($d) = @_; my ($alg, $hash);
through [qw(openssl asn1parse -inform der)], $d, sub {
if(/(\d+):d=\d+ +hl= *(\d+) +l= *(\d+) +prim: +OCTET STRING/){
$hash = substr $d, $1 + $2, $3
}elsif(/prim: +OBJECT +:(sha\w+)/){
$alg = $1;
}
undef
};
$alg, $hash
}
use File::Temp;
my $tf = new File::Temp;
my $pub_key;
my @type = qw(PGP X509 PKCS7);
my $r = 0;
if((my $cert = shift) =~ /(\.x509)$|\.pem$/i){
$pub_key = $tf->filename;
system qw(openssl x509 -pubkey -noout),
'-inform', $1 ? 'der' : 'pem',
'-in', $cert, '-out', $pub_key;
die "status $?" if $? != 0;
}
die "no certificate/key file" unless $pub_key;
for my $kof (@ARGV){
open my $ko, '<', $kof or die "open $kof: $!\n";
seek $ko, -4096, 2 or die "seek: $!";
read $ko, my $d, 4096 or die "read: $!";
my ($algo, $hash, $type, $signer_len, $key_id_len, $sig_len, $magic) =
unpack 'C5x3Na*', substr $d, -40;
die "no signature in $kof"
unless $magic eq "~Module signature appended~\n";
die "this script only knows about PKCS#7 signatures"
unless $type[$type] eq 'PKCS7';
my $hash = gethash substr $d, - 40 - $sig_len, $sig_len;
die "hash not found" unless $hash;
my ($alg, $vhash) = gethash
through [qw(openssl rsautl -verify -pubin -inkey), $pub_key],
$hash;
seek $ko, 0, 0 or die "seek: $!";
read $ko, my $d, (-s $ko) - $sig_len - 40 or die "read: $!";
use Digest::SHA;
my $fhash = new Digest::SHA($alg)->add($d)->digest;
if($fhash eq $vhash){
print "OK $kof\n";
}else{
print "**FAIL** $kof\n";
$r = 1;
warn 'orig=', unpack('H*', $vhash), "\n";
warn 'file=', unpack('H*', $fhash), "\n";
}
}
exit $r;