Harekaze Advent Calender21æ¥ç®ã®è¨äºã§ã
忥ã¯banbanããã§ãããHarekazeãããããè¨äºã§ãï¼ï¼
ããã§ç§ã話ãã®ã¯æè¿è¦ã¤ããCTFã«ä½¿ãããã¼ã«ã®ä½¿ãæ¹ã§ãã
Frida
Fridaã¨ã¯Androidã¢ããªã¸ã®åçè§£æãã¼ã«ã§ããã¢ããªã®è§£æãã»ãã¥ãªãã£ã«é¢ãã調æ»ãè¡ãããã¼ã«ã ããã§ãã
ã¤ãæè¿ç¥ã£ãã®ã§ããä»åã¯ããã使ãã¨ããã¾ã§è¡ããããªã¨æãã¾ã
ã¡ãªã¿ã«ãã®è¨äºã§ã¯Android Emulatorãªã©ã¯ä½¿ããåæã§é²ãã¾ããå
·ä½çã«è¨ãã¨adb devices
ã³ãã³ããæã£ãã¨ãã«ããã¤ã¹ãèªèããã¦ããç¶æ³ã«ãã¦ãã ããã
ç§ã®è©¦ããç°å¢ã¯ãããªæã
$ uname -a Linux durian 3.13.0-121-generic #170-Ubuntu SMP Wed Jun 14 09:04:33 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 14.04.5 LTS Release: 14.04 Codename: trusty $ adb --version Android Debug Bridge version 1.0.39 Version 27.0.0-4455170 Installed as ~/android/android-sdk-linux/platform-tools/adb
Android Emulatorã®ç°å¢
ç°å¢ | |
---|---|
OS | Android7.0.0 |
CPU | ARM(armeabi-v7a) |
Fridaã®ã¤ã³ã¹ãã¼ã«
åºæ¬çã«ã¯Fridaã®ãã¼ã¸ã«æ¸ãã¦ããã¨ããã«ããã°åé¡ãªãã§ãã
Linuxå´ã§ã®æºå
$ sudo pip install frida
Androidå´ã®æºå
ã¾ãããããfrida-serverããã¦ã³ãã¼ãå±éãã¾ãã
$ adb root # might be required $ adb push frida-server /data/local/tmp/ $ adb shell "chmod 755 /data/local/tmp/frida-server" $ adb shell "/data/local/tmp/frida-server &"
ã¨ããæãã§frida-serverãå®è¡ãã¦ããã°æºåå®äºã§ãã
Fridaã使ã
ãµã³ãã«ã³ã¼ããç¨ãã¦è§£èª¬ãã¦ããã¾ãããã®ãµã³ãã«ã³ã¼ãã§ããSECCON2015 reverse-engineering-android-apk-1ã®åé¡ã§ãã
import frida, sys def on_message(message, data): if message['type'] == 'send': print("[*] {0}".format(message['payload'])) else: print(message) jscode = """ Java.perform(function () { // Function to hook is defined here var MainActivity = Java.use('com.example.seccon2015.rock_paper_scissors.MainActivity'); // Whenever button is clicked MainActivity.onClick.implementation = function (v) { // Show a message to know that the function got called send('onClick'); // Call the original onClick handler this.onClick(v); // Set our values after running the original onClick handler this.m.value = 0; this.n.value = 1; this.cnt.value = 999; // Log to the console that it's done, and we should have the flag! console.log('Done:' + JSON.stringify(this.cnt)); }; }); """ process = frida.get_usb_device().attach('com.example.seccon2015.rock_paper_scissors') script = process.create_script(jscode) script.on('message', on_message) print('[*] Running CTF') script.load() sys.stdin.read()
åºæ¬çãªä½¿ãæ¹ã§ãã
process = frida.get_usb_device().attach(packagename)
ã®packageã«è§£æããapkã®ããã±ã¼ã¸ãå
¥åãããã¨ã§åçè§£æãã§ããç°å¢ã«ãªã
process.create_script(jscode)
ã§å®è¡ãããJavaScriptãèªã¿è¾¼ã¿script.load()ã§ã¹ã¯ãªããå®è¡ããã¾ããFridaã§ä½¿ããJavaScriptã®ã¡ã½ããã¯ãããåç
§ãã¦ãã ããã
ãã®apkã®MainActivityã§ããä¸ã®ããã«ãªã£ã¦ãã¾ãã
public class MainActivity extends Activity implements android.view.View.OnClickListener { public MainActivity() { cnt = 0; } public native int calc(); public void onClick(View view) { if(flag == 1) return; flag = 1; ((TextView)findViewById(0x7f0c0052)).setText(""); TextView textview = (TextView)findViewById(0x7f0c0050); TextView textview1 = (TextView)findViewById(0x7f0c0051); m = 0; n = (new Random()).nextInt(3); int i = n; textview1.setText((new String[] { "CPU: Paper", "CPU: Rock", "CPU: Scissors" })[i]); if(view == P) { textview.setText("YOU: Paper"); m = 0; } if(view == r) { textview.setText("YOU: Rock"); m = 1; } if(view == S) { textview.setText("YOU: Scissors"); m = 2; } handler.postDelayed(showMessageTask, 1000L); } protected void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(0x7f040018); P = (Button)findViewById(0x7f0c004d); S = (Button)findViewById(0x7f0c004f); r = (Button)findViewById(0x7f0c004e); P.setOnClickListener(this); r.setOnClickListener(this); S.setOnClickListener(this); flag = 0; } Button P; Button S; int cnt; int flag; private final Handler handler = new Handler(); int m; int n; Button r; private final Runnable showMessageTask = new Runnable() { public void run() { TextView textview = (TextView)findViewById(0x7f0c0052); if(n - m == 1) { MainActivity mainactivity = MainActivity.this; mainactivity.cnt = mainactivity.cnt + 1; textview.setText((new StringBuilder()).append("WIN! +").append(String.valueOf(cnt)).toString()); } else if(m - n == 1) { cnt = 0; textview.setText("LOSE +0"); } else if(m == n) textview.setText((new StringBuilder()).append("DRAW +").append(String.valueOf(cnt)).toString()); else if(m < n) { cnt = 0; textview.setText("LOSE +0"); } else { MainActivity mainactivity1 = MainActivity.this; mainactivity1.cnt = mainactivity1.cnt + 1; textview.setText((new StringBuilder()).append("WIN! +").append(String.valueOf(cnt)).toString()); } if(1000 == cnt) textview.setText((new StringBuilder()).append("SECCON{").append(String.valueOf((cnt + calc()) * 107)).append("}").toString()); flag = 0; } final MainActivity this$0; { this$0 = MainActivity.this; super(); } }; static { System.loadLibrary("calc"); } }
onClickã§ããããããéå§ããã¦mã¨nã¨cntã®å¤æ¬¡ç¬¬ã§flagãåºåãããããã§ãã
m=1
,n=0
,cnt=999
ãã»ãããã¦run
颿°ãå®è¡ã§ããã°calcã®ãã£ã¹ã¢ã»ã³ãã«ãããªãã¦ãçããåºããã§ã
Fridaã§ããæ¢åã®Javaã¡ã½ããã«æ¡å¼µãæ½ããã¨ãã§ãã¾ããä¸ã®ã¯å ¬å¼ã§è¼ãããã¦ãããµã³ãã«ã§ãã
Java.perform(function () { var Activity = Java.use("android.app.Activity"); Activity.onResume.implementation = function () { send("onResume() got called! Let's call the original implementation"); this.onResume(); }; });
Java.perform
ã§Javaã使ããã¨ã宣è¨ããJava.use
ã§æ¡å¼µãããJavaã®ã¯ã©ã¹ãè¨å®ãActivity.---.implementation
ã§ãã®ã¯ã©ã¹ã®ä¸ã®é¢æ°ã«æ¡å¼µãæ½ãã¾ãã
ã¨ã¯ã¹ããã¤ãã³ã¼ãã¯com.example.seccon2015.rock_paper_scissors.MainActivity
ã®m
ã¨n
ã¨cnt
ã夿´ãããã®ã§ä¸ã®ããã«ãªãã¾ã
Java.perform(function () { // Function to hook is defined here var MainActivity = Java.use('com.example.seccon2015.rock_paper_scissors.MainActivity'); // Whenever button is clicked MainActivity.onClick.implementation = function (v) { // Show a message to know that the function got called send('onClick'); // Call the original onClick handler this.onClick(v); // Set our values after running the original onClick handler this.m.value = 0; this.n.value = 1; this.cnt.value = 999; // Log to the console that it's done, and we should have the flag! console.log('Done:' + JSON.stringify(this.cnt)); };
ããã§å®è¡ããã¨æ¬¡ã®ç»é¢ã«ãªãã¾ã(ã¹ã¯ã·ã§ãæ»ãã§ãã®ã§surfaceã«ã¡ã©ã§ããã¾ãã)
Fridaã§ã§ãããã¨
ç¾ç¶èªåã調ã¹ã¦ãçã£æä¸ãªã®ã§ã¾ã ã¾ã ã§ãããã¨ã¯ããã¨æãã¾ããCTFã§ãã使ããããªã®ã
- Javaã¡ã½ãããæ¡å¼µãã
- å®è¡ä¸ã®ã¡ã¢ãªã®ä¸èº«ãè¦ã
ãããããªã¨....
çµãã
ã¡ãªã¿ã«Android Hacking Event 2017 AES-Decrypt Writeup - ごちうさ民の覚え書きãFridaã使ã£ã¦ãã¾ããå人çã«ã¯è¦æ¸ã飲ãã§ããHelloDalvikãFridaã§ãããªãããªã¨èãã¦ãã¾ããããã°ãã¾ã
ææ¥ã¯ãããããã§ãï¼CBCTFã®æå·åã®è§£èª¬ã£ã¦ããã..
ç´ æ´ããã人ã®åæ¥ãèªåã§ããã¾ãã.....ããCTFã©ã¤ããéã£ã¦ãã ããï¼ï¼