tl;dr
codesign --remove-signature
在删除代码签名之后没有修复MachO Header的偏移,导致生成的MachO文件畸形。我很确定应该有无数人踩过这个坑,自己研究一遍权当练习
正文
以Dash为例,操作之后的app打开直接crash,以lldb file
后报warning:
warning: (x86_64) /Applications/Dash.app/Contents/MacOS/Dash load command 3 LC_SEGMENT_64 has a fileoff + filesize (0x335ed0) that extends beyond the end of the file (0x335ec4), the segment will be truncated to match Current executable set to '/Applications/Dash.app/Contents/MacOS/Dash' (x86_64).
用MachOView打开可执行文件,定位到最后一个LC_SEGMENT:
简单的做下数学验证:
0x31D000+0x18ED0=0x335ED0
,比文件实际大小0x335ec4大出0x335ED0-0x335ec4=0xC
将原来的File Size 减少0xC,改为0x18ec4
,保存
这里正确的做法是根据原版的LC_CODE_SIGNATURE计算正确的新大小而不是无脑修改到文件末尾, 只不过对于我们的使用需求而言这么做足够了
结论
- 不要迷信Apple官方的工具,第三方实现例如https://github.com/steakknife/unsign https://github.com/alexzielenski/optool 都是很不错的选择
- 盘古(我记得是833? 越狱)利用Segment Overlapping之后Apple加强了对MachO的校验(也干掉了我很多戏耍■■者的Trick, 心好痛)