본문 바로가기
보안/iOS보안

TheOS를 이용한 MobileSubstrate Tweak 빌드하기

by lucidmaj7 2023. 2. 25.
728x90
반응형

주의)

본 포스트는 연구목적을 위해 작성되었습니다.

절대 악의적인 행위를 시도하지 마세요.

책임은 절대 본인에게 있습니다.

This post is intended for research purposes. Never try anything malicious. The responsibility is absolutely up to you.

本郵報是爲研究目的而寫的。千萬不要嘗試惡意行爲。責任絕對在於本人。

本ポストは研究目的のために作成されました。絶対に悪意のある行為を試みないでください。責任は絶対本人にあります。

 

 

 

1. TheOS 설치

 bash -c "$(curl -fsSL https://raw.githubusercontent.com/theos/theos/master/bin/install-theos)"

설치가 완료되면 다른 터미널을 열어 다음 명령어를 쳐보자.

$THEOS/bin/nic.pl

2. iphone/tweak 프로젝트 생성

$THEOS/bin/nic.pl

nic.pl 명령어를 입력하고 iphone/tweak을 선택한다.

 

iphone/tweak을 선택하고나면 프로젝트 생성을 위해 이것 저것 물어보는데 여기서 제일 중요한 건 Bundle filter이다.

만들어진 tweak이 어떤 앱에 삽입될지를 명시하는 부분으로 지정된 bundle id를 가진 앱에만 로드되게 된다.

여기서는 lucidmaj7.tweaktest.testapp이라는 테스트 앱에 삽입될 것이므로 MobileSubstrate Bundle Filter에 동일하게 써준다.

정상적으로 생성되었다면  트윅 프로젝트 폴더에 다음과 같은 파일이 존재하게 된다.

여기서 Tweak.x파일이 실제 Tweak으로 앱에 삽입되어 돌아가는 코드를 담게 된다.

3. 후킹 Tweak 예제 코드

이제 Tweak.x코드에 fopen, lstat 함수를 후킹하는 코드를 예제로 작성해보겠다.

(자세한 내용은 https://theos.dev/docs/logos-syntax 를 참고하기 바람)

#import <Foundation/Foundation.h>
#include <sys/stat.h>

FILE *fopen(const char *path, const char *mode);
int lstat(const char *, struct stat *);

// The hook is thus made
%hookf(FILE *, fopen, const char *path, const char *mode) {
        NSLog(@"hellotweak: fopen path=%s\n", path);
        return %orig; // Call the original implementation of this function
}

%hookf(int, lstat, const char *filename, struct stat* buf) {
        NSLog(@"hellotweak: lstat path=%s\n", filename);
        return %orig; // Call the original implementation of this function
}

%ctor {
        NSLog(@"hellotweak ctor");
}

위와 같이 Logos를 활용해, 후킹 코드를 작성해보았다. 

4. 빌드

코드가 다 작성되었으면 make 명령을 사용하여 다음과 같이 빌드를 해준다.

make package FINALPACKAGE=1

빌드가 완료되면 ./packages에 deb 패키지 파일이 생성된다.

5. 탈옥된 아이폰에 설치하기

빌드된 트윅 패키지(deb) 탈옥된 아이폰에 옮긴다. scp를 이용하여 옮기는 편이 편하다.

다음 패키지를 설치하기 위해 dpkg명령으로 설치해준다.

dpkg -i [deb패키지파일]

설치가 성공적으로 완료되면

/Library/MobileSubstrate/DynamicLibraries에 dylib파일이 존재하는것을 볼 수 있다.

이 파일이 앱이 실행될 때 삽입된다.

respring을 한번 실행해준다.

killall SpringBoard

6. 앱 실행

콘솔을 통해  Tweak에서 NSLog로 출력되는 결과를 볼 수 있다.

 

728x90
반응형

댓글