æ ªå¼ä¼ç¤¾Ninjastarsã»ãã¥ãªãã£ã¨ã³ã¸ãã¢ã®ä¸ç¬ã§ãã
ä»æ¥ã¯Androidã¢ããªã±ã¼ã·ã§ã³ã§ã®LD_PRELOADæ»æã¨ãã®å¯¾çã«ã¤ãã¦æ¸ããã¦ããã ãã¾ãã
ãã¼ãè¡çºãªã©ã«ããã¦ã¯LD_PRELOADãå©ç¨ããæ»æã¯ä¸è¬çã§ã¯ããã¾ããããåå¨ãç¥ããªãã¨ãªã¹ã¯å¯¾çãã§ããªãã®ã§ãç´¹ä»ãããã¾ãã
åæ
Android Studioã»ããã¢ããæ¸ã¿
ndk-buildãã§ããç¶æ
ç°å¢
rootedãªå®æ© or Android Emulator
ä»åãããã¨
LD_PRELOADで動的ライブラリ関数を上書きする | Siguniang's Blog
ã¾ãä¸è¨ã®æ¹ã®å®é¨ç¨ããã°ã©ã ãAndroidã§åãããã¨ãç®æ¨ã¨ãã¾ãã
ã¾ãä»åã¯ãã®å¯¾çææ³ã«ã¤ãã¦ãã説æããã¦ããã ãã¾ãã
ã»ããã¢ããæé
é©å½ã«ä½æãããã©ã«ãã«jniãã©ã«ããä½ãä¸è¨ã®Android.mkãApplication.mkãrandom_num.cãunrandom.cãmyrandom.cãä½æãã¦ãã ããã
AndroidStudioã®Terminalã§
cd ä½æãããã©ã«ã
ndk-build
ã§ãã«ããåºæ¥ã¾ãã
libsãã©ã«ãã«x86ãarmeabi-v7aã®ãã¤ããªãããããã§ãã¾ãã®ã§ç°å¢ã«åããã¦ã³ãã³ãããã³ãããã
adb push (x86 or armãã¤ããªã®random_numã®ãã¹) /data/local/tmp adb push (x86 or armãã¤ããªã®libunrandomã®ãã¹) /data/local/tmp adb push (x86 or armãã¤ããªã®libmyrandomã®ãã¹) /data/local/tmp adb shell cd data/local/tmp chmod 755 random_num
Androidã«ãããLD_PRELOADã®è¨å®
1.端æ«ä¸ã§SELinuxãç¡å¹åã
2a.ã¢ããªã±ã¼ã·ã§ã³ã®å ´å:
setpropã§ã¢ããªã®ããã±ã¼ã¸ã«å¯¾ã㦠LD_PRELOADãè¨å®ããããã®æããã±ã¼ã¸åã«wrap.ãä»å ããã
ä¾ãã°å¯¾è±¡ã¢ããªã®ããã±ã¼ã¸åã"com.doraneko.SurvivalShooter"ãèªã¿è¾¼ã¾ãããsoãã¡ã¤ã«ã®ãã¹ã/data/local/tmp/libinject.soã®å ´å
setprop wrap.com.doraneko.SurvivalShooter LD_PRELOAD=/data/local/tmp/libinject.so
2b.å®è¡ãã¡ã¤ã«ã®å ´å:
ä¾ãã°å®è¡ãã¡ã¤ã«åãhelloãèªã¿è¾¼ã¾ãããsoãã¡ã¤ã«ã®ãã¹ãdata/local/tmp/libinject.soã®å ´å
LD_PRELOAD=./data/local/tmp/libinject.so ./hello
å®é¨
ããã§ã¯å®éã«random_numã«å¯¾ãã¦libunrandom.soãlibmyrandom.soãLD_PRELOADãç¨ãã¦ã¤ã³ã¸ã§ã¯ã·ã§ã³ãã¦ã¿ã¾ãã
LD_PRELOAD=./data/local/tmp/libunrandom.so ./random_num LD_PRELOAD=./data/local/tmp/libmyrandom.so ./random_num
è¦äºã«randomé¢æ°ãã¤ã³ã¸ã§ã¯ã·ã§ã³ããsoãã¡ã¤ã«ã®é¢æ°ã«ç½®æããã¦ãã¾ããã¾ãç»åã®ããã«soãã¡ã¤ã«ãã¤ã³ã¸ã§ã¯ã·ã§ã³ããã¦ãããã¨ãåããã¾ãã
ãã®ããã«æ¢åã®é¢æ°ãç½®æãããå±éºæ§ãããã¨ã¨ãã«ãsoãã¡ã¤ã«ã®ã¤ã³ã¸ã§ã¯ã·ã§ã³èªä½ããã®ä»ã®æ»æã®èµ·ç¹ã«ããªãããå¯è½æ§ãããã¾ãã
ä¸è¬çãªãã¼ãææ³ã§ã¯GameGuardianãgdbãFridaãªã©ãç¨ãã¦ptraceã§ã®å¯¾è±¡ããã»ã¹ã¸ã®ã¢ã¿ãããå¿
è¦ã¨ãªãã¾ãã
ãããLD_PRELOADæ»æã®å ´åptraceã§ã®ã¢ã¿ãããä¼´ããsoãã¡ã¤ã«ã®ã¤ã³ã¸ã§ã¯ã·ã§ã³ãé¢æ°ã®ç½®æãå¯è½ã¨ãªãã¾ãã
常ã«ããããæ»æææ³ãåå¨ããã¨ãããã¨ã念é ã«ç½®ãã¦ãé²å¾¡é¢ãèãã¦ããå¿
è¦ããããã¨æããã¾ãã
ã½ã¼ã¹ã³ã¼ã
â»AndroidStudioã§ndkãã«ãããã¨ã½ã¼ã¹ã³ã¼ãä¸ã§randé¢æ°ã§è¨è¿°ãã¦ãlrand48é¢æ°ã§ã³ã³ãã¤ã«ããããããªã®ã§ãunrandom.cãmyrandom.cã§ã¯ç½®æãã¹ãé¢æ°ã¨ãã¦lrand48ã§è¨è¿°ãã¦ãã¾ãã
Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_CFLAGS += -fPIE LOCAL_LDFLAGS += -fPIE -pie LOCAL_MODULE := random_num LOCAL_SRC_FILES := random_num.c include $(BUILD_EXECUTABLE) ############################################ include $(CLEAR_VARS) LOCAL_MODULE_FILENAME:= libunrandom LOCAL_MODULE := unrandom LOCAL_SRC_FILES := unrandom.c include $(BUILD_SHARED_LIBRARY) ############################################ include $(CLEAR_VARS) LOCAL_MODULE_FILENAME:= libmyrandom LOCAL_MODULE := myrandom LOCAL_SRC_FILES := myrandom.c include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_ABI := x86 armeabi-v7a
random_num.c
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ srand(time(NULL)); int i = 10; while(i--) printf("%d\n",rand()); return 0; }
unrandom.c
int lrand48(){ return 42; //the most random number in the universe }
myrandom.c
#define _GNU_SOURCE #include <dlfcn.h> #include <stdio.h> typedef int (*orig_rand_f_type)(); int lrand48(){ printf("custom rand is called\n"); orig_rand_f_type orig_rand; orig_rand = (orig_rand_f_type)dlsym(RTLD_NEXT, "lrand48"); return orig_rand(); }
é²å¾¡ææ³
é常ã®æ¹æ³ã§ã¢ããªã±ã¼ã·ã§ã³ãå®è¡ããå ´åã親ããã»ã¹ã¯zygoteã«ãªãã¾ãã
USER PID PPID VSIZE RSS WCHAN PC NAME root 1841 1 1611188 125080 0 b7700c60 S zygote ...... u0_a48 4267 1841 1112668 145184 0 b7700c60 S com.doraneko.SurvivalShooter
ãããLD_PRELOADãè¨å®ããç¶æ ã§ã¢ããªãèµ·åããã¨è¦ªã¯zygoteã§ãªããªãã¾ãã(å®éã¯/system/bin/sh)
USER PID PPID VSIZE RSS WCHAN PC NAME root 1841 1 1611188 125080 0 b7700c60 S zygote ........ u0_a48 4569 4557 1125376 189684 0 b7720c60 S com.doraneko.SurvivalShooter
ã¤ã¾ã対çã®ä¸ã¤ã®ææ³ã¨ãã¦ã¯è¦ªããã»ã¹ãzygoteã§ããã確èªããæ¹æ³ãæå¹ã§ãã
ãã ããã§æ³¨æããªãã¦ã¯ãããªãã®ã¯ããã§ãã¯å¦çã§æ¨æºé¢æ°ã使ç¨ããã¨ããèªä½æ»æè
ã«ãã£ã¦ç½®æããã¦ãã¾ãå¯è½æ§ãããã¨ãããã¨ã§ãã
å®éä¸ã¯æ»æææ³ã«å¯¾ãã対çã ãã§ãªãããã®å¯¾çã®åé¿æ¹æ³ã«å¯¾ããæ´ãªã対çãªã©ä½éã«ãé²å¾¡ãã¦ããå¿
è¦ãããã¾ãã
æå¾ã«å¯¾è±¡ã¢ããªã±ã¼ã·ã§ã³ã®éåä¿¡é¢æ°ãLD_PRELOADã§ç½®æãã¦ãHTTPéä¿¡ã®å
容ãlogcatã§åºåããããã°ã©ã ãä½æãã¾ããã
LD_PRELOADã«ããHTTPéä¿¡ãã£ããã£
注æäºé
æ¬ã¬ãã¼ãã«è¨è¼ããã¦ããå
容ã許å¯ããã¦ããªãã½ããã¦ã§ã¢ã§è¡ãã¨ãå ´åã«ãã£ã¦ã¯ç¯ç½ªè¡çºã¨ãªãå¯è½æ§ãããã¾ãããã®ãããè¨äºã®å
容ã試ãéã«ã¯è¨±å¯ãããã½ããã¦ã§ã¢ã«å¯¾ãã¦ã®ã¿å®æ½ããããã«ãã¦ãã ããã
æ¬ã¬ãã¼ãã«ã¤ãã¦
ãåãåã
E-mail:[email protected]
æ ªå¼ä¼ç¤¾Ninjastarsã¨ã³ã¸ãã¢
ä¸ç¬å¥äºé