[开源小工具] gpg 靓号生成脚本

2021-05-29 16:18:00 +08:00
 err1y

仓库地址:gpg_awesome_keyid

我的 keyid:0x699a698e77777777

原理

根据 rfc4880 的 pgp keyid 的计算方式我们可以得知,修改 key 的创建时间我们即可以达到快速修改 keyid 的目的。

12.2.  Key IDs and Fingerprints
...
   a.3) low-order length octet of (b)-(e) (1 octet)
     b) version number = 4 (1 octet);
     c) timestamp of key creation (4 octets);
...

使用如下代码进行快速计算 key

// src/actions.js@do_generate
let prikey = null;
let start = new Date();

for(let index = 0; ; index++) {
    if(0 === index % 10000) {
        // 每 1 万次之后重新生成 key,防止创建时间过于小
        prikey = await gen_basic_key();
        // ...
    }
    // 修改 key 的创建时间并重新计算指纹(时间精度为秒)
    // ! 需要注意的是,此时的 key 的 subkey 和 uid 的签名都是需要修正的,是原始指纹的 key 的签名
    prikey.keyPacket.created = new Date(prikey.keyPacket.created.getTime() - 1000);
    let r = {
        // 因为修改了创建时间但是并未重新计算 private key 的指纹,但获取公钥时会重新计算公钥指纹,达到计算指纹的目的
        fpr: prikey.toPublic().getFingerprint(),
        // key 需要修正,加密子钥和 uid 的签名都是使用的旧 id 进行签名的,import 过程通过 openpgp.reformat 修正
        key: prikey.armor()
    };
    // ...
}

计算速度

测试环境

计算速度

> node . start
...
[.../gpg_awesome_keyid/dbs/key2.db] 耗时 57.582 秒,已计算 key250000 个,数据库中已保存 6 个 key
[.../gpg_awesome_keyid/dbs/key3.db] 耗时 58.054 秒,已计算 key250000 个,数据库中已保存 6 个 key
[.../gpg_awesome_keyid/dbs/key1.db] 耗时 58.213 秒,已计算 key250000 个,数据库中已保存 8 个 key
[.../gpg_awesome_keyid/dbs/key0.db] 耗时 58.85 秒,已计算 key250000 个,数据库中已保存 5 个 key

预计 8 连 keyid 时间

每分钟 100w 数据

任意 8 连概率大约 0xFFFFFFFF/16 ( 16 种可能)

通过计算:0xFFFFFFFF / 16 / 1000000 / 60 ≈ 4.47 小时

使用

安装

git clone https://github.com/erriy/gpg_awesome_keyid.git \
&& cd gpg_awesome_keyid \
&& npm i

自定义靓号规则

// src/rule.js 文件中
// notify 和 save 函数任意一个返回 true 则保存,否则直接丢弃

/**
 * 返回 true 则保存数据库后并发送通知
 */
function notify (fingerprint) {
    // 默认提醒后八位相同的 key
    return 1 === new Set(fingerprint.slice(32)).size;
}

/**
 * 返回 true 则保存到数据库
 */
function save (fingerprint) {
    const special_list = [
        '01234567',
        '76543210',
        'abcdef',
        '01020304',
        '11223344',
    ];

    if(
        new Set(fingerprint.slice(35)).size === 1
        || new Set(fingerprint.slice(32)).size <= 2
        || new Set(fingerprint.slice(24)).size <= 3
        || new Set(fingerprint).size <= 4
    ) {
        return true;
    }

    for(let s of special_list) {
        if(fingerprint.endsWith(s)){
            return true;
        }
    }

    return false;
}


修改 gpg 使用的算法

// src/action.js@gen_basic_key
// 推荐使用 ecc 算法,生成的公钥更小且计算速度更快
async function gen_basic_key () {
    const { privateKeyArmored } = await openpgp.generateKey({
        // 如果要修改算法,请自己去 https://github.com/openpgpjs/openpgpjs#performance 里面找
        type   : 'ecc',
        curve  : 'curve25519',
        userIDs: [{name: 't'}] // userid 在导入时会重新生成,此处修改没有意义
    });
    return await openpgp.readKey({ armoredKey: privateKeyArmored });
}

开始算号

# 直接开始算号
node . start
# 开始算号,发现帐号后发送 bark 通知
node . start --barkid xxxxxx

查找保存的数据

# 列出所有数据
node . list 
# 列出指定后缀的数据
# node . list [suffix]
# 比如列出以 fffff 结尾的 key
node . list fffff

保存结果

注意:导入的 key 并未设置密码,请自行使用命令设置密码,数据库中存在明文 key,记得删除,防止泄漏

# 保存以 fffff 结尾的 key
node . import fffff
# 保存以 fffff 结尾的 key 并指定 uid.name 和 uid.email
node . import fffff -n name -email test@test.com

其他更快速的方法

相关资料

3218 次点击
所在节点    开源软件
8 条回复
d5
2021-05-29 16:58:28 +08:00
酷,有意思,谢谢楼主开源👍
dallaslu
2021-05-30 09:52:49 +08:00
err1y
2021-05-30 13:05:05 +08:00
@dallaslu 其实用 opencl/cuda 等库调用 gpu 很快,我这个太慢了,回头学学 opencl

这个快,调 gpu 的
https://github.com/lachesis/scallion
chinni
2021-12-06 22:44:39 +08:00
@err1y openpgp.reformat 增加 uid 后再给 gpg 使用 这个有什么详细的操作方法么 ?
我找了下 没特别明白
err1y
2021-12-08 11:24:06 +08:00
@chinni
https://github.com/openpgpjs/openpgpjs/blob/main/src/openpgp.js#L100-L121
不一定是最优方法,你可以去看看 scallion ,之前我在测试的时候并没有找到指定 uid 的参数,使用 scallion 生成出来的 key 是没有 uid 的,然后导致 gpg 无法导入,我选择使用 openpgpjs 的 reformatKey 方法增加的 uid

大概需要使用 node 去这么做,你可以研究下(未测试,之前写这帖子的时候是测试成功的)

``` javascript
const openpgp = require('openpgp');

const key = `
-----BEGIN PGP PRIVATE KEY BLOCK-----
... 此处是 scallion 生成的 key
-----END PGP PRIVATE KEY BLOCK-----
`;


(async()=>{
const {key: new_key, privateKeyArmored: prikey_armord} = await openpgp.reformatKey({
privateKey: await openpgp.readKey({armoredKey: key}),
// 这里填写你要给 key 增加的 uid
userIDs: [{name: 'test', email: 'test@test.com', comment: ''}]
});
console.log(prikey_armord, new_key.getUserIDs());
})();
```
chinni
2021-12-08 11:33:23 +08:00
@err1y 好的多谢大佬 后面我找到了一个 gpg import 导入的参数 --allow-non-selfsigned-uid 可以直接导入 不合法的(就是 email 格式不正确的 UID 的 key 。然后 导入了。 可以解决这个问题
wc7086
2022-05-22 15:10:57 +08:00
这可不兴靓号啊,这样搞别人不是随随便便就能把自己的密钥给穷举出来了。
wc7086
2022-05-22 15:34:20 +08:00
> 这可不兴靓号啊,这样搞别人不是随随便便就能把自己的密钥给穷举出来了。
一时糊涂搞错了,不过靓号放在公共密钥服务器上还挺容易被攻击的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/780038

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX