IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

IOS安全—阻止tweak注入hook api

Coder 2016-02-11 14:59:15 累计浏览 1,921 次
本机暂存

在网上看到一种方法可以通过在Other Linker Flags中添加:

1
-Wl,-sectcreate,__RESTRICT,__restrict,/dev/null

的方法来阻止dylib注入。

便动手试了一下,编写一个测试Demo不添加任何linker flags,然后使用theos对其进行hook。

Snip20151110_8

启动后使用image list -o -f   来查看加载的动态库:

[145] 0x0025f000 /Library/MobileSubstrate/MobileSubstrate.dylib(0x000000000025f000)

[152] 0x003c0000 /Library/MobileSubstrate/DynamicLibraries/TestLog.dylib(0x00000000003c0000)

发现我们用来hook编写的dylib已经被加载,自然能看到hook成功的log。

2015-11-10 15:21:50.824 myTest[21906:273137] viewDidLoad   — 你已经被我Hook了。。。。。。

然后使用linker flags:

1
-Wl,-sectcreate,__RESTRICT,__restrict,/dev/null

重新编译生成。

运行后在所有加载的动态库中已找不到hook的dylib,hook失败了,说明这是有效的。

再来看看生成的macho文件多了一个__RESTRICT/__restrict  section。

Snip20151110_9

为什么加了这样的一个section就能阻止dylib注入了呢?

在这里找到了答案:http://www.opensource.apple.com/source/dyld/dyld-210.2.3/src/dyld.cpp

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

if ( removedCount != 0 ) {

dyld::log("dyld: DYLD_ environment variables being ignored because ");

switch (sRestrictedReason) {

case restrictedNot:

break;

case restrictedBySetGUid:

dyld::log("main executable (%s) is setuid or setgid\n", sExecPath);

break;

case restrictedBySegment:

dyld::log("main executable (%s) has __RESTRICT/__restrict section\n", sExecPath);

break;

case restrictedByEntitlements:

dyld::log("main executable (%s) is code signed with entitlements\n", sExecPath);

break;

}

}

也就是说下面三种情况,可以让环境变量:DYLD_INSERT_LIBRARIES被无视

  • 1.Set restricted status by entitlements

    This option is only available to applications on OS X with special entitlements.

  • 2.setuid and setgid

    Any application that makes these two calls are going to be marked as restricted by the linker as a security measure.

  • 3.Restricted Segment of Header

    The final way to mark a binary as restricted is by telling the linker to add new section to the binary header that is named “__RESTRICT” and has a section named “__restrict” when you compile it.

所以编译生成的含有__RESTRICT/__restrict  section的app会忽略DYLD_INSERT_LIBRARIES。

当然解决办法也是有的,把section的名字修改一下即可。

用010 editor打开可执行文件,找到section的名字:

Snip20151110_10

手动修改其为另外的一个字符串,结果如下:

Snip20151110_12

再用MachOView打开,确认没有改错:

Snip20151110_13

然后重新签名,安装app.

又可以成功注入了!

参考链接:

http://bbs.iosre.com/t/tweak-app-app-tweak/438

http://www.opensource.apple.com/source/dyld/dyld-210.2.3/src/dyld.cpp

http://www.samdmarshall.com/blog/blocking_code_injection_on_ios_and_os_x.html

同分类推荐文章

  1. 绿盟科技《APT组织研究年鉴》(2026 版)正式发布 (2026-06-16 20:21:10)
  2. 【已复现】Linux内核Fragnesia权限提升漏洞(CVE-2026-46300) (2026-06-15 10:53:58)
  3. 企业文档安全最佳实践(二):给文档上“身份证”——手动标密与智能自动标密 (2026-06-12 17:18:33)

查看更多 安全 文章 →

建议继续学习

  1. App的成本 (累计阅读 7,584)
  2. iPhone下的libcurl with SSL for iOS (累计阅读 6,350)
  3. iOS的定位原理揭秘 (累计阅读 5,946)
  4. iOS内存暴增问题追查与使用陷阱 (累计阅读 5,838)
  5. IOS APP设计流程 (累计阅读 5,085)
  6. 在Visual Studio中使用MonoTouch开发iOS应用程序(上):环境配置 (累计阅读 5,076)
  7. iOS push服务 (累计阅读 4,943)
  8. 有关思维,有关Ipad一个Bug的故事 (累计阅读 4,770)
  9. IPhone上的邮件推送 (累计阅读 4,617)
  10. 手机交互设计资料 (累计阅读 4,508)