iphoneDaemonTool

前言

创建iPhone Daemon的例子

iphone/tool 的编写

  • iphone/tool 创建
    #$/opt/theos/bin/nic.pl 
    NIC 2.0 - New Instance Creator
    [10.] iphone/tool
    
  • layout文件夹下创建LaunchDaemons配置文件
devzkndeMacBook-Pro:LaunchDaemons devzkn$ echo "<?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
-bash: !DOCTYPE: event not found
> <plist version="1.0">
> <dict>
>         <key>KeepAlive</key>
>         <true/>
>         <key>Label</key>
>         <string>com.kn.knDaemonDemo</string>
>         <key>Program</key>
>         <string>/usr/bin/knDaemonDemo</string>
>         <key>RunAtLoad</key>
>         <true/>
> </dict>
> </plist>" > com.kn.knDaemonDemo.plist
devzkndeMacBook-Pro:LaunchDaemons devzkn$ cat *
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Program</key>
	<string>/usr/bin/knDaemonDemo</string>
	<key>RunAtLoad</key>
	<true/>
	<key>KeepAlive</key>
	<true/>
	<key>Label</key>
	<string>com.kn.knDaemonDemo</string>
</dict>
</plist>

在Theos建立的工程的根目录下建立一个 layout文件夹,这个文件夹就相当于设备的根目录了!在编译生成的deb包中,会自动放到对应的文件夹

  • 通过MakeFile 修改守护进程和它的配置文件由root:wheel来拥有
THEOS_DEVICE_IP=usb2222	#5C9

ARCHS = armv7 armv7s arm64
TARGET = iphone:latest:8.0

include $(THEOS)/makefiles/common.mk

TOOL_NAME = knDaemonDemo
knDaemonDemo_FILES = main.mm

include $(THEOS_MAKE_PATH)/tool.mk

after-install::
	install.exec "echo '' > /var/log/syslog"
	install.exec "chown root:wheel /usr/bin/$(TOOL_NAME)"
	install.exec "chown root:wheel  /Library/LaunchDaemons/com.kn.$(TOOL_NAME).plist"
	install.exec "reboot"
  • deploy
    #!/bin/sh
    cd `dirname $0` 
    make clean
    make package install
    rm -f ./debs/*
    exit 0
    
    devzkndeMacBook-Pro:kndaemondemo devzkn$ deploy
    ==> Cleaning…
    Making all for tool knDaemonDemo…
    ==> Compiling main.mm (armv7)…
    ==> Linking tool knDaemonDemo (armv7)…
    ==> Compiling main.mm (armv7s)…
    ==> Linking tool knDaemonDemo (armv7s)…
    ==> Compiling main.mm (arm64)…
    ==> Linking tool knDaemonDemo (arm64)…
    ==> Merging tool knDaemonDemo…
    ==> Signing knDaemonDemo…
    Making stage for tool knDaemonDemo…
    dpkg-deb: building package 'com.yourcompany.kndaemondemo' in './packages/com.yourcompany.kndaemondemo_0.0.1-2+debug_iphoneos-arm.deb'.
    ==> Installing…
    Selecting previously unselected package com.yourcompany.kndaemondemo.
    (Reading database ... 4263 files and directories currently installed.)
    Preparing to unpack /tmp/_theos_install.deb ...
    Unpacking com.yourcompany.kndaemondemo (0.0.1-2+debug) ...
    Setting up com.yourcompany.kndaemondemo (0.0.1-2+debug) ...
    install.exec "echo '' > /var/log/syslog"
    install.exec "chown root:wheel /usr/bin/knDaemonDemo"
    install.exec "chown root:wheel  /Library/LaunchDaemons/com.kn.knDaemonDemo.plist"
    install.exec "reboot"
    Connection to localhost closed by remote host.
    
  • 查看运行效果
    iPhone:~ root# ps -e |grep knDaemonDemo
    160 ??         0:10.72 /usr/bin/knDaemonDemo
    510 ttys000    0:00.00 grep knDaemonDemo
    

dylib、bundle或daemon,的位置

  • 基于CydiaSubstrate的dylib
    iPhone:~ root# ls -l /Library/MobileSubstrate/DynamicLibraries/
    total 3568
    -rwxr-xr-x 1 mobile staff 1292720 Oct 31 15:01 AFlexLoader.dylib*
    -rw-r--r-- 1 mobile staff      60 Oct 31 15:01 AFlexLoader.plist
    
  • Bundle
    # AppStore App
    /var/mobile/Containers/Bundle/Application/
    # 系统自带的app
    iPhone:~ root# ls -l /Applications
    lrwxr-xr-x 1 root admin 32 Apr 23  2017 /Applications -> /var/stash/_.TvfJPY/Applications/
    #Frameworks
    iPhone:~ root# ls -l /System/Library/Frameworks
    total 0
    drwxr-xr-x 42 root wheel 1462 Oct 14  2014 AVFoundation.framework/
    drwxr-xr-x 42 root wheel 1496 Oct 14  2014 AVKit.framework/
    #PrivateFrameworks
    iPhone:~ root# ls -l /System/Library/PrivateFrameworks
    total 4
    drwxr-xr-x  4 root wheel   306 Oct  7  2014 ABLE.framework/
    drwxr-xr-x  4 root wheel   408 Oct  7  2014 ABLEModel.framework/
    drwxr-xr-x  4 root wheel   170 Sep 13  2014 ACTFramework.framework/
    
  • daemon的配置文件
    iPhone:~ root# ls -lrt /System/Library/LaunchDaemons/
    total 32
    -rwxr-xr-x 1 root wheel   328 Mar  6  2014 com.jensen.iRE.Startup.plist*
    -rw-r--r-- 1 root wheel   281 Sep 13  2014 com.apple.absd.plist
    # 通常用户自己的demo,就放在此目录,就比如本文的knDaemonDemo
    iPhone:/Library root# ls -lrt /Library/LaunchDaemons
    total 24
    -rw-r--r-- 1 root wheel 847 Feb 15  2011 com.openssh.sshd.plist
    -rw-r--r-- 1 root wheel 267 Feb  6  2017 com.rpetrich.rocketbootstrapd.plist
    -rw-r--r-- 1 root wheel 446 Feb 16  2017 com.saurik.Cydia.Startup.plist
    lrwxr-xr-x 1 root admin  61 Apr 23  2017 com.apple.mobile.installd.plist -> /System/Library/LaunchDaemons/com.apple.mobile.installd.plist
    -rw-r--r-- 1 root wheel 779 Dec 14 00:54 re.frida.server.plist
    -rw-r--r-- 1 root wheel 366 Dec 24 14:24 com.kn.knDaemonDemo.plist
    # /Library/LaunchAgents
    iPhone:/Library root# ls -lrt /Library/LaunchAgents
    

knDaemonDemo/main.mm

  • 入口方法
    int main(int argc, char **argv, char **envp) {
    return 0;//可以用来注册 CFMessagePort
    }
    int main (int argc, const char * argv[]) {
    return 0;
    }
    
  • hook 的代码在main()执行结束之前或者之后执行(解释一下:attribute((constructor)) 在main() 之前执行,attribute((destructor)) 在main()执行结束之后执行.)— 可以用来hook 守护进程。
//constructor   在main之前 
%ctor {
    setuid(0); //设置超级用户
}
 等价于
static __attribute__((constructor)) void _logosLocalCtor_f35a0c0f(int __unused argc, char __unused **argv, char __unused **envp) {

}
%dtor { … }
  • 设置constructor优先级
//声明
__attribute__((constructor(101))) void before1();

//实现
void before1()
{
    printf("before1\n");
}

参考

解释一下:__attribute__((constructor)) 在main() 之前执行,__attribute__((destructor)) 在main()执行结束之后执行.
#include <stdio.h>
#include <stdlib.h>
 
static  __attribute__((constructor)) void before()
{
 
    printf("Hello");
}
 
static  __attribute__((destructor)) void after()
{
    printf(" World!\n");
}
 
int main(int args,char ** argv)
{
 
    return EXIT_SUCCESS;
}

see also

  • osx
  • iphone

转载请注明: > iphoneDaemonTool

在操作过程或者文章有问题的话欢迎在 原文 里提问或指正。

赞赏支持

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少
最近的文章

IDA

前言Interactive Disassembler Professional(“交互式反汇编器专业版”) 共有(File , Edit , Jump , Search , View , Debugger , Options , Windows , Help)9个模块.“IDA 目录的结构” /Applications/IDA Pro 7.0/ida64.app/Contents/MacOS /Applications/IDA Pro 7.0/ida64.app/Co...…

iOSre继续阅读
更早的文章

assemblyLanguage

前言汇编语言是用助记符,符号和数字等来表示指令的程序设计语言,它与机器语言指令是一一对应的 不同内核的CPU,必须有对应的汇编语言编译器将汇编语言编写的程序编译成对应CPU的机器语言代码,CPU才能正确识别和执行这些代码;不同架构的CPU的汇编指令集并不相同 汇编的本质就是在(寄存器和寄存器)或 (寄存器和内存)之间来回move 数据. 汇编程序员可以使用指令来读写CPU中的寄存器,从而实现对于CPU的控制 8086的寻址...…

iOSre继续阅读
更多