飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 2918|回复: 6

[原创] .Net逆向基础:与没有引用其它程序集的强名称程序进行对抗

[复制链接]
  • TA的每日心情
    擦汗
    2024-3-20 09:24
  • 签到天数: 373 天

    [LV.9]以坛为家II

    发表于 2022-2-2 23:52:50 | 显示全部楼层 |阅读模式
    本帖最后由 763837023 于 2022-2-3 00:01 编辑

    原作者:CodeCracker

    翻译及补充:慕若曦

    工具:

    在本节课中,CodeCracker 为我们介绍了对有强名称保护的程序如何进行分析,本文针对 CodeCracker 提供的 main.exe 作为案例,为了方便新手学习,我补充了部分配图,并对部分令新手较难上手的操作补充了图文说明,但遗憾的是我并不是专业的程序员,所以部分术语翻译可能有误,如果您有更好的术语翻译,欢迎告知我。


    怎样判断程序集是否被签名(强名称)
    • CFF Explorer中加载要分析的程序
    • 转到.NET Directory -> MetaData Streams -> #~ -> MetaData Tables (或者Tables) -> Assembly
    • 选中要分析的程序集(图中只有一个程序集DemoApplication
    • 观察右侧的PublicKey的值,如果为0,证明这个程序集没有被签名
    微信截图_20220202174643.png

    如果 PublicKey 的值不为 0,说明程序集是受到强名称保护的。

    此时这个值应该是 Blob 的索引号,我们需要复制它,然后进入#Blob部分,在右键菜单里选择Go to Offset(部分汉化版的 CFF 翻译为跳转偏移),即可跳转到对应的PublicKey

    QQ截图20220202182115.png

    需要注意的是,这里跳转到的是 PublicKey,而不是PublicKeyToken,这是两个不同的属性!

    对抗强名称保护的方法方法 1:对程序集重新签名

    步骤:

    • 使用sn.exe来创建密钥对(key pair),对应的命令为:sn –k keypair.snk
    • 使用RE-Sign工具,用生成的秘钥对对程序集进行签名

    为让新手更容易的上手,方法 1 的流程已经帮你写好了批处理,在RE-Sign-sn工具包里你能找到一个名为genkeypair.bat的批处理文件,运行它即可得到两个文件,keypair.snk是用来给程序签名的秘钥文件,对应的token存放在newpktoken.txt这个文档中,然后使用RE-Sign进行签名即可。

    需要强调的是,RE-Sign这个工具过于古老而且没有人维护,在 Win10 上运行会出现部分 Bug,比如界面异常,我在翻译这篇教程的时候尝试修复了它,重命名为RE-Sign_Fix.exe,你可以根据需要去选择使用。

    20220202193548.png
    方法 2:使用 CFF Explorer 自动删除强名称保护

    CFF Explorer中加载被强名称保护的文件,在Rebuilder 选项里,我们只勾选Remove Strong Name Signature,然后点击Rebuild即可。

    微信截图_20220202204713.png
    方法 3:使用 CFF Explorer 手动删除强名称保护

    这个方法是方法 2 的手动实现,具体的步骤如下:

    • CFF Explorer中加载被强名称保护的文件

    • 在.NET Directory中有一个Flag标志,这就是 dotNet 的CLI Header Assembly flags,点击我箭头标注的位置,取消对Strong name signed的勾选


    微信图片_20220202230103.png
    • .NET Directory中,有StrongNameSignature RVAStrongNameSignature Size两个标记,将它们的值设置为0,小提示,双击对应的值去修改,八位数值
    微信截图_20220202230909.png
    • 转到.NET Directory -> MetaData Streams -> #~ -> MetaData Tables(或者Tables) -> Assembly,把PublicKey设置为 0000

    • 然后在Flag里去掉对PublicKey的勾选

      微信图片_20220202231549.png

    感谢

    致谢:原作者 CodeCracker

    附原文内容以供参考:

    Defeat strong name protection from assemblies which doesn’t have any reference from other assemblies (the main exe for example)

    How can I see if an assembly is signed?

    Load the desired assembly under CFF Explorer and go at .NET Directory->MetaData Streams->#~->MetaData Tables (Tables)->Assembly and select the current assembly.

    You will see a member named PublicKey. If has the value 0 means that our assembly is not strong named!

    If is different from 0 it means that is strong named and the value will be an Index under Blob. We simply copy the value and we go under “#Blob” section. We choose “Go to Offset”, we enter the copied value and you will lead to the PublicKey (not the PublicKeyToken - take care!).

    We have some options for defeating strong name protection:

    Option A: Resign the assembly whit our own key using re-sign tools:

    Steps:

    • Use sn.exe to create a key pair using the command:

      sn –k keypair.snk
    • Use RE-Sign tools to resign the assembly whit the generate snk file keypair.snk


    Option B: Automatic Strong Name Protection Remove using CFF Explorer:

    Load the file under CFF Explorer VII, we go to Rebuilder, we mark only Remove Strong Name Signature and we click Rebuild

    Option C: Manual Strong Name Protection Remove using CFF Explorer (the same thing as under B but manual):

    • In CFF Explorer in .NET Directory we have the member Flags (CLI Header Assembly flags) – unmark “Strong name signed”.
    • In CFF Explorer in .NET Directory we have 2 members: StrongNameSignature RVA and StrongNameSignature Size – set value of these to zero.
    • In CFF Explorer in .NET Directory->MetaData Streams->#~->MetaData Tables (Tables) ->Assembly:

    The member PublicKey (the index) in Assembly Table must be 0000

    At Flags member unmark PublicKey - the assembly ref holds the full (unhashed) public key.



    评分

    参与人数 2威望 +2 飘云币 +2 收起 理由
    不破不立 + 1 + 1 PYG有你更精彩!
    wgz001 + 1 + 1 PYG有你更精彩!

    查看全部评分

    本帖被以下淘专辑推荐:

    PYG19周年生日快乐!
  • TA的每日心情
    擦汗
    2024-3-20 09:24
  • 签到天数: 373 天

    [LV.9]以坛为家II

     楼主| 发表于 2022-2-2 23:54:00 | 显示全部楼层
    Typora里排的版,粘贴到论坛好像多多少少有点儿排版问题,将就着看吧……
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    昨天 23:19
  • 签到天数: 998 天

    [LV.10]以坛为家III

    发表于 2022-2-8 23:31:15 | 显示全部楼层
    厉害,谢谢分享。
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    4 天前
  • 签到天数: 152 天

    [LV.7]常住居民III

    发表于 2022-3-27 11:12:20 | 显示全部楼层
            感谢发布原创作品,PYG有你更精彩!
    PYG19周年生日快乐!
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表