写了个修改进程内存数据的程序,需要用到task_for_pid函数。之前对一个进程内存搜索修改都没有问题,换了个进程就报错kern_failure
JerrysPhone:/home/jerry root# ./mscan 810
attach to process pid[810]
kr: 5
The_task: 0×0
这说明mscan程序不具备访问别的进程的权限
<key>task_for_pid-allow</key>
并且,这个被扫描报错的进程也没有主动开放被别的进程访问的能力
<key>get-task-allow</key>
作为调试工具,需要在签名是带上下面的entitlement,以便内核开放出功能
http://iphonedevwiki.net/index.php/Debugserver#Patching_for_process_attaching
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>com.apple.springboard.debugapplications</key>
<true/>
<key>get-task-allow</key>
<true/>
<key>task_for_pid-allow</key>
<true/>
<key>run-unsigned-code</key>
<true/>
</dict>
</plist>
如果去看gdb和memscan,都是具备这样的权限:
ldid -e /usr/bin/gdb
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>com.apple.springboard.debugapplications</key>
<true/>
<key>get-task-allow</key>
<true/>
<key>task_for_pid-allow</key>
<true/>
</dict>
</plist>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>com.apple.springboard.debugapplications</key>
<true/>
<key>get-task-allow</key>
<true/>
<key>task_for_pid-allow</key>
<true/>
</dict>
</plist>
root# ldid -e /usr/bin/memscan
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>com.apple.springboard.debugapplications</key>
<true/>
<key>get-task-allow</key>
<true/>
<key>task_for_pid-allow</key>
<true/>
<key>run-unsigned-code</key>
<true/>
</dict>
但我遇到的问题是说ldid -Sentitlement.xml app之后,运行就报Killed: 9,固件版本为6.1(10B142)
root# ./mscan 810
Killed: 9
这说明签名后的程序无法通过内核的校验。
OSX自带签名工具codesign的使用小结:
1. 使用codesign,首先需要产生自签名证书。
参考:《解决iOS上Killed 9 错误》
http://danqingdani.blog.163.com/blog/static/186094195201332210447261/
他创建了一个名为tanjiti的自签名证书
2. 签名一个程序的命令格式为:
codesign -fs <自签名证书> mach-o_file
例如:codesign -fs tanjiti sqlite3
3. 签名一个程序,并添加资格(Entitlements)文件:
codesign -fs <自签名证书> --entitlements <资格文件> mach-o_file
4. 查看签名程序的资格(Entitlements),可以用下面的命令,注意有一个“-”
codesign -d --entitlements - /Applications/Whatever.app/
5. 另外我看到一种代--resource-rules的使用方式
codesign --force --sign <自签名证书> --resource-rules=ResourceRules.plist --entitlements ent.xml mach-o_file
显然
--force等价于-f
--sign等价于-s
--force --sign等价于-fs
我用上面第3种方式来签名同样遇到Killed: 9的问题。用第2种方式回到kern_failure的问题。这样看和ldid的效果一致。
因为开始说到“之前对一个进程内存搜索修改都没有问题”,于是聚焦到这个进程,把entitlements拿出来一看,发现有:
<key>get-task-allow</key><true/>
所以找到出现kern_failure问题的mach-o文件,entitlements内容如下:
<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>keychain-access-groups</key>
<array>
<string>LHH6UF568H.com.wanmei.mtheroes</string>
</array>
<key>application-identifier</key>
<string>LHH6UF568H.com.wanmei.mtheroes</string>
<key>aps-environment</key>
<string>production</string>
</dict>
</plist>
做如下修改:
- <string>production</string>
-
+ <string>development</string>
+ <key>get-task-allow</key>
+ <true/>
保存成资格文件ent.xml,然后签名:
codesign -fs <自签名证书> --entitlements ent.xml mach-o_file
这样kern_failure的问题针对这个mach-o_file解决了。
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课