Github GPG签名

Author Avatar
Amos
发表:2022-01-30 10:39:20
修改:2022-02-02 23:41:10

就在2022年1月26日,我看见圈子内有人说 Linux 之父 Torvalds 要删除 Linux,表示它很糟糕,并且非常诙谐地建议你使用 Windows XP ……

哇哦!这简直是非常大的新闻了(这引起了数万开发者的关注)。

我赶紧通过事发的链接(原文链接)进行查看,原文如下:

hey guys its me linus torvalds, author of the smash hit linux. yes its me you
can look at the url of the repo and the thingy at the top of the files it
proves its 100% me.

i deleted linux because i hate it now i think it sucks. you should go use this
awesome os its called windows xp i just discovered it its great

这样的感觉很熟悉,因为我记得在很久之前也有人通过 Github 的漏洞伪造了作者信息进行恶意提交,误导其他开发者,这样的例子还不算少,我抱着警惕的心理,将文章继续向下翻了翻。

果不其然!这依旧是一起伪造作者提交的事件。

这位假 “Torvalds” 在最后留了一篇讨论的链接:

https://news.ycombinator.com/item?id=24976138

打开链接我们可以看见,这是在2020年11月3日发起的一篇针对伪造提交的讨论,实际上类似事件可以追溯到更早之前。

不得不说,这位假 “Torvalds” 对该事件的宣传方式真的起效了(大家全上钩了哈哈哈哈哈哈)。

讨论内容大致就是说:其他人可能还没有意识到,通过 Github 别人可以伪造我的信息进行提交,看起来就是我提交的一样,因为大多数人没有使用 GPG 进行提交,并且大多数人也找不到“已验证”作者的标识,特别容易被恶意的人钓鱼。

还提供了一个演示:https://github.com/slimsag/linux/tree/5895e21f3c744ed9829e3afe9691e3eb1b1932ae#linux-kernel

在这篇讨论中,最重要的信息就是如何查看 “已验证”作者标识GPG签名

Others probably have not fully realized this yet, but with GitHub one can:
1) Publish arbitrary commits under your https://github.com/my/project URL, e.g. a fake https://github.com/my/project/blob//README.md in your project describing how to install it that actually describes installing malware.

2) Publish those commits under your name, with your email address, and GitHub will prominently display it as if you made the commit (most do not use GPG signatures, and most do not know to look for “Verified” anyway)

It seemed only a matter of time before this behavior got abused for something (anti-DMCA action is perhaps the best outcome of this situation I can imagine..)

查看“已验证”作者标识

在 Github 提交历史中可以直接查看该提交的 “Verified” 已验证标识,点击“Verified”标识可以查看真实的提交用户。
未标识 “Verified” 的提交,就有可能不是作者本人,需要警惕内容的真实性。


Verified标识

Verified标识

GPG签名

GPG的作用非常多,但这次主要是用来对 Git 提交进行签名验证,所以我们要做的事情就非常简单了。

由于此处仅演示 Windows 的生成方式,我仅作为快速演示,详细教程还请查看官方教程。
Github 官方 GPG 签名教程:https://docs.github.com/articles/generating-a-gpg-key

1、生成 GPG 密钥

我目前使用的环境是 Windows 系统以及 Git 自带的 GPG,我们可以打开 Git Bash 或 终端 输入 gpg --version 查看版本,在 Git 低版本下可能无法使用 gpg,建议升级 Git 版本。
也可以下载 GPG 命令行工具

$ gpg --version
gpg (GnuPG) 2.2.20-unknown
libgcrypt 1.8.5
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Home: /c/Users/xxxx/.gnupg
Supported algorithms:
Pubkey: RSA, ELG, DSA, ECDH, ECDSA, EDDSA
Cipher: IDEA, 3DES, CAST5, BLOWFISH, AES, AES192, AES256, TWOFISH,
        CAMELLIA128, CAMELLIA192, CAMELLIA256
Hash: SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224
Compression: Uncompressed, ZIP, ZLIB, BZIP2

确认 GPG 没问题后。
打开 Git Bash 或 终端 输入:

$ gpg --full-generate-key

提示选择需要生成的密钥类型,我们直接回车选择默认即可。

$ gpg --full-generate-key
gpg (GnuPG) 2.2.20-unknown; Copyright (C) 2020 Free Software Foundation, Inc.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA (default) ------直接选择它
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
  (14) Existing key from card
Your selection?

提示指定密钥大小,我们需要密钥大小至少为4096。

RSA keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 4096 ------此处直接输入4096

提示指定密钥的有效时间,此处按照自己的需求进行设置,按照提示输入,比如:不限时间输入 0 、5年输入 5y 、5周输入 5w 等……

Please specify how long the key should be valid.
         0 = key does not expire
      <n>  = key expires in n days
      <n>w = key expires in n weeks
      <n>m = key expires in n months
      <n>y = key expires in n years
Key is valid for? (0) 5y ------此处按照自己的需求和提示设置密钥有效时间
Key expires at xxxx年xx月 xx日 周x xx:xx:xx
Is this correct? (y/N) y ------确认上方有效时间

提示输入用户信息,需要注意的是Email邮箱是需要输入Github已经验证过的邮箱。

GnuPG needs to construct a user ID to identify your key.

Real name: 你的名字
Email address: 登录Github的邮箱(已验证)
Comment: 自定义信息,可以不填
You selected this USER-ID:
    "名字 <邮箱>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit?
------修改输入 (N)姓名 (C)自定义信息 (E)邮箱
------在确认无误后输入 O 继续下一步,

密码保护私钥

紧接着需要输入一个密码保护私钥,在输入两次后即可完成 GPG 签名的生成。

We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: /c/Users/xxxx/.gnupg/trustdb.gpg: trustdb created
gpg: key 5BExxxxxEAA marked as ultimately trusted
gpg: directory '/c/Users/xxxx/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/c/Users/xxxx/.gnupg/openpgp-revocs.d/C06EF83CxxxxxxxxxxxxxEB186516808EAA.rev'
public and secret key created and signed.

pub   rsa4096 xxxx-xx-xx [SC] [expires: xxxx-xx-xx]
      C06EF83CxxxxxxxxxxxxxEB186516808EAA
uid                      名称 <邮箱>
sub   rsa4096 xxxx-xx-xx [E] [expires: xxxx-xx-xx]

2、查询 GPG 签名

打开 Git Bash 或 终端 输入:

$ gpg --list-secret-keys --keyid-format=long

查询结果如下:

$ gpg --list-secret-keys --keyid-format=long
gpg: checking the trustdb
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
gpg: next trustdb check due at xxxx-xx-xx
/c/Users/Amos/.gnupg/pubring.kbx
--------------------------------
sec   rsa4096/5BEB18xxxxxx8EAA xxxx-xx-xx [SC] [expires: xxxx-xx-xx]
      C06EF83C0655DBBEB95B49195BEB186516808EAA
uid                 [ultimate] 名字 <邮箱>
ssb   rsa4096/DA1C2xxxxxxA09CC xxxx-xx-xx [E] [expires: xxxx-xx-xx]

此处我们需要注意 sec rsa4096/5BEB18xxxxxx8EAA5BEB18xxxxxx8EAA 字符串。

接下来我们通过 GPG 密钥的 ID 5BEB18xxxxxx8EAA 查询 GPG 签名密钥,输入:

$ gpg --armor --export 5BEB18xxxxxx8EAA

复制 GPG 密钥,从 -----BEGIN PGP PUBLIC KEY BLOCK----- 开始,到 -----END PGP PUBLIC KEY BLOCK----- 结束(包括这两段分界)。

$ gpg --armor --export 5BEB18xxxxxx8EAA
-----BEGIN PGP PUBLIC KEY BLOCK-----

mQINBGH6j7MBEADcWe1iI5BOhaVRIW72knKBjj2ReosIpIrmA+Zhi9c2I8K/Z3U+
+njq1mbkQAHtpES4TNnHAIMK2WYIzSV8qSwlsbvLLgK5StGKTjbmpVyHMJWsTQ8g
ttTRg0sGChaQSxNYi5WPmZqEwXeV27lJ3sEdwOSpTP8A+D95Iyng5mbD2pZv57rw
...............
ur+TKImEJzz8BF1PMeoFKBu2M4OBfAeLgi5ODQH0Q9nrKfSomlU+oGuEakxOK9f0
MZyZtFXv/Ffn3Nx9LZ5VRxLB1PgXk+7UwPzGuVjrCVVfmuhpFQekIkzHyjlPVh1K
rZA=
=hsUy
-----END PGP PUBLIC KEY BLOCK-----

3、将 GPG 绑定 Github

提交 Github 的 GPG 签名,位于右上角个人设置SSH and GPG keys处。

SSH and GPG keyshttps://github.com/settings/keys

添加 GPG 并粘贴刚才复制的 GPG 密钥内容。

添加 GPG 位置:https://github.com/settings/gpg/new

密码保护私钥

添加完成后,即为绑定成功。

4、将 GPG 绑定 Git

根据第2步查询得知的 GPG 密钥 ID 5BEB18xxxxxx8EAA,告知绑定 Git,输入:

$ git config --global user.signingkey 5BEB18xxxxxx8EAA

5、项目使用 GPG 签名提交

在之后我们对项目有提交时,只需要多加入 -S 即可使用 GPG 签名提交。

$ git commit -S -m "本次提交的描述"

在提交后,我们就拥有了 “Verified” 标识~~

注意:GitHub Desktop 不支持签名提交
如果要将 Git 客户端配置为默认对本地仓库的提交签名,请在 Git 版本 2.0.0 及更高版本中,运行 git config commit.gpgsign true
要在计算机上的任何本地仓库中默认对所有提交签名,请运行 git config --global commit.gpgsign true

总结

这次 “惊喜” 也让我们在今后注意不被恶意的人所引导,虽然自己之前也吃瓜过真实恶性的伪造事件,但长时间下来自己也就淡忘了,使用 GPG 签名也仅仅是目前保护自己保护他人的手段,更重要的还是需要对事件保持警惕,保有自己的观点和想法。

参考

[1] Github Verify commit signatures

转载请遵循 协议许可
本文所有内容严禁任何形式的盗用
本文作者:Amos Amos
本文链接:https://amoshk.top/2022013001/

评论
✒️ 支持 Markdown 格式
🖼️ 头像与邮箱绑定 Gravatar 服务
📬 邮箱会回复提醒(也许会在垃圾箱内)