1. 앱을 실행 시키자 마자 종료됨. 당황.
2. MainActivity 에서 보안 모듈을 불러오는 듯. 해당 로직에서 루팅을 탐지하고 앱을 종료시키는 듯 하다.
if(a.R()) {
Toast.makeText(this.getApplicationContext(), "Phone is Rooted", 0).show();
this.finish();
}
3. a.R() 메소드 접근
4. 소스코드 분석을 하면 File 메소드 부분만 넘기면 되는듯 하다.
5. android Developer 홈페이지에서 File에 대하여 조금 알아보자.
https://developer.android.com/reference/java/io/File
File | Android Developers
developer.android.com
6. 신기술을 한번 사용해 볼까. chatGPT 에게 의뢰를 한번 해보자. 근데 영어 문법이 맞는지 모르겠다.
역시 신 문물은 대단하다. BUT, Interceptor.attach() 문법은 현재 상황에서 적합하지 않다. 왜냐면, 작동을 안한다.
통상적으로 java에 대해서는 Interceptor.attach 말고 그냥 implementation만 구문만 사용하는데, 아 갑자기 왜 이건되고 저건 안되는 건지 호기심이 들었다. 근데 정확히 모르겠다. 지금부터는 단순히 검색하면서 맞나....? 맞겠지...? 뇌피셜이다.
일단 Interceptor.attach()를 검색하면 NativePointer라는 단어가 항상, 꾸준히 나온다.
NativePointer가 뭐여...?
https://stackoverflow.com/questions/38672839/what-is-a-native-pointer-and-returnaddress
What is a native pointer and returnAddress?
I was reading the internals of JVM from here and came across the term "native pointer", what does it mean, does it has something to do with JNI? In the text above it talks about Program Counters and
stackoverflow.com
일단 몇몇 키워드가 눈에 보인다. 이것들을 조합해서 검색해보면, 킹갓 제너럴 형님께서 설명해주신다.
https://sorjfkrh5078.tistory.com/278
[Java] Java에 포인터가 없는 이유 - 포인터(Pointer) vs 참조(Reference)
C나 C++ 에는 포인터라는 개념이 존재하지만 Java에는 포인터라는 개념을 사용하지 않는다. 비슷한 개념으로 참조라는 것을 사용한다. 그렇다면 왜 Java에는 포인터가 존재하지 않을까? 그전에 포
sorjfkrh5078.tistory.com
아아아아아아, 한마디로 자바는 가비지콜렉터라는 것이 알아서 메모리 관리를 해주는데, 가비지콜렉터가 실행될 때마다 주소값이 바뀌기 때문에 포인터를 사용할 수 없어서 Interceptor.attach()가 에러가 나는 것 같네....? 아마도
결국에 그러한 이유로, Java 클래스를 후킹할 때는 Interceptor.attach() 문법이 적합하지 않다라는 것 같다.
더욱 자세한 내용은 누가 알려줬으면 좋겠다.쩝쩝. 아무도 안 알려준다.
7. 근데 $init은 왜 쓰지. 한번 물어보자. 문법은 내맘대로임.
8. 대충 그렇다고 한다.
9. 그렇다. $init은 Java 클래스 함수의 생성자 함수를 나타내는데 사용된다고 한다. 그럼여기서 Java의 new 연산자에 대해서 조금 알 필요가 있지 않을까.
이럴 땐 우리 개쩌는 형님들의 포스팅을 참고하면 된다.
https://yoo11052.tistory.com/52
[Java] new 연산자란
Java를 하다가 제일 먼저 new 연산자를 접하는게 아마 배열을 공부할 때 일텐데, 나도 처음에 new 연산자가 무슨 역할을 하는지 정확히 모른채 공부를 해오다가 new 연산자의 역할이 꽤나 중요하다
yoo11052.tistory.com
10. 자, 여기까지 이제 작성 되었다. 음.... 알맹이를 작성해야하는데......
function hook_File() {
Java.perform(function() {
var File = Java.use('java.io.File');
var File_constructor = File.$init.overload('java.lang.String');
File_constructor.implementation = function(path) {
}
});
};
hook_File();
11. 그냥 프리다 작성 요령을 검색해보자.
https://neo-geo2.gitbook.io/adventures-on-security/
Hello World! - Adventures on Security
neo-geo2.gitbook.io
오우.... 뭔가 Frida 스크립트를 작성하는 방법론? 비슷한 매뉴얼같다. 이건 정말 개이득이다.
어쨋거나, 핵심은 우리는 함수를 재정의해야한다.
즉, 우리가 재정의 한 함수가 어플리케이션 구동에 영향을 미치지 않고, 우리가 원하는 부분만 변조하겠다라는게 핵심이다.
다시한번 FIle 메소드를 보면 인스턴스 자체를 생성하고 있으며, 우리가 재정의한 함수 역시 인스턴스 자체를 반환시켜주어야 할 것 같다.
껄껄 좋다. 위에서 본 가이드 대로 this를 이용해서 넘겨주면 될 것 같다.
function hook_File() {
Java.perform(function() {
var File = Java.use('java.io.File');
var File_constructor = File.$init.overload('java.lang.String');
File_constructor.implementation = function(path) {
console.log(path);
return this.File_constructor(path);
}
});
};
hook_File();
하..... 그냥 다시 검색이다. 역시 문법은 내 맘이다.
How to get the return value of a Java method using Frida
TLDR i am looking to use Frida to call an unused method in an Android app and get back its value. Currently, I am not getting any return in my scripts output. Code for the Android app: package
stackoverflow.com
갓갓 형님이 알려준데로 다시 해보자.
function hook_File() {
Java.perform(function() {
var File = Java.use('java.io.File');
var File_constructor = File.$init.overload('java.lang.String');
File_constructor.implementation = function(path) {
var retval = File_constructor();
console.log(path);
return retval;
}
});
};
hook_File();
화가 난다. 프리다는 종잡을 수 없다. 그냥 에러 자체를 검색해보자.
https://github.com/frida/frida-java-bridge/issues/37
"TypeError: cannot read property '$handle' of undefined." issue · Issue #37 · frida/frida-java-bridge
Hello, I have been working on analyzing an obfuscated Android malware. When I try to call a function, I receive an error called "TypeError: cannot read property '$handle' of undefined....
github.com
새로운 키워드 .call() 발견되었다. 아래에 너무 자세히 설명되어 있는데, 솔직히 이해 잘 못했다.
Javascript에서의 this 와 call, apply, bind 메서드
어떤 함수가 실행될 때 실행 컨텍스트(execute context)가 block scope가 아닌 function scope로 생성된다. 이렇게 생성되는 Javascript의 모든 function scope 내에서 this라는 특수한 식별자가 자동으로 설정된다. t
velog.io
당장은 단순하게 call() 함수를 통해서 다시한번 자신을 실행한다? 라는 개념으로 접근해봐야겠다.
12. 검색을 토대로 스크립트를 작성해보자.
function hook_File() {
Java.perform(function() {
var File = Java.use('java.io.File');
var File_constructor = File.$init.overload('java.lang.String');
File_constructor.implementation = function(path) {
var retval = File_constructor.call(this, path);
console.log(path);
return retval;
}
});
};
hook_File();
13. 드디어 문자열들이 찍혔다. 이제 /su 문자열을 후킹해주면 될 것 같다.
var DETECT_PATH = ['/su'];
function hook_File() {
Java.perform(function() {
var File = Java.use('java.io.File');
var File_constructor = File.$init.overload('java.lang.String');
File_constructor.implementation = function(path) {
var retval = File_constructor.call(this, path);
console.log(path);
//path 문자열 안에 DETECT_PATH의 문자열이 포함되는 것이 존재하면 해당 path 값만 변조
if (DETECT_PATH.some(element => path.includes(element)))
return File_constructor.call(this, 'vnthasiobnlath');
return retval;
}
});
};
hook_File();
14. 그렇다. 미처 못봤다. 프리다 체크 로직도 우회를 해야한다.
사실, 구글에 'frida File hook' 이라고만 쳐도 수 많은 결과가 출력되며, 아무 스크립트나 복붙해서 사용하면 된다.
BUT, 발전이 없다... 결국에는 기본으로 돌아가서 원리를 깨우쳐야만 한다는 것을 나중에 깨닫는다.
또한, 굳이 굳이 File 메소드를 후킹하는 이유는 내 목표 때문이다.
나는 결국에 모바일 자동화 도구를 만들어 보고싶은데, 모든 어플의 패키지명을 일일히 후킹할 수는 없다.
결국 java api 및 네이티브 함수의 후킹을 통해서 솔루션들을 공통으로 우회해야한다. 아직 감은 안잡힌다.
내가 처음 Frida를 공부할 때, 누가 알려준 적도 없고 할 줄 아는사람도 주변에 없었다. 그렇다고 해서 지금 내가 개쩌는 고수라는 것은 아님. 개초보임.
그래서 지금도 그렇고 이 방식은 리얼 내가 삽질하는 과정 그 자체를 표현하였다.
누군가에게 이렇게 문제를 해결해 나가는 방식이 도움이 될지는 모르겠지만, 일단 나에게는 너무 좋아서 앞으로도 할거다.
끝.
=========================================================================================
추가
$init과 $new의 사용 시점
https://github.com/frida/frida/issues/1035
Hooking of variables and understanding Frida's $new · Issue #1035 · frida/frida
Hi Question 1 I am trying to understand more about how frida works and I was trying to hook some variables. As an example Snippet 1 public class AClass { public static boolean AClassVariable = fals...
github.com
'Mobile > Android' 카테고리의 다른 글
[Android] Damn-Vulnerable-Bank - 2 Frida 탐지 우회 (feat. Frida, 구조체) (0) | 2022.12.30 |
---|---|
[Android] Damn-Vulnerable-Bank 환경구축(Feat. AWS) (0) | 2022.12.29 |
[Android ] adb shell 편하게 잡자(feat. 가상머신) (0) | 2022.10.19 |