> For the complete documentation index, see [llms.txt](https://docs.cooku222.kr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cooku222.kr/security/mobile/frida-lab/frida-labs-0x5.md).

# Frida-Labs 0x5

출처: <https://github.com/DERE-ad2001/Frida-Labs/tree/main/Frida%200x5>

***

```
PS C:\Users\after\Downloads> adb install Challenge_0x5.apk
Performing Streamed Install
Success
```

여느때와 마찬가지로 설치하고 시작

<figure><img src="https://blog.kakaocdn.net/dna/cZXd9t/dJMcacB5bFZ/AAAAAAAAAAAAAAAAAAAAAAvdmkl0oWyzu7tbAkECeLyq9NAEhOsnZCWgAIpW6Sh6/img.png?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&#x26;expires=1782831599&#x26;allow_ip=&#x26;allow_referer=&#x26;signature=K8aDCZVAOy8Yqot8qTqtB0sqMow%3D" alt="" height="823" width="1432"><figcaption><p>MainActivity</p></figcaption></figure>

MainActivity 안에는 flag라는 메서드가 있다. 이 메서드는 플래그를 복호화한 뒤 그 값을 TextView에 설정하고 있다. 또한 if 체크를 우회하려면 인자로 1337을 전달해야한다.\
이 flag() 메서드는 다른 클래스에서 호출되지 않고 있다. 그렇다면 MainActivity의 새로운 인스턴스를 하나 생성해서 이 메서드를 직접 호출할 수 있겠다.

* 패키지 이름: com.ad2001.frida0x5
* 클래스 이름: MainActivity
* 함수 이름: flag

```
Java.perform(function() {

  var <class_reference> = Java.use("<package_name>.<class>");
  var <class_instance> = <class_reference>.$new(); // Class Object
  <class_instance>.<method>(); // Calling the method

})
```

```
Java.perform(function(){
	var a=Java.use("com.ad2001.frida0x5.MainActivity");
	var main_act=a.$new();
	main_act.flag(1337);
})
```

<figure><img src="https://blog.kakaocdn.net/dna/oKFkR/dJMcafyO0mt/AAAAAAAAAAAAAAAAAAAAACpbLJ-4hsblSjt4NBnEVZHWevqi1D_4dRzSp6tH-Ora/img.png?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&#x26;expires=1782831599&#x26;allow_ip=&#x26;allow_referer=&#x26;signature=gX69vHs9ABzf%2FSUmTDyD7fhvQd0%3D" alt="" height="700" width="767"><figcaption></figcaption></figure>

위와 같이 에러가 터지는 것을 확인할 수 있는데, frida로 MainActivity에 인스턴스를 넣는게 상당히 까다로울 수 있다고 한다. 특정 상태에서만 이를 성공시킬 수 있다고 한다. \
따라서 이미 MainActivity에 존재하는 인스턴스를 가져와서 작동시키는 것이 해결책이 될 수 있겠다.&#x20;

**기존 인스턴스에서 메서드 호출하기**

이번에는 다른 Frida 템플릿을 사용해본다.

* Java.performNow : Java 런타임 컨텍스트 안에서 코드를 즉시 실행하기 위해 사용되는 함수
* Java.choose: 런타임 중에, 첫 번째 인자로 전달된 지정된 Java 클래스의 인스턴스들을 열거

```
Java.performNow(function() {
  Java.choose('<Package>.<class_Name>', {
    onMatch: function(instance) {
      // TODO
    },
    onComplete: function() {}
  });
});
```

여기에는 두 가지 콜백 함수가 존재하는데,&#x20;

#### onMatch

onMatch 콜백 함수는 Java.choose작업 중에 지정된 클래스의 각 인스턴스가 발견될 때마다 실행된다.\
이 콜백 함수는 현재 매칭된 인스턴스를 매개변수로 전달받는다.\
onMatch 내부에서는 각 인스턴스에 대해 수행하고 싶은 커스텀 동작을 자유롭게 정의할 수 있다.

```
function(instance) {}
```

여기서 instance 매개변수는 대상 클래스에서 매칭된 각 인스턴스를 의미하며, 이름은 원하는 다른 이름으로 바꿔도 상관없다.

#### onComplete

onComplete 콜백은 Java.choose 작업이 완전히 종료된 후 실행된다. 이 블록에서는 후처리 작업이나 정리 로직을 수행할 수 있다.\
onComplete는 선택 사항이며, 검색이 끝난 뒤에 특별히 할 작업이 없다면 비워 두어도 문제없다.

* 패키지 이름: com.ad2001.frida0x5
* 클래스 이름: MainActivity
* 함수 이름: flag

```
Java.performNow(function(){
	Java.Choose('com.ad2001.frida0x5.MainActivity', {
		onMatch: function(instance){
        	console.log("Instance Found");
        },
        onComplete: function(){
    });
});
```

```
[Android Emulator 5554::com.ad2001.frida0x5 ]-> Java.performNow(functi
on() {
  Java.choose('com.ad2001.frida0x5.MainActivity', {
    onMatch: function(instance) {
      console.log("Instance found");
    },
    onComplete: function() {}
  });
});
Instance found
Instance found
```

이렇게 하면 입력해준 자바스크립트 코드가 작동하기는 하는데.. 아직 flag()에 1337 값을 전달해주지 않아 후킹이 실행되진 않는다.

```
[Android Emulator 5554::com.ad2001.frida0x5 ]-> Java.performNow(functi
on() {
  Java.choose('com.ad2001.frida0x5.MainActivity', {
      onMatch: function(instance) { // "instance" is the instance for
the MainActivity
        console.log("Instance found");
        instance.flag(1337); // Calling the function
    },
    onComplete: function() {}
  });
});
Instance found
Instance found
```

<figure><img src="https://blog.kakaocdn.net/dna/bazLFf/dJMcadgF8yZ/AAAAAAAAAAAAAAAAAAAAAM4jlpfcki7nWII82mwSgFeq3z7YnASKJhzHNOFPBQWg/img.png?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&#x26;expires=1782831599&#x26;allow_ip=&#x26;allow_referer=&#x26;signature=vnNEsCuCaUvGNN1SqCOUsCUlDnI%3D" alt="" height="590" width="293"><figcaption></figcaption></figure>

플래그가 뜬다!\
\
+) 콜백 함수란?\
다른 함수에 인자로 전달되어 나중에 호출하는 함수를 이른다. 비동기 처리나 함수의 동작 형태를 위임하고 싶을때 사용한다.

<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cooku222.kr/security/mobile/frida-lab/frida-labs-0x5.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
