diff --git a/PermissionManager/.gitignore b/PermissionManager/.gitignore new file mode 100644 index 00000000..aa724b77 --- /dev/null +++ b/PermissionManager/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/PermissionManager/.idea/.gitignore b/PermissionManager/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/PermissionManager/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/PermissionManager/.idea/compiler.xml b/PermissionManager/.idea/compiler.xml new file mode 100644 index 00000000..fb7f4a8a --- /dev/null +++ b/PermissionManager/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/PermissionManager/.idea/gradle.xml b/PermissionManager/.idea/gradle.xml new file mode 100644 index 00000000..526b4c25 --- /dev/null +++ b/PermissionManager/.idea/gradle.xml @@ -0,0 +1,20 @@ + + + + + + + \ No newline at end of file diff --git a/PermissionManager/.idea/misc.xml b/PermissionManager/.idea/misc.xml new file mode 100644 index 00000000..10a0ff7f --- /dev/null +++ b/PermissionManager/.idea/misc.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/PermissionManager/Scrooge b/PermissionManager/Scrooge new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/PermissionManager/Scrooge @@ -0,0 +1 @@ + diff --git a/PermissionManager/app/.gitignore b/PermissionManager/app/.gitignore new file mode 100644 index 00000000..42afabfd --- /dev/null +++ b/PermissionManager/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/PermissionManager/app/appKey.jks b/PermissionManager/app/appKey.jks new file mode 100644 index 00000000..95ea5aef Binary files /dev/null and b/PermissionManager/app/appKey.jks differ diff --git a/PermissionManager/app/build.gradle b/PermissionManager/app/build.gradle new file mode 100644 index 00000000..02da6ec2 --- /dev/null +++ b/PermissionManager/app/build.gradle @@ -0,0 +1,53 @@ +plugins { + id 'com.android.application' +} + +android { + compileSdk 31 + + defaultConfig { + applicationId "com.linux.permissionmanager" + minSdk 26 + targetSdk 31 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + externalNativeBuild { + cmake { + cppFlags '-std=c++17' + abiFilters "arm64-v8a" //需要什么构架的so,就在这边添加即 + } + } + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + externalNativeBuild { + cmake { + path file('src/main/cpp/CMakeLists.txt') + version '3.18.1' + } + } + buildFeatures { + viewBinding true + } +} + +dependencies { + + implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'com.google.android.material:material:1.5.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' +} \ No newline at end of file diff --git a/PermissionManager/app/proguard-rules.pro b/PermissionManager/app/proguard-rules.pro new file mode 100644 index 00000000..481bb434 --- /dev/null +++ b/PermissionManager/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/PermissionManager/app/src/androidTest/java/com/linux/permissionmanager/ExampleInstrumentedTest.java b/PermissionManager/app/src/androidTest/java/com/linux/permissionmanager/ExampleInstrumentedTest.java new file mode 100644 index 00000000..e63884ad --- /dev/null +++ b/PermissionManager/app/src/androidTest/java/com/linux/permissionmanager/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.linux.permissionmanager; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.linux.permissionmanager", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/PermissionManager/app/src/main/AndroidManifest.xml b/PermissionManager/app/src/main/AndroidManifest.xml new file mode 100644 index 00000000..ac7de7ed --- /dev/null +++ b/PermissionManager/app/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/PermissionManager/app/src/main/cpp/CMakeLists.txt b/PermissionManager/app/src/main/cpp/CMakeLists.txt new file mode 100644 index 00000000..23b18751 --- /dev/null +++ b/PermissionManager/app/src/main/cpp/CMakeLists.txt @@ -0,0 +1,54 @@ +# For more information about using CMake with Android Studio, read the +# documentation: https://d.android.com/studio/projects/add-native-code.html + +# Sets the minimum version of CMake required to build the native library. + +cmake_minimum_required(VERSION 3.18.1) + +# Declares and names the project. + +project("permissionmanager") + +# Creates and names a library, sets it as either STATIC +# or SHARED, and provides the relative paths to its source code. +# You can define multiple libraries, and CMake builds them for you. +# Gradle automatically packages shared libraries with your APK. + +add_library( # Sets the name of the library. + permissionmanager + + # Sets the library as a shared library. + SHARED + + # Provides a relative path to your source file(s). + ../../../../../testRoot/jni/kernel_root_kit/kernel_root_kit_process64_inject.cpp + ../../../../../testRoot/jni/kernel_root_kit/kernel_root_kit_ptrace_arm64_utils.cpp + ../../../../../testRoot/jni/kernel_root_kit/kernel_root_kit_su_install_helper.cpp + ../../../../../testRoot/jni/kernel_root_kit/kernel_root_kit_parasite_app.cpp + ../../../../../testRoot/jni/kernel_root_kit/kernel_root_kit_parasite_patch_elf.cpp + cJSON.c + root.cpp) + +# Searches for a specified prebuilt library and stores the path as a +# variable. Because CMake includes system libraries in the search path by +# default, you only need to specify the name of the public NDK library +# you want to add. CMake verifies that the library exists before +# completing its build. + +find_library( # Sets the name of the path variable. + log-lib + + # Specifies the name of the NDK library that + # you want CMake to locate. + log) + +# Specifies libraries CMake should link to your target library. You +# can link multiple libraries, such as libraries you define in this +# build script, prebuilt third-party libraries, or system libraries. + +target_link_libraries( # Specifies the target library. + permissionmanager + + # Links the target library to the log library + # included in the NDK. + ${log-lib}) \ No newline at end of file diff --git a/PermissionManager/app/src/main/cpp/cJSON.c b/PermissionManager/app/src/main/cpp/cJSON.c new file mode 100644 index 00000000..c62e9eed --- /dev/null +++ b/PermissionManager/app/src/main/cpp/cJSON.c @@ -0,0 +1,750 @@ +/* + Copyright (c) 2009 Dave Gamble + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/* cJSON */ +/* JSON parser in C. */ + +#include +#include +#include +#include +#include +#include +#include +#include "cJSON.h" + +static const char *ep; + +const char *cJSON_GetErrorPtr(void) {return ep;} + +static int cJSON_strcasecmp(const char *s1,const char *s2) +{ + if (!s1) return (s1==s2)?0:1;if (!s2) return 1; + for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0; + return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2); +} + +static void *(*cJSON_malloc)(size_t sz) = malloc; +static void (*cJSON_free)(void *ptr) = free; + +static char* cJSON_strdup(const char* str) +{ + size_t len; + char* copy; + + len = strlen(str) + 1; + if (!(copy = (char*)cJSON_malloc(len))) return 0; + memcpy(copy,str,len); + return copy; +} + +void cJSON_InitHooks(cJSON_Hooks* hooks) +{ + if (!hooks) { /* Reset hooks */ + cJSON_malloc = malloc; + cJSON_free = free; + return; + } + + cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc; + cJSON_free = (hooks->free_fn)?hooks->free_fn:free; +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(void) +{ + cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON)); + if (node) memset(node,0,sizeof(cJSON)); + return node; +} + +/* Delete a cJSON structure. */ +void cJSON_Delete(cJSON *c) +{ + cJSON *next; + while (c) + { + next=c->next; + if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child); + if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring); + if (!(c->type&cJSON_StringIsConst) && c->string) cJSON_free(c->string); + cJSON_free(c); + c=next; + } +} + +/* Parse the input text to generate a number, and populate the result into item. */ +static const char *parse_number(cJSON *item,const char *num) +{ + long long n=0,sign=1,scale=0;long long subscale=0,signsubscale=1,temp = 0; + + if (*num=='-') sign=-1,num++; /* Has sign? */ + if (*num=='0') num++; /* is zero */ + if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */ + if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */ + if (*num=='e' || *num=='E') /* Exponent? */ + { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */ + while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */ + } + temp = n; + n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */ + + item->valuedouble=(double)n; //convert double + item->valueint= temp; //convert long long + item->type=cJSON_Number; + return num; +} + +static int pow2gt (int x) { --x; x|=x>>1; x|=x>>2; x|=x>>4; x|=x>>8; x|=x>>16; return x+1; } + +typedef struct {char *buffer; int length; int offset; } printbuffer; + +static char* ensure(printbuffer *p,int needed) +{ + char *newbuffer;int newsize; + if (!p || !p->buffer) return 0; + needed+=p->offset; + if (needed<=p->length) return p->buffer+p->offset; + + newsize=pow2gt(needed); + newbuffer=(char*)cJSON_malloc(newsize); + if (!newbuffer) {cJSON_free(p->buffer);p->length=0,p->buffer=0;return 0;} + if (newbuffer) memcpy(newbuffer,p->buffer,p->length); + cJSON_free(p->buffer); + p->length=newsize; + p->buffer=newbuffer; + return newbuffer+p->offset; +} + +static int update(printbuffer *p) +{ + char *str; + if (!p || !p->buffer) return 0; + str=p->buffer+p->offset; + return p->offset+strlen(str); +} + +/* Render the number nicely from the given item into a string. */ +static char *print_number(cJSON *item,printbuffer *p) +{ + char *str=0; + double d=item->valuedouble; + if (d==0) + { + if (p) str=ensure(p,2); + else str=(char*)cJSON_malloc(2); /* special case for 0. */ + if (str) strcpy(str,"0"); + } + else if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN) + { + if (p) str=ensure(p,21); + else str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */ + if (str) sprintf(str,"%lld",item->valueint); + } + else + { + if (p) str=ensure(p,64); + else str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */ + if (str) + { + if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d); + else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d); + else sprintf(str,"%f",d); + } + } + return str; +} + +static unsigned parse_hex4(const char *str) +{ + unsigned h=0; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + h=h<<4;str++; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + h=h<<4;str++; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + h=h<<4;str++; + if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0; + return h; +} + +/* Parse the input text into an unescaped cstring, and populate item. */ +static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; +static const char *parse_string(cJSON *item,const char *str) +{ + const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2; + if (*str!='\"') {ep=str;return 0;} /* not a string! */ + + while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */ + + out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */ + if (!out) return 0; + + ptr=str+1;ptr2=out; + while (*ptr!='\"' && *ptr) + { + if (*ptr!='\\') *ptr2++=*ptr++; + else + { + ptr++; + switch (*ptr) + { + case 'b': *ptr2++='\b'; break; + case 'f': *ptr2++='\f'; break; + case 'n': *ptr2++='\n'; break; + case 'r': *ptr2++='\r'; break; + case 't': *ptr2++='\t'; break; + case 'u': /* transcode utf16 to utf8. */ + uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */ + + if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; /* check for invalid. */ + + if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */ + { + if (ptr[1]!='\\' || ptr[2]!='u') break; /* missing second-half of surrogate. */ + uc2=parse_hex4(ptr+3);ptr+=6; + if (uc2<0xDC00 || uc2>0xDFFF) break; /* invalid second-half of surrogate. */ + uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF)); + } + + len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len; + + switch (len) { + case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; + case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; + case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6; + case 1: *--ptr2 =(uc | firstByteMark[len]); + } + ptr2+=len; + break; + default: *ptr2++=*ptr; break; + } + ptr++; + } + } + *ptr2=0; + if (*ptr=='\"') ptr++; + item->valuestring=out; + item->type=cJSON_String; + return ptr; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static char *print_string_ptr(const char *str,printbuffer *p) +{ + const char *ptr;char *ptr2,*out;int len=0,flag=0;unsigned char token; + + for (ptr=str;*ptr;ptr++) flag|=((*ptr>0 && *ptr<32)||(*ptr=='\"')||(*ptr=='\\'))?1:0; + if (!flag) + { + len=ptr-str; + if (p) out=ensure(p,len+3); + else out=(char*)cJSON_malloc(len+3); + if (!out) return 0; + ptr2=out;*ptr2++='\"'; + strcpy(ptr2,str); + ptr2[len]='\"'; + ptr2[len+1]=0; + return out; + } + + if (!str) + { + if (p) out=ensure(p,3); + else out=(char*)cJSON_malloc(3); + if (!out) return 0; + strcpy(out,"\"\""); + return out; + } + ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;} + + if (p) out=ensure(p,len+3); + else out=(char*)cJSON_malloc(len+3); + if (!out) return 0; + + ptr2=out;ptr=str; + *ptr2++='\"'; + while (*ptr) + { + if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++; + else + { + *ptr2++='\\'; + switch (token=*ptr++) + { + case '\\': *ptr2++='\\'; break; + case '\"': *ptr2++='\"'; break; + case '\b': *ptr2++='b'; break; + case '\f': *ptr2++='f'; break; + case '\n': *ptr2++='n'; break; + case '\r': *ptr2++='r'; break; + case '\t': *ptr2++='t'; break; + default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */ + } + } + } + *ptr2++='\"';*ptr2++=0; + return out; +} +/* Invote print_string_ptr (which is useful) on an item. */ +static char *print_string(cJSON *item,printbuffer *p) {return print_string_ptr(item->valuestring,p);} + +/* Predeclare these prototypes. */ +static const char *parse_value(cJSON *item,const char *value); +static char *print_value(cJSON *item,int depth,int fmt,printbuffer *p); +static const char *parse_array(cJSON *item,const char *value); +static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p); +static const char *parse_object(cJSON *item,const char *value); +static char *print_object(cJSON *item,int depth,int fmt,printbuffer *p); + +/* Utility to jump whitespace and cr/lf */ +static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;} + +/* Parse an object - create a new root, and populate. */ +cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated) +{ + const char *end=0; + cJSON *c=cJSON_New_Item(); + ep=0; + if (!c) return 0; /* memory fail */ + + end=parse_value(c,skip(value)); + if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */ + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}} + if (return_parse_end) *return_parse_end=end; + return c; +} +/* Default options for cJSON_Parse */ +cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);} + +/* Render a cJSON item/entity/structure to text. */ +char *cJSON_Print(cJSON *item) {return print_value(item,0,1,0);} +char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0,0);} + +char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt) +{ + printbuffer p; + p.buffer=(char*)cJSON_malloc(prebuffer); + p.length=prebuffer; + p.offset=0; + return print_value(item,0,fmt,&p); + return p.buffer; +} + + +/* Parser core - when encountering text, process appropriately. */ +static const char *parse_value(cJSON *item,const char *value) +{ + if (!value) return 0; /* Fail on null. */ + if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; } + if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; } + if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; } + if (*value=='\"') { return parse_string(item,value); } + if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); } + if (*value=='[') { return parse_array(item,value); } + if (*value=='{') { return parse_object(item,value); } + + ep=value;return 0; /* failure. */ +} + +/* Render a value to text. */ +static char *print_value(cJSON *item,int depth,int fmt,printbuffer *p) +{ + char *out=0; + if (!item) return 0; + if (p) + { + switch ((item->type)&255) + { + case cJSON_NULL: {out=ensure(p,5); if (out) strcpy(out,"null"); break;} + case cJSON_False: {out=ensure(p,6); if (out) strcpy(out,"false"); break;} + case cJSON_True: {out=ensure(p,5); if (out) strcpy(out,"true"); break;} + case cJSON_Number: out=print_number(item,p);break; + case cJSON_String: out=print_string(item,p);break; + case cJSON_Array: out=print_array(item,depth,fmt,p);break; + case cJSON_Object: out=print_object(item,depth,fmt,p);break; + } + } + else + { + switch ((item->type)&255) + { + case cJSON_NULL: out=cJSON_strdup("null"); break; + case cJSON_False: out=cJSON_strdup("false");break; + case cJSON_True: out=cJSON_strdup("true"); break; + case cJSON_Number: out=print_number(item,0);break; + case cJSON_String: out=print_string(item,0);break; + case cJSON_Array: out=print_array(item,depth,fmt,0);break; + case cJSON_Object: out=print_object(item,depth,fmt,0);break; + } + } + return out; +} + +/* Build an array from input text. */ +static const char *parse_array(cJSON *item,const char *value) +{ + cJSON *child; + if (*value!='[') {ep=value;return 0;} /* not an array! */ + + item->type=cJSON_Array; + value=skip(value+1); + if (*value==']') return value+1; /* empty array. */ + + item->child=child=cJSON_New_Item(); + if (!item->child) return 0; /* memory fail */ + value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */ + if (!value) return 0; + + while (*value==',') + { + cJSON *new_item; + if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */ + child->next=new_item;new_item->prev=child;child=new_item; + value=skip(parse_value(child,skip(value+1))); + if (!value) return 0; /* memory fail */ + } + + if (*value==']') return value+1; /* end of array */ + ep=value;return 0; /* malformed. */ +} + +/* Render an array to text */ +static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p) +{ + char **entries; + char *out=0,*ptr,*ret;int len=5; + cJSON *child=item->child; + int numentries=0,i=0,fail=0; + size_t tmplen=0; + + /* How many entries in the array? */ + while (child) numentries++,child=child->next; + /* Explicitly handle numentries==0 */ + if (!numentries) + { + if (p) out=ensure(p,3); + else out=(char*)cJSON_malloc(3); + if (out) strcpy(out,"[]"); + return out; + } + + if (p) + { + /* Compose the output array. */ + i=p->offset; + ptr=ensure(p,1);if (!ptr) return 0; *ptr='['; p->offset++; + child=item->child; + while (child && !fail) + { + print_value(child,depth+1,fmt,p); + p->offset=update(p); + if (child->next) {len=fmt?2:1;ptr=ensure(p,len+1);if (!ptr) return 0;*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;p->offset+=len;} + child=child->next; + } + ptr=ensure(p,2);if (!ptr) return 0; *ptr++=']';*ptr=0; + out=(p->buffer)+i; + } + else + { + /* Allocate an array to hold the values for each */ + entries=(char**)cJSON_malloc(numentries*sizeof(char*)); + if (!entries) return 0; + memset(entries,0,numentries*sizeof(char*)); + /* Retrieve all the results: */ + child=item->child; + while (child && !fail) + { + ret=print_value(child,depth+1,fmt,0); + entries[i++]=ret; + if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1; + child=child->next; + } + + /* If we didn't fail, try to malloc the output string */ + if (!fail) out=(char*)cJSON_malloc(len); + /* If that fails, we fail. */ + if (!out) fail=1; + + /* Handle failure. */ + if (fail) + { + for (i=0;itype=cJSON_Object; + value=skip(value+1); + if (*value=='}') return value+1; /* empty array. */ + + item->child=child=cJSON_New_Item(); + if (!item->child) return 0; + value=skip(parse_string(child,skip(value))); + if (!value) return 0; + child->string=child->valuestring;child->valuestring=0; + if (*value!=':') {ep=value;return 0;} /* fail! */ + value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */ + if (!value) return 0; + + while (*value==',') + { + cJSON *new_item; + if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */ + child->next=new_item;new_item->prev=child;child=new_item; + value=skip(parse_string(child,skip(value+1))); + if (!value) return 0; + child->string=child->valuestring;child->valuestring=0; + if (*value!=':') {ep=value;return 0;} /* fail! */ + value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */ + if (!value) return 0; + } + + if (*value=='}') return value+1; /* end of array */ + ep=value;return 0; /* malformed. */ +} + +/* Render an object to text. */ +static char *print_object(cJSON *item,int depth,int fmt,printbuffer *p) +{ + char **entries=0,**names=0; + char *out=0,*ptr,*ret,*str;int len=7,i=0,j; + cJSON *child=item->child; + int numentries=0,fail=0; + size_t tmplen=0; + /* Count the number of entries. */ + while (child) numentries++,child=child->next; + /* Explicitly handle empty object case */ + if (!numentries) + { + if (p) out=ensure(p,fmt?depth+4:3); + else out=(char*)cJSON_malloc(fmt?depth+4:3); + if (!out) return 0; + ptr=out;*ptr++='{'; + if (fmt) {*ptr++='\n';for (i=0;ioffset; + len=fmt?2:1; ptr=ensure(p,len+1); if (!ptr) return 0; + *ptr++='{'; if (fmt) *ptr++='\n'; *ptr=0; p->offset+=len; + child=item->child;depth++; + while (child) + { + if (fmt) + { + ptr=ensure(p,depth); if (!ptr) return 0; + for (j=0;joffset+=depth; + } + print_string_ptr(child->string,p); + p->offset=update(p); + + len=fmt?2:1; + ptr=ensure(p,len); if (!ptr) return 0; + *ptr++=':';if (fmt) *ptr++='\t'; + p->offset+=len; + + print_value(child,depth,fmt,p); + p->offset=update(p); + + len=(fmt?1:0)+(child->next?1:0); + ptr=ensure(p,len+1); if (!ptr) return 0; + if (child->next) *ptr++=','; + if (fmt) *ptr++='\n';*ptr=0; + p->offset+=len; + child=child->next; + } + ptr=ensure(p,fmt?(depth+1):2); if (!ptr) return 0; + if (fmt) for (i=0;ibuffer)+i; + } + else + { + /* Allocate space for the names and the objects */ + entries=(char**)cJSON_malloc(numentries*sizeof(char*)); + if (!entries) return 0; + names=(char**)cJSON_malloc(numentries*sizeof(char*)); + if (!names) {cJSON_free(entries);return 0;} + memset(entries,0,sizeof(char*)*numentries); + memset(names,0,sizeof(char*)*numentries); + + /* Collect all the results into our arrays: */ + child=item->child;depth++;if (fmt) len+=depth; + while (child) + { + names[i]=str=print_string_ptr(child->string,0); + entries[i++]=ret=print_value(child,depth,fmt,0); + if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1; + child=child->next; + } + + /* Try to allocate the output string */ + if (!fail) out=(char*)cJSON_malloc(len); + if (!out) fail=1; + + /* Handle failure */ + if (fail) + { + for (i=0;ichild;int i=0;while(c)i++,c=c->next;return i;} +cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;} +cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;} +/* Utility for handling references. */ +static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;} + +/* Add item to array/object. */ +void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}} +void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);} +void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (!(item->type&cJSON_StringIsConst) && item->string) cJSON_free(item->string);item->string=(char*)string;item->type|=cJSON_StringIsConst;cJSON_AddItemToArray(object,item);} +void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));} +void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));} + +cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0; + if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;} +void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));} +cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;} +void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));} + +/* Replace array/object items with new ones. */ +void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) {cJSON_AddItemToArray(array,newitem);return;} + newitem->next=c;newitem->prev=c->prev;c->prev=newitem;if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;} +void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return; + newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem; + if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);} +void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}} + +/* Create basic types: */ +cJSON *cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;} +cJSON *cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;} +cJSON *cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;} +cJSON *cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;} +cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(long long)num;}return item;} +cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;} +cJSON *cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;} +cJSON *cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;} + +/* Create Arrays: */ +cJSON *cJSON_CreateIntArray(const int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} +cJSON *cJSON_CreateFloatArray(const float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} +cJSON *cJSON_CreateDoubleArray(const double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} +cJSON *cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && ichild=n;else suffix_object(p,n);p=n;}return a;} + +/* Duplication */ +cJSON *cJSON_Duplicate(cJSON *item,int recurse) +{ + cJSON *newitem,*cptr,*nptr=0,*newchild; + /* Bail on bad ptr */ + if (!item) return 0; + /* Create new item */ + newitem=cJSON_New_Item(); + if (!newitem) return 0; + /* Copy over all vars */ + newitem->type=item->type&(~cJSON_IsReference),newitem->valueint=item->valueint,newitem->valuedouble=item->valuedouble; + if (item->valuestring) {newitem->valuestring=cJSON_strdup(item->valuestring); if (!newitem->valuestring) {cJSON_Delete(newitem);return 0;}} + if (item->string) {newitem->string=cJSON_strdup(item->string); if (!newitem->string) {cJSON_Delete(newitem);return 0;}} + /* If non-recursive, then we're done! */ + if (!recurse) return newitem; + /* Walk the ->next chain for the child. */ + cptr=item->child; + while (cptr) + { + newchild=cJSON_Duplicate(cptr,1); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) {cJSON_Delete(newitem);return 0;} + if (nptr) {nptr->next=newchild,newchild->prev=nptr;nptr=newchild;} /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + else {newitem->child=newchild;nptr=newchild;} /* Set newitem->child and move to it */ + cptr=cptr->next; + } + return newitem; +} + +void cJSON_Minify(char *json) +{ + char *into=json; + while (*json) + { + if (*json==' ') json++; + else if (*json=='\t') json++; /* Whitespace characters. */ + else if (*json=='\r') json++; + else if (*json=='\n') json++; + else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; /* double-slash comments, to end of line. */ + else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} /* multiline comments. */ + else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} /* string literals, which are \" sensitive. */ + else *into++=*json++; /* All other characters. */ + } + *into=0; /* and null-terminate. */ +} diff --git a/PermissionManager/app/src/main/cpp/cJSON.h b/PermissionManager/app/src/main/cpp/cJSON.h new file mode 100644 index 00000000..f61a9af0 --- /dev/null +++ b/PermissionManager/app/src/main/cpp/cJSON.h @@ -0,0 +1,149 @@ +/* + Copyright (c) 2009 Dave Gamble + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef cJSON__h +#define cJSON__h +#include +#ifdef __cplusplus +extern "C" +{ +#endif + +/* cJSON Types: */ +#define cJSON_False 0 +#define cJSON_True 1 +#define cJSON_NULL 2 +#define cJSON_Number 3 +#define cJSON_String 4 +#define cJSON_Array 5 +#define cJSON_Object 6 + +#define cJSON_IsReference 256 +#define cJSON_StringIsConst 512 + +/* The cJSON structure: */ +typedef struct cJSON { + struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ + struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ + + int type; /* The type of the item, as above. */ + + char *valuestring; /* The item's string, if type==cJSON_String */ + long long valueint; /* The item's number, if type==cJSON_Number */ + double valuedouble; /* The item's number, if type==cJSON_Number */ + + char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ +} cJSON; + +typedef struct cJSON_Hooks { + void *(*malloc_fn)(size_t sz); + void (*free_fn)(void *ptr); +} cJSON_Hooks; + +/* Supply malloc, realloc and free functions to cJSON */ +extern void cJSON_InitHooks(cJSON_Hooks* hooks); + + +/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */ +extern cJSON *cJSON_Parse(const char *value); +/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */ +extern char *cJSON_Print(cJSON *item); +/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */ +extern char *cJSON_PrintUnformatted(cJSON *item); +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ +extern char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt); +/* Delete a cJSON entity and all subentities. */ +extern void cJSON_Delete(cJSON *c); + +/* Returns the number of items in an array (or object). */ +extern int cJSON_GetArraySize(cJSON *array); +/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */ +extern cJSON *cJSON_GetArrayItem(cJSON *array,int item); +/* Get item "string" from object. Case insensitive. */ +extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string); + +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ +extern const char *cJSON_GetErrorPtr(void); + +/* These calls create a cJSON item of the appropriate type. */ +extern cJSON *cJSON_CreateNull(void); +extern cJSON *cJSON_CreateTrue(void); +extern cJSON *cJSON_CreateFalse(void); +extern cJSON *cJSON_CreateBool(int b); +extern cJSON *cJSON_CreateNumber(double num); +extern cJSON *cJSON_CreateString(const char *string); +extern cJSON *cJSON_CreateArray(void); +extern cJSON *cJSON_CreateObject(void); + +/* These utilities create an Array of count items. */ +extern cJSON *cJSON_CreateIntArray(const int *numbers,int count); +extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count); +extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count); +extern cJSON *cJSON_CreateStringArray(const char **strings,int count); + +/* Append item to the specified array/object. */ +extern void cJSON_AddItemToArray(cJSON *array, cJSON *item); +extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item); +extern void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */ +/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ +extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item); + +/* Remove/Detatch items from Arrays/Objects. */ +extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which); +extern void cJSON_DeleteItemFromArray(cJSON *array,int which); +extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string); +extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string); + +/* Update array items. */ +extern void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem); /* Shifts pre-existing items to the right. */ +extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem); +extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); + +/* Duplicate a cJSON item */ +extern cJSON *cJSON_Duplicate(cJSON *item,int recurse); +/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will +need to be released. With recurse!=0, it will duplicate any children connected to the item. +The item->next and ->prev pointers are always zero on return from Duplicate. */ + +/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ +extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated); + +extern void cJSON_Minify(char *json); + +/* Macros for creating things quickly. */ +#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) +#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) +#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse()) +#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) +#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) +#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) + +/* When assigning an integer value, it needs to be propagated to valuedouble too. */ +#define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)) +#define cJSON_SetNumberValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/PermissionManager/app/src/main/cpp/root.cpp b/PermissionManager/app/src/main/cpp/root.cpp new file mode 100644 index 00000000..ed0da3b5 --- /dev/null +++ b/PermissionManager/app/src/main/cpp/root.cpp @@ -0,0 +1,385 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../../../../testRoot/jni/kernel_root_kit/kernel_root_kit_umbrella.h" +#include "../../../../../testRoot/jni/testRoot.h" +#include "urlEncodeUtils.h" +#include "cJSON.h" +using namespace std; + +std::string g_last_su_full_path; + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_testRoot( + JNIEnv* env, + jobject /* this */, + jstring rootKey) { + const char *str1 = env->GetStringUTFChars(rootKey, 0); + string strRootKey= str1; + env->ReleaseStringUTFChars(rootKey, str1); + + std::string result; + kernel_root::fork_pipe_info finfo; + ssize_t err = 0; + if(fork_pipe_child_process(finfo)) { + err = kernel_root::get_root(strRootKey.c_str()); + result = "getRoot:"; + result += std::to_string(err); + result += "\n\n"; + if(err == 0) { + result += get_capability_info(); + result += "\n\n"; + } + write_errcode_from_child(finfo, err); + write_string_from_child(finfo, result); + _exit(0); + return 0; + } + err = 0; + if(!wait_fork_child_process(finfo)) { + err = -1120001; + } else { + if(!read_errcode_from_child(finfo, err)) { + err = -1120002; + } else if(!read_string_from_child(finfo, result)) { + err = -1120003; + } + } + return env->NewStringUTF(result.c_str()); +} + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_runRootCmd( + JNIEnv* env, + jobject /* this */, + jstring rootKey, + jstring cmd) { + const char *str1 = env->GetStringUTFChars(rootKey, 0); + string strRootKey= str1; + env->ReleaseStringUTFChars(rootKey, str1); + + str1 = env->GetStringUTFChars(cmd, 0); + string strCmd= str1; + env->ReleaseStringUTFChars(cmd, str1); + + + ssize_t err; + string result = kernel_root::run_root_cmd(strRootKey.c_str(), strCmd.c_str(), err); + stringstream sstr; + sstr << "runRootCmd err:" << err << ", result:" << result; + return env->NewStringUTF(sstr.str().c_str()); +} + + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_runInit64ProcessCmd( + JNIEnv* env, + jobject /* this */, + jstring rootKey, + jstring cmd) { + const char *str1 = env->GetStringUTFChars(rootKey, 0); + string strRootKey= str1; + env->ReleaseStringUTFChars(rootKey, str1); + + str1 = env->GetStringUTFChars(cmd, 0); + string strCmd= str1; + env->ReleaseStringUTFChars(cmd, str1); + + + ssize_t err; + string result = kernel_root::safe_run_init64_cmd_wrapper(strRootKey.c_str(), strCmd.c_str(), err); + + stringstream sstr; + sstr << "runInit64Cmd err:" << err << ", result:" << result; + return env->NewStringUTF(sstr.str().c_str()); +} + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_installSu( + JNIEnv* env, + jobject /* this */, + jstring rootKey, + jstring basePath) { + + const char *str1 = env->GetStringUTFChars(rootKey, 0); + string strRootKey= str1; + env->ReleaseStringUTFChars(rootKey, str1); + + str1 = env->GetStringUTFChars(basePath, 0); + string strBasePath= str1; + env->ReleaseStringUTFChars(basePath, str1); + + stringstream sstr; + //安装su工具套件 + ssize_t err; + std::string su_hide_full_path = kernel_root::safe_install_su(strRootKey.c_str(), strBasePath.c_str(), err); + sstr << "install su err:" << err<<", su_hide_full_path:" << su_hide_full_path << std::endl; + g_last_su_full_path = su_hide_full_path; + if (err == 0) { + sstr << "installSu done."<< std::endl; + } + return env->NewStringUTF(sstr.str().c_str()); +} + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_getLastInstallSuFullPath( + JNIEnv* env, + jobject /* this */) { + return env->NewStringUTF(g_last_su_full_path.c_str()); +} + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_uninstallSu( + JNIEnv* env, + jobject /* this */, + jstring rootKey, + jstring basePath) { + + const char *str1 = env->GetStringUTFChars(rootKey, 0); + string strRootKey= str1; + env->ReleaseStringUTFChars(rootKey, str1); + + str1 = env->GetStringUTFChars(basePath, 0); + string strBasePath= str1; + env->ReleaseStringUTFChars(basePath, str1); + + stringstream sstr; + + ssize_t err = kernel_root::safe_uninstall_su(strRootKey.c_str(), strBasePath.c_str()); + sstr << "uninstallSu err:" << err << std::endl; + if (err != 0) { + return env->NewStringUTF(sstr.str().c_str()); + } + g_last_su_full_path.clear(); + sstr << "uninstallSu done."; + return env->NewStringUTF(sstr.str().c_str()); +} + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_autoSuEnvInject( + JNIEnv* env, + jobject /* this */, + jstring rootKey, + jstring targetProcessCmdline) { + + if(g_last_su_full_path.empty()) { + return env->NewStringUTF("【错误】请先安装部署su"); + } + const char *str1 = env->GetStringUTFChars(rootKey, 0); + string strRootKey= str1; + env->ReleaseStringUTFChars(rootKey, str1); + + str1 = env->GetStringUTFChars(targetProcessCmdline, 0); + string strTargetProcessCmdline = str1; + env->ReleaseStringUTFChars(targetProcessCmdline, str1); + + stringstream sstr; + + //杀光所有历史进程 + std::set out; + ssize_t err = kernel_root::safe_find_all_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), out); + sstr << "find_all_cmdline_process err:"<< err<<", cnt:"<NewStringUTF(sstr.str().c_str()); + } + std::string kill_cmd; + for (pid_t t : out) { + err = kernel_root::safe_kill_process(strRootKey.c_str(), t); + sstr << "kill_ret err:"<< err << std::endl; + if (err != 0) { + return env->NewStringUTF(sstr.str().c_str()); + } + } + pid_t pid; + err = kernel_root::safe_wait_and_find_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), 60*1000, pid); + + std::string folder_path = g_last_su_full_path; + int n = folder_path.find_last_of("/"); + if(n != -1) { + folder_path = folder_path.substr(0,n); + } + sstr << "autoSuEnvInject("<< err<<", " << folder_path <<")" << std::endl; + if (err != 0) { + return env->NewStringUTF(sstr.str().c_str()); + } + err = kernel_root::safe_inject_process_env64_PATH_wrapper(strRootKey.c_str(), pid, folder_path.c_str()); + sstr << "autoSuEnvInject ret val:" << err << std::endl; + if (err != 0) { + return env->NewStringUTF(sstr.str().c_str()); + } + sstr << "autoSuEnvInject done."; + return env->NewStringUTF(sstr.str().c_str()); +} + + + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_getAllCmdlineProcess( + JNIEnv* env, + jobject /* this */, + jstring rootKey) { + std::stringstream ss; + + const char *str1 = env->GetStringUTFChars(rootKey, 0); + std::string strRootKey = str1; + env->ReleaseStringUTFChars(rootKey, str1); + + std::map pid_map; + ssize_t err = kernel_root::safe_get_all_cmdline_process(strRootKey.c_str(), pid_map); + if(err != 0) { + ss << "get_all_cmdline_process err:"<< err<< std::endl; + return env->NewStringUTF(ss.str().c_str()); + } + cJSON *root = cJSON_CreateArray(); + for (auto iter = pid_map.begin(); iter != pid_map.end(); iter++ ) { + cJSON *item = cJSON_CreateObject(); + size_t len = iter->second.length(); + size_t max_encoded_len = 3 * len + 1; + shared_ptr spData(new (std::nothrow) char[max_encoded_len], std::default_delete()); + memset(spData.get(), 0, max_encoded_len); + url_encode(const_cast(iter->second.c_str()), spData.get()); + cJSON_AddNumberToObject(item, "pid", iter->first); + cJSON_AddStringToObject(item, "name", spData.get()); + cJSON_AddItemToArray(root, item); + } + ss << cJSON_Print(root); + cJSON_Delete(root); + return env->NewStringUTF(ss.str().c_str()); +} + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_parasitePrecheckApp( + JNIEnv* env, + jobject /* this */, + jstring rootKey, + jstring targetProcessCmdline) { + + const char *str1 = env->GetStringUTFChars(rootKey, 0); + string strRootKey= str1; + env->ReleaseStringUTFChars(rootKey, str1); + + str1 = env->GetStringUTFChars(targetProcessCmdline, 0); + string strTargetProcessCmdline = str1; + env->ReleaseStringUTFChars(targetProcessCmdline, str1); + + stringstream sstr; + std::set test_pid; + ssize_t err = kernel_root::safe_find_all_cmdline_process(strRootKey.c_str(), strTargetProcessCmdline.c_str(), test_pid); + if (err != 0) { + sstr << "find_all_cmdline_process err:"<< err<<", cnt:"<< test_pid.size() << std::endl; + return env->NewStringUTF(sstr.str().c_str()); + } + if (test_pid.size() == 0) { + sstr << "目标进程不存在" << std::endl; + return env->NewStringUTF(sstr.str().c_str()); + } + + std::map so_path_list; + err = kernel_root::safe_parasite_precheck_app(strRootKey.c_str(), strTargetProcessCmdline.c_str(), so_path_list); + if (err) { + sstr << "parasite_precheck_app ret val:" << err << std::endl; + if(err == -9904) { + sstr << "此目标APP为32位应用,无法寄生" << err << std::endl; + } + return env->NewStringUTF(sstr.str().c_str()); + } + + if (!so_path_list.size()) { + sstr << "无法检测到目标APP的JNI环境,目标APP暂不可被寄生;您可重新运行目标APP后重试;或将APP进行手动加固(加壳),因为加固(加壳)APP后,APP会被产生JNI环境,方可寄生!" << std::endl; + return env->NewStringUTF(sstr.str().c_str()); + } + + cJSON *root = cJSON_CreateArray(); + for (auto iter = so_path_list.begin(); iter != so_path_list.end(); iter++) { + cJSON *item = cJSON_CreateObject(); + size_t len = iter->first.length(); + size_t max_encoded_len = 3 * len + 1; + std::shared_ptr spData(new (std::nothrow) char[max_encoded_len], std::default_delete()); + memset(spData.get(), 0, max_encoded_len); + url_encode(const_cast(iter->first.c_str()), spData.get()); + cJSON_AddStringToObject(item, "name", spData.get()); + cJSON_AddNumberToObject(item, "status", iter->second); + cJSON_AddItemToArray(root, item); + } + sstr << cJSON_Print(root); + cJSON_Delete(root); + return env->NewStringUTF(sstr.str().c_str()); + +} + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_parasiteImplantApp( + JNIEnv* env, + jobject /* this */, + jstring rootKey, + jstring targetProcessCmdline, + jstring targetSoFullPath) { + + const char *str1 = env->GetStringUTFChars(rootKey, 0); + string strRootKey= str1; + env->ReleaseStringUTFChars(rootKey, str1); + + str1 = env->GetStringUTFChars(targetProcessCmdline, 0); + string strTargetProcessCmdline = str1; + env->ReleaseStringUTFChars(targetProcessCmdline, str1); + + str1 = env->GetStringUTFChars(targetSoFullPath, 0); + string strTargetSoFullPath = str1; + env->ReleaseStringUTFChars(targetSoFullPath, str1); + + stringstream sstr; + ssize_t err = kernel_root::safe_parasite_implant_app(strRootKey.c_str(), strTargetProcessCmdline.c_str(), strTargetSoFullPath.c_str()); + if (err != 0) { + sstr << "parasite_implant_app err:"<< err << std::endl; + return env->NewStringUTF(sstr.str().c_str()); + } + sstr << "parasiteImplantApp done."; + return env->NewStringUTF(sstr.str().c_str()); + +} + + +extern "C" JNIEXPORT jstring JNICALL +Java_com_linux_permissionmanager_MainActivity_parasiteImplantSuEnv( + JNIEnv* env, + jobject /* this */, + jstring rootKey, + jstring targetProcessCmdline, + jstring targetSoFullPath) { + if(g_last_su_full_path.empty()) { + return env->NewStringUTF("【错误】请先安装部署su"); + } + + const char *str1 = env->GetStringUTFChars(rootKey, 0); + string strRootKey= str1; + env->ReleaseStringUTFChars(rootKey, str1); + + str1 = env->GetStringUTFChars(targetProcessCmdline, 0); + string strTargetProcessCmdline = str1; + env->ReleaseStringUTFChars(targetProcessCmdline, str1); + + str1 = env->GetStringUTFChars(targetSoFullPath, 0); + string strTargetSoFullPath = str1; + env->ReleaseStringUTFChars(targetSoFullPath, str1); + + std::string folder = g_last_su_full_path; + int n = folder.find_last_of("/"); + folder = folder.substr(0, n); + + stringstream sstr; + ssize_t err = kernel_root::safe_parasite_implant_su_env(strRootKey.c_str(), strTargetProcessCmdline.c_str(), strTargetSoFullPath.c_str(), folder); + if (err != 0) { + sstr << "parasite_implant_su_env err:"<< err << std::endl; + return env->NewStringUTF(sstr.str().c_str()); + } + sstr << "parasiteImplantSuEnv done."; + return env->NewStringUTF(sstr.str().c_str()); + +} + diff --git a/PermissionManager/app/src/main/cpp/urlEncodeUtils.h b/PermissionManager/app/src/main/cpp/urlEncodeUtils.h new file mode 100644 index 00000000..0d1482f7 --- /dev/null +++ b/PermissionManager/app/src/main/cpp/urlEncodeUtils.h @@ -0,0 +1,74 @@ +#ifndef URL_ENCODE_UTILS_H_ +#define URL_ENCODE_UTILS_H_ +#include +#include + +static inline char to_hex(char code) { + static char hex[] = "0123456789ABCDEF"; + return hex[code & 15]; +} +static inline char from_hex(char ch) { + return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10; +} + +/* +//使用例子 +int main() { + char str[] = "你好,世界"; + char encoded_str[256]; + url_encode(str, encoded_str); + printf("Encoded URL: %s\n", encoded_str); + return 0; +} +*/ +static void url_encode(char *str, char *encoded_str) { + char *pstr = str, *buf = encoded_str; + while (*pstr) { + unsigned char c = *pstr; + if (c <= 0x7F) { // ASCII + if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { + *buf++ = c; + } else if (c == ' ') { + *buf++ = '+'; + } else { + *buf++ = '%', *buf++ = to_hex(c >> 4), *buf++ = to_hex(c & 15); + } + } else { // Non-ASCII + while (c) { + *buf++ = '%', *buf++ = to_hex(c >> 4), *buf++ = to_hex(c & 15); + c = *(++pstr); + } + continue; + } + pstr++; + } + *buf = '\0'; +} +/* +//使用例子 +int main() { + char url[] = "%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C"; // "你好,世界"的URL编码 + char decoded_str[256]; + url_decode(url, decoded_str); + printf("Decoded URL: %s\n", decoded_str); + return 0; +} +*/ +static void url_decode(char *str, char *decoded_str) { + char *pstr = str, *buf = decoded_str; + while (*pstr) { + if (*pstr == '%') { + if (pstr[1] && pstr[2]) { + *buf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]); + pstr += 2; + } + } else if (*pstr == '+') { + *buf++ = ' '; + } else { + *buf++ = *pstr; + } + pstr++; + } + *buf = '\0'; +} +#endif \ No newline at end of file diff --git a/PermissionManager/app/src/main/java/com/linux/permissionmanager/Adapter/SelectAppRecyclerAdapter.java b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Adapter/SelectAppRecyclerAdapter.java new file mode 100644 index 00000000..8713f088 --- /dev/null +++ b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Adapter/SelectAppRecyclerAdapter.java @@ -0,0 +1,119 @@ +package com.linux.permissionmanager.Adapter; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.os.Handler; +import android.os.Message; +import android.text.Html; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.PopupWindow; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.linux.permissionmanager.Model.SelectAppRecyclerItem; +import com.linux.permissionmanager.R; + +import java.util.ArrayList; +import java.util.List; + +public class SelectAppRecyclerAdapter extends RecyclerView.Adapter { + public static class ViewHolder extends RecyclerView.ViewHolder { + public View v; + public ImageView select_app_icon; + public TextView select_app_text; + public TextView select_package_name; + public ViewHolder(View v) { + super(v); + this.v = v; + } + + } + + private int resourceId; + private List objects; + private PopupWindow popupWindow; + private Handler selectAppCallback; + private Context ctx; + public SelectAppRecyclerAdapter(Context ctx, int textViewResourceId, List objects, PopupWindow popupWindow, Handler selectAppCallback) { + this.resourceId = textViewResourceId; + this.objects = new ArrayList<>(objects); + this. popupWindow = popupWindow; + this. selectAppCallback = selectAppCallback; + this. ctx = ctx; + } + + public void updateList(List newList) { + objects.clear(); + objects.addAll(newList); + notifyDataSetChanged(); + } + + + public List getList() { + return objects; + } + + + @NonNull + @Override + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View v =LayoutInflater.from(parent.getContext()).inflate(resourceId,parent,false); + SelectAppRecyclerAdapter.ViewHolder holder = new SelectAppRecyclerAdapter.ViewHolder(v); + + holder.select_app_icon = v.findViewById(R.id.select_app_icon); + holder.select_app_text=v.findViewById(R.id.select_app_text) ; + holder.select_package_name=v.findViewById(R.id.select_package_name) ; + return holder; + } + + + @Override + public void onBindViewHolder(@NonNull SelectAppRecyclerAdapter.ViewHolder holder, int position) { + SelectAppRecyclerItem appItem=objects.get(position); + String showName = appItem.getShowName(ctx); + String packageName = appItem.getPackageName(); + Drawable icon = appItem.getDrawable(ctx); + + //图标+进程PID+名字+内存 + holder.select_app_icon.setImageDrawable(icon); + String showText=""+showName +" " + +" "+" ("+packageName+")"+""; + + holder.select_app_text.setText(Html.fromHtml(showText)); + holder.select_package_name.setText(packageName); + + //item被点击 + holder.v.setOnClickListener(new ClickRecyclerItemListener(appItem)); + } + + + @Override + public int getItemCount() { + return objects.size(); + } + + @Override + public int getItemViewType(int position) { + return position; + } + + + class ClickRecyclerItemListener implements View.OnClickListener { + SelectAppRecyclerItem appItem; + public ClickRecyclerItemListener( SelectAppRecyclerItem appItem){ + this.appItem =appItem; + } + @Override + public void onClick(View v) { + popupWindow.dismiss(); + Message msg = new Message(); + msg.obj = (SelectAppRecyclerItem)appItem; + selectAppCallback.sendMessage(msg); + } + } +} diff --git a/PermissionManager/app/src/main/java/com/linux/permissionmanager/Adapter/SelectFileRecyclerAdapter.java b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Adapter/SelectFileRecyclerAdapter.java new file mode 100644 index 00000000..a39437ba --- /dev/null +++ b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Adapter/SelectFileRecyclerAdapter.java @@ -0,0 +1,109 @@ +package com.linux.permissionmanager.Adapter; + +import android.content.Context; +import android.graphics.Color; +import android.os.Handler; +import android.os.Message; +import android.text.Html; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.PopupWindow; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import com.linux.permissionmanager.Model.SelectFileRecyclerItem; +import com.linux.permissionmanager.R; + +import java.util.ArrayList; +import java.util.List; +public class SelectFileRecyclerAdapter extends RecyclerView.Adapter { + public static class ViewHolder extends RecyclerView.ViewHolder { + public View v; + public TextView select_file_name; + public TextView select_file_desc; + public ViewHolder(View v) { + super(v); + this.v = v; + } + } + private int resourceId; + private List objects; + private PopupWindow popupWindow; + private Handler selectFileCallback; + private Context ctx; + public SelectFileRecyclerAdapter(Context ctx, int textViewResourceId, List objects, PopupWindow popupWindow, Handler selectFileCallback) { + this.resourceId = textViewResourceId; + this.objects = new ArrayList<>(objects); + this. popupWindow = popupWindow; + this. selectFileCallback = selectFileCallback; + this. ctx = ctx; + } + + public void updateList(List newList) { + objects.clear(); + objects.addAll(newList); + notifyDataSetChanged(); + } + + + public List getList() { + return objects; + } + + + @NonNull + @Override + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View v =LayoutInflater.from(parent.getContext()).inflate(resourceId,parent,false); + SelectFileRecyclerAdapter.ViewHolder holder = new SelectFileRecyclerAdapter.ViewHolder(v); + + holder.select_file_name=v.findViewById(R.id.select_file_name) ; + holder.select_file_desc=v.findViewById(R.id.select_file_desc) ; + return holder; + } + + @Override + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { + SelectFileRecyclerItem fileItem=objects.get(position); + String fileName = fileItem.getFileName(); + String fileDesc = fileItem.getFileDesc(); + Color fileDescColor = fileItem.getFileDescColor(); + String hexColor = String.format("#%06X", (0xFFFFFF & fileDescColor.toArgb())); + + String showText=""+fileName +" "; + String showDesc=""+fileDesc +" "; + holder.select_file_name.setText(Html.fromHtml(showText)); + holder.select_file_desc.setText(Html.fromHtml(showDesc)); + + holder.v.setOnClickListener(new ClickRecyclerItemListener(fileItem)); + } + + @Override + public int getItemCount() { + return objects.size(); + } + + @Override + public int getItemViewType(int position) { + return position; + } + + + class ClickRecyclerItemListener implements View.OnClickListener { + SelectFileRecyclerItem fileItem; + public ClickRecyclerItemListener(SelectFileRecyclerItem fileItem){ + this.fileItem =fileItem; + } + @Override + public void onClick(View v) { + popupWindow.dismiss(); + Message msg = new Message(); + msg.obj = (SelectFileRecyclerItem)fileItem; + selectFileCallback.sendMessage(msg); + } + } +} \ No newline at end of file diff --git a/PermissionManager/app/src/main/java/com/linux/permissionmanager/MainActivity.java b/PermissionManager/app/src/main/java/com/linux/permissionmanager/MainActivity.java new file mode 100644 index 00000000..17177031 --- /dev/null +++ b/PermissionManager/app/src/main/java/com/linux/permissionmanager/MainActivity.java @@ -0,0 +1,778 @@ +package com.linux.permissionmanager; + +import android.annotation.SuppressLint; +import android.app.AlertDialog; +import android.app.ProgressDialog; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.Gravity; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.EditText; +import android.widget.LinearLayout; +import android.widget.PopupWindow; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.linux.permissionmanager.Adapter.SelectAppRecyclerAdapter; +import com.linux.permissionmanager.Adapter.SelectFileRecyclerAdapter; +import com.linux.permissionmanager.Model.PopupWindowOnTouchClose; +import com.linux.permissionmanager.Model.SelectAppRecyclerItem; +import com.linux.permissionmanager.Model.SelectFileRecyclerItem; +import com.linux.permissionmanager.Utils.ScreenInfoUtils; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.net.URLDecoder; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +public class MainActivity extends AppCompatActivity implements View.OnClickListener { + private String rootKey = "bz4kKoPVSAG1tnwlcs1PJ1qp6HtVymj60CoTgsjmMd1UALve"; + private String suBasePath = "/data/local/tmp"; + private String lastInputCmd = "id"; + private SharedPreferences m_shareSave; + private ProgressDialog m_loadingDlg = null; + private final String[] RECOMMEND_FILES = {"libc++_shared.so"}; + + static { + System.loadLibrary("permissionmanager"); + } + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + m_shareSave = getSharedPreferences("zhcs", Context.MODE_PRIVATE); + try { + rootKey = m_shareSave.getString("rootKey", rootKey); + } catch (Exception e) { + } + try { + lastInputCmd = m_shareSave.getString("lastInputCmd", lastInputCmd); + } catch (Exception e) { + } + + showUserInputRootKeyDlg(); + + Button test_root_btn = findViewById(R.id.test_root_btn); + Button run_root_cmd_btn = findViewById(R.id.run_root_cmd_btn); + Button run_init_process_cmd_btn = findViewById(R.id.run_init_process_cmd_btn); + Button su_env_install_btn = findViewById(R.id.su_env_install_btn); + Button su_env_inject_btn = findViewById(R.id.su_env_inject_btn); + Button clean_su_btn = findViewById(R.id.clean_su_btn); + Button implant_app_btn = findViewById(R.id.implant_app_btn); + Button copy_info_btn = findViewById(R.id.copy_info_btn); + Button clean_info_btn = findViewById(R.id.clean_info_btn); + + test_root_btn.setOnClickListener(this); + run_root_cmd_btn.setOnClickListener(this); + run_init_process_cmd_btn.setOnClickListener(this); + su_env_install_btn.setOnClickListener(this); + su_env_inject_btn.setOnClickListener(this); + clean_su_btn.setOnClickListener(this); + implant_app_btn.setOnClickListener(this); + copy_info_btn.setOnClickListener(this); + clean_info_btn.setOnClickListener(this); + + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.test_root_btn: + appendConsoleMsg(testRoot(rootKey)); + break; + case R.id.run_root_cmd_btn: + showUserInputRootCmdDlg(false); + break; + case R.id.run_init_process_cmd_btn: + showUserInputRootCmdDlg(true); + break; + case R.id.su_env_install_btn: + onClickSuEnvInstallBtn(); + break; + case R.id.su_env_inject_btn: + showSelectSuInjectModeDlg(); + break; + case R.id.clean_su_btn: + appendConsoleMsg(uninstallSu(rootKey,suBasePath)); + break; + case R.id.implant_app_btn: + onClickImplantAppBtn(); + break; + case R.id.copy_info_btn: + copyConsoleMsg(); + break; + case R.id.clean_info_btn: + cleanConsoleMsg(); + break; + default: + break; + } + } + public void showUserInputRootKeyDlg() { + Handler inputCallback = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + String inputTxt = (String)msg.obj; + rootKey = inputTxt; + SharedPreferences.Editor mEdit = m_shareSave.edit(); + mEdit.putString("rootKey", rootKey); + mEdit.commit(); + super.handleMessage(msg); + } + }; + showInputDlg(rootKey,"请输入ROOT权限的KEY", inputCallback); + } + + public void showUserInputRootCmdDlg(boolean isKernelCmd) { + Handler inputCallback = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + String inputTxt = (String)msg.obj; + lastInputCmd = inputTxt; + SharedPreferences.Editor mEdit = m_shareSave.edit(); + mEdit.putString("lastInputCmd", lastInputCmd); + mEdit.commit(); + appendConsoleMsg(inputTxt + "\n" + + (isKernelCmd ? runInit64ProcessCmd(rootKey, inputTxt) : runRootCmd(rootKey, inputTxt))); + super.handleMessage(msg); + } + }; + + showInputDlg(lastInputCmd, isKernelCmd ? "输入ROOT命令" : "输入普通命令", inputCallback); + } + + public void showInputDlg(String defaultText, String title, Handler confirmCallback) { + final EditText inputTxt = new EditText(MainActivity.this); + inputTxt.setText(defaultText); + inputTxt.setFocusable(true); + inputTxt.setSelection(defaultText.length(), 0); + AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); + builder.setTitle(title).setIcon(android.R.drawable.ic_dialog_info).setView(inputTxt) + .setNegativeButton("取消", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + String text = inputTxt.getText().toString(); + Message msg = new Message(); + msg.obj = text; + confirmCallback.sendMessage(msg); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + + private void showMsgDlg(String title, String msg, Drawable icon) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(title); + builder.setMessage(msg); + if(icon != null) { + builder.setIcon(icon); + } + builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + + public void onClickSuEnvInstallBtn() { + //1.安装su工具 + String insRet = installSu(rootKey, suBasePath); + appendConsoleMsg(insRet); + if(insRet.indexOf("installSu done.") != -1) { + //2.复制su的路径到剪贴板 + String suFullPath = getLastInstallSuFullPath(); + appendConsoleMsg("lastInstallSuFullPath:" + suFullPath); + showMsgDlg("温馨提示", + "安装部署su成功,su路径已复制到剪贴板。", null); + copyEditText(suFullPath); + appendConsoleMsg("安装部署su成功,su路径已复制到剪贴板"); + } + } + + private void suTempInject() { + Handler selectInjectSuAppCallback = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + + SelectAppRecyclerItem appItem = (SelectAppRecyclerItem) msg.obj; + + if (m_loadingDlg == null) { + m_loadingDlg = new ProgressDialog(MainActivity.this); + m_loadingDlg.setCancelable(false); + } + m_loadingDlg.setTitle(""); + m_loadingDlg.setMessage("请现在手动启动APP [" + appItem.getShowName(MainActivity.this) + "]"); + m_loadingDlg.show(); + + new Thread() { + public void run() { + String autoSuEnvInjectRet = autoSuEnvInject(rootKey, appItem.getPackageName()); + runOnUiThread(new Runnable() { + public void run() { + appendConsoleMsg(autoSuEnvInjectRet); + m_loadingDlg.cancel(); + + if(autoSuEnvInjectRet.indexOf("autoSuEnvInject done.")!= -1) { + showMsgDlg("提示", + "已授予ROOT权限至APP [" + appItem.getShowName(MainActivity.this) + "]", + appItem.getDrawable(MainActivity.this)); + } + + } + }); + } + }.start(); + super.handleMessage(msg); + } + }; + showSelectAppDlg(selectInjectSuAppCallback); + } + + private void suForeverInject() { + Handler selectImplantSuEnvCallback = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + SelectAppRecyclerItem appItem = (SelectAppRecyclerItem) msg.obj; + if (m_loadingDlg == null) { + m_loadingDlg = new ProgressDialog(MainActivity.this); + m_loadingDlg.setCancelable(false); + } + m_loadingDlg.setTitle(""); + m_loadingDlg.setMessage("请现在手动启动APP [" + appItem.getShowName(MainActivity.this) + "]"); + m_loadingDlg.show(); + new Thread() { + public void run() { + String parasitePrecheckAppRet = parasitePrecheckApp(rootKey, appItem.getPackageName()); + runOnUiThread(new Runnable() { + public void run() { + m_loadingDlg.cancel(); + Map fileList = parseSoFullPathInfo(parasitePrecheckAppRet); + if (fileList.size() == 0 && !parasitePrecheckAppRet.isEmpty()) { + appendConsoleMsg(parasitePrecheckAppRet); + return; + } + Handler selectFileCallback = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + SelectFileRecyclerItem fileItem = (SelectFileRecyclerItem) msg.obj; + String parasiteImplantSuEnvRet = parasiteImplantSuEnv(rootKey, appItem.getPackageName(), fileItem.getFilePath()); + appendConsoleMsg(parasiteImplantSuEnvRet); + if(parasiteImplantSuEnvRet.indexOf("parasiteImplantSuEnv done.")!= -1) { + showMsgDlg("提示", + "已永久寄生su环境至APP [" + appItem.getShowName(MainActivity.this) + "]", + appItem.getDrawable(MainActivity.this)); + } + super.handleMessage(msg); + } + }; + showSelectFileDlg(fileList, selectFileCallback); + } + }); + } + }.start(); + super.handleMessage(msg); + } + }; + View view = showSelectAppDlg(selectImplantSuEnvCallback); + CheckBox show_system_app_ckbox = view.findViewById(R.id.show_system_app_ckbox); + CheckBox show_thirty_app_ckbox = view.findViewById(R.id.show_thirty_app_ckbox); + CheckBox show_running_app_ckbox = view.findViewById(R.id.show_running_app_ckbox); + show_system_app_ckbox.setChecked(false); + show_system_app_ckbox.setEnabled(false); + + show_thirty_app_ckbox.setChecked(true); + show_thirty_app_ckbox.setEnabled(false); + + show_running_app_ckbox.setChecked(true); + show_running_app_ckbox.setEnabled(false); + } + + public void showSelectSuInjectModeDlg() { + final String[] items = {"临时授权su", "永久授权su"}; + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("请选择一个选项"); + builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + if(which == 0) { + suTempInject(); + } else if(which == 1) { + suForeverInject(); + } + } + }); + builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + AlertDialog dialog = builder.create(); + dialog.show(); + } + + public void onClickImplantAppBtn() { + showMsgDlg("建议", "为了实现最佳隐蔽性,推荐寄生到能常驻后台且联网的APP上,如音乐类、播放器类、运动类、广播类、社交聊天类APP", null); + Handler selectImplantAppCallback = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + SelectAppRecyclerItem appItem = (SelectAppRecyclerItem) msg.obj; + if (m_loadingDlg == null) { + m_loadingDlg = new ProgressDialog(MainActivity.this); + m_loadingDlg.setCancelable(false); + } + m_loadingDlg.setTitle(""); + m_loadingDlg.setMessage("请现在手动启动APP [" + appItem.getShowName(MainActivity.this) + "]"); + m_loadingDlg.show(); + new Thread() { + public void run() { + String parasitePrecheckAppRet = parasitePrecheckApp(rootKey, appItem.getPackageName()); + runOnUiThread(new Runnable() { + public void run() { + m_loadingDlg.cancel(); + Map fileList = parseSoFullPathInfo(parasitePrecheckAppRet); + if (fileList.size() == 0 && !parasitePrecheckAppRet.isEmpty()) { + appendConsoleMsg(parasitePrecheckAppRet); + return; + } + Handler selectFileCallback = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + SelectFileRecyclerItem fileItem = (SelectFileRecyclerItem) msg.obj; + String parasiteImplantAppRet = parasiteImplantApp(rootKey, appItem.getPackageName(), fileItem.getFilePath()); + appendConsoleMsg(parasiteImplantAppRet); + if(parasiteImplantAppRet.indexOf("parasiteImplantApp done.")!= -1) { + showMsgDlg("提示", + "已经寄生到APP [" + appItem.getShowName(MainActivity.this) + "]", + appItem.getDrawable(MainActivity.this)); + } + super.handleMessage(msg); + } + }; + showSelectFileDlg(fileList, selectFileCallback); + } + }); + } + }.start(); + super.handleMessage(msg); + } + }; + View view = showSelectAppDlg(selectImplantAppCallback); + CheckBox show_system_app_ckbox = view.findViewById(R.id.show_system_app_ckbox); + CheckBox show_thirty_app_ckbox = view.findViewById(R.id.show_thirty_app_ckbox); + CheckBox show_running_app_ckbox = view.findViewById(R.id.show_running_app_ckbox); + show_system_app_ckbox.setChecked(false); + show_system_app_ckbox.setEnabled(false); + show_thirty_app_ckbox.setChecked(true); + show_thirty_app_ckbox.setEnabled(false); + show_running_app_ckbox.setChecked(true); + show_running_app_ckbox.setEnabled(false); + } + + public void appendConsoleMsg(String msg) { + EditText console_edit = findViewById(R.id.console_edit); + StringBuffer txt = new StringBuffer(); + txt.append(console_edit.getText().toString()); + if (txt.length() != 0) { + txt.append("\n"); + } + txt.append(msg); + txt.append("\n"); + console_edit.setText(txt.toString()); + console_edit.setSelection(txt.length()); + } + + public void copyConsoleMsg() { + EditText edit = findViewById(R.id.console_edit); + copyEditText(edit.getText().toString()); + Toast.makeText(this, "复制成功", Toast.LENGTH_SHORT).show(); + } + + public void cleanConsoleMsg() { + EditText console_edit = findViewById(R.id.console_edit); + console_edit.setText(""); + } + + public void copyEditText(String text) { + ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + ClipData mClipData = ClipData.newPlainText("Label", text); + cm.setPrimaryClip(mClipData); + } + + public View showSelectAppDlg(Handler selectAppCallback) { + final PopupWindow popupWindow = new PopupWindow(this); + + View view = View.inflate(this, R.layout.select_app_wnd, null); + popupWindow.setContentView(view); + + popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT); + popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); + popupWindow.setBackgroundDrawable(new ColorDrawable(0x9B000000)); + popupWindow.setOutsideTouchable(true); + popupWindow.setFocusable(true); + popupWindow.setTouchable(true); + + //全屏 + View parent = View.inflate(MainActivity.this, R.layout.activity_main, null); + popupWindow.showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0); + popupWindow.showAsDropDown(parent, 0, 0); + + popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { + @Override + public void onDismiss() { + } + }); + + final int screenWidth = ScreenInfoUtils.getRealWidth(this); + final int screenHeight = ScreenInfoUtils.getRealHeight(this); + + final double centerWidth = ((double) screenWidth) * 0.80; + final double centerHeight = ((double) screenHeight) * 0.90; + + LinearLayout center_layout = (LinearLayout) view.findViewById(R.id.center_layout); + android.view.ViewGroup.LayoutParams lp = center_layout.getLayoutParams(); + lp.width = (int) centerWidth; + lp.height = (int) centerHeight; + + //点击阴影部分可关闭窗口 + popupWindow.setTouchInterceptor(new PopupWindowOnTouchClose(popupWindow, + screenWidth, screenHeight, (int) centerWidth, (int) centerHeight)); + + List appList = new ArrayList<>(); + List packages = getPackageManager().getInstalledPackages(0); + + for (int i = 0; i < packages.size(); i++) { + PackageInfo packageInfo = packages.get(i); + String packageName = packageInfo.applicationInfo.packageName; + if(packageName.equals(getPackageName())){ + continue; + } + appList.add(new SelectAppRecyclerItem(packageInfo)); + } + + SelectAppRecyclerAdapter adapter = new SelectAppRecyclerAdapter( + MainActivity.this, R.layout.select_app_recycler_item, appList, popupWindow, selectAppCallback); + RecyclerView select_app_recycler_view = (RecyclerView) view.findViewById(R.id.select_app_recycler_view); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); + linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); + select_app_recycler_view.setLayoutManager(linearLayoutManager); + select_app_recycler_view.setAdapter(adapter); + + // 获取正在运行的APP + String runningApp = getAllCmdlineProcess(rootKey); + Map processMap = parseProcessInfo(runningApp); + if(processMap.size() == 0 && !runningApp.isEmpty()) { + appendConsoleMsg(runningApp); + } + + TextView clear_search_btn = view.findViewById(R.id.clear_search_btn); + EditText search_edit = view.findViewById(R.id.search_edit); + CheckBox show_system_app_ckbox = view.findViewById(R.id.show_system_app_ckbox); + CheckBox show_thirty_app_ckbox = view.findViewById(R.id.show_thirty_app_ckbox); + CheckBox show_running_app_ckbox = view.findViewById(R.id.show_running_app_ckbox); + show_system_app_ckbox.setEnabled(true); + show_thirty_app_ckbox.setEnabled(true); + show_running_app_ckbox.setEnabled(true); + Map finalProcessMap = processMap; + @SuppressLint("HandlerLeak") Handler updateAppListFunc = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + List newAppList = new ArrayList<>(); + String filterText = search_edit.getText().toString(); + for(SelectAppRecyclerItem item : appList) { + PackageInfo pack = item.getPackageInfo(); + if(!show_system_app_ckbox.isChecked()) { + if ((pack.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { //系统应用 + continue; + } + } + if(!show_thirty_app_ckbox.isChecked()) { + if ((pack.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { //第三方应用 + continue; + } + } + if (show_running_app_ckbox.isChecked()) { + boolean isFound = finalProcessMap.values().stream() + .anyMatch(value -> value.contains(item.getPackageName())); + if (!isFound) { + continue; + } + } + if(item.getPackageName().indexOf(filterText) != -1 || item.getShowName(MainActivity.this).indexOf(filterText) != -1) { + newAppList.add(item); + } + } + adapter.updateList(newAppList); + super.handleMessage(msg); + } + }; + search_edit.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + String text = s.toString(); + if(text.length() > 0) { + clear_search_btn.setVisibility(View.VISIBLE); + } else { + clear_search_btn.setVisibility(View.GONE); + } + updateAppListFunc.sendMessage(new Message()); + } + @Override + public void afterTextChanged(Editable s) { + } + }); + clear_search_btn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + search_edit.setText(""); + } + }); + + show_system_app_ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + updateAppListFunc.sendMessage(new Message()); + } + }); + show_thirty_app_ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + updateAppListFunc.sendMessage(new Message()); + } + }); + show_running_app_ckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + updateAppListFunc.sendMessage(new Message()); + } + }); + updateAppListFunc.sendMessage(new Message()); + return view; + } + + private boolean checkIsRecommendFile(String filePath) { + Path path = Paths.get(filePath); + String fileName = path.getFileName().toString(); + for (String recommendFile : RECOMMEND_FILES) { + if (recommendFile.equals(fileName)) { + return true; + } + } + return false; + } + + public View showSelectFileDlg(Map filePath, Handler selectFileCallback) { + final PopupWindow popupWindow = new PopupWindow(this); + + View view = View.inflate(this, R.layout.select_file_wnd, null); + popupWindow.setContentView(view); + + popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT); + popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); + popupWindow.setBackgroundDrawable(new ColorDrawable(0x9B000000)); + popupWindow.setOutsideTouchable(true); + popupWindow.setFocusable(true); + popupWindow.setTouchable(true); + + //全屏 + View parent = View.inflate(MainActivity.this, R.layout.activity_main, null); + popupWindow.showAtLocation(parent, Gravity.NO_GRAVITY, 0, 0); + popupWindow.showAsDropDown(parent, 0, 0); + + popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { + @Override + public void onDismiss() { + } + }); + + final int screenWidth = ScreenInfoUtils.getRealWidth(this); + final int screenHeight = ScreenInfoUtils.getRealHeight(this); + + final double centerWidth = ((double) screenWidth) * 0.80; + final double centerHeight = ((double) screenHeight) * 0.90; + + LinearLayout center_layout = (LinearLayout) view.findViewById(R.id.center_layout); + android.view.ViewGroup.LayoutParams lp = center_layout.getLayoutParams(); + lp.width = (int) centerWidth; + lp.height = (int) centerHeight; + + //点击阴影部分可关闭窗口 + popupWindow.setTouchInterceptor(new PopupWindowOnTouchClose(popupWindow, + screenWidth, screenHeight, (int) centerWidth, (int) centerHeight)); + + List fileList = new ArrayList<>(); + for (Map.Entry entry : filePath.entrySet()) { + String strFilePath = entry.getKey(); + Integer status = entry.getValue(); + if(status != 1) { + continue; + } + String strFileDesc = checkIsRecommendFile(strFilePath) ? "(推荐,正在运行)" : "(正在运行)"; + fileList.add(new SelectFileRecyclerItem(strFilePath, strFileDesc, Color.valueOf(0xFFFFFFFF))); + } + for (Map.Entry entry : filePath.entrySet()) { + String strFilePath = entry.getKey(); + Integer status = entry.getValue(); + if(status != 2) { + continue; + } + String strFileDesc ="(未运行)"; + fileList.add(new SelectFileRecyclerItem(strFilePath, strFileDesc, Color.valueOf(Color.GRAY))); + } + + SelectFileRecyclerAdapter adapter = new SelectFileRecyclerAdapter( + MainActivity.this, R.layout.select_file_recycler_item, fileList, popupWindow, selectFileCallback); + RecyclerView select_file_recycler_view = (RecyclerView) view.findViewById(R.id.select_file_recycler_view); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); + linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); + select_file_recycler_view.setLayoutManager(linearLayoutManager); + select_file_recycler_view.setAdapter(adapter); + + TextView clear_search_btn = view.findViewById(R.id.clear_search_btn); + EditText search_edit = view.findViewById(R.id.search_edit); + + @SuppressLint("HandlerLeak") Handler updateFileListFunc = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + List newFileList = new ArrayList<>(); + String filterText = search_edit.getText().toString(); + for(SelectFileRecyclerItem item : fileList) { + String fileName = item.getFileName(); + if(fileName.indexOf(filterText) != -1) { + newFileList.add(item); + } + } + adapter.updateList(newFileList); + super.handleMessage(msg); + } + }; + search_edit.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + String text = s.toString(); + if(text.length() > 0) { + clear_search_btn.setVisibility(View.VISIBLE); + } else { + clear_search_btn.setVisibility(View.GONE); + } + updateFileListFunc.sendMessage(new Message()); + } + + @Override + public void afterTextChanged(Editable s) { + } + }); + clear_search_btn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + search_edit.setText(""); + } + }); + updateFileListFunc.sendMessage(new Message()); + return view; + } + + private Map parseProcessInfo(String jsonStr) { + Map processMap = new HashMap<>(); + try { + JSONArray jsonArray = new JSONArray(jsonStr); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + int pid = jsonObject.getInt("pid"); + String encodedValue = jsonObject.getString("name"); + String name = URLDecoder.decode(encodedValue, "UTF-8"); + processMap.put(pid, name); + } + } catch (Exception e) { + e.printStackTrace(); + } + return processMap; + } + + public Map parseSoFullPathInfo(String jsonStr) { + Map soPathMap = new HashMap<>(); + try { + JSONArray jsonArray = new JSONArray(jsonStr); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + String encodedValue = jsonObject.getString("name"); + String name = URLDecoder.decode(encodedValue, "UTF-8"); + int pid = jsonObject.getInt("status"); + soPathMap.put(name,pid); + } + } catch (Exception e) { + e.printStackTrace(); + } + return soPathMap; + } + + public native String testRoot(String rootKey); + + public native String runRootCmd(String rootKey, String cmd); + + public native String runInit64ProcessCmd(String rootKey, String cmd); + + public native String installSu(String rootKey, String basePath); + + public native String getLastInstallSuFullPath(); + + public native String uninstallSu(String rootKey, String basePath); + + public native String autoSuEnvInject(String rootKey, String targetProcessCmdline); + + public native String getAllCmdlineProcess(String rootKey); + + public native String parasitePrecheckApp(String rootKey, String targetProcessCmdline); + + public native String parasiteImplantApp(String rootKey, String targetProcessCmdline, String targetSoFullPath); + + public native String parasiteImplantSuEnv(String rootKey, String targetProcessCmdline, String targetSoFullPath); + +} \ No newline at end of file diff --git a/PermissionManager/app/src/main/java/com/linux/permissionmanager/Model/PopupWindowOnTouchClose.java b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Model/PopupWindowOnTouchClose.java new file mode 100644 index 00000000..ca4889d7 --- /dev/null +++ b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Model/PopupWindowOnTouchClose.java @@ -0,0 +1,48 @@ +package com.linux.permissionmanager.Model; + +import android.view.MotionEvent; +import android.view.View; +import android.widget.PopupWindow; + +public class PopupWindowOnTouchClose implements View.OnTouchListener { + private boolean lastVailedDown = true; + private int screenWidth, screenHeight, centerWidth, centerHeight; + private PopupWindow popupWindow; + + public PopupWindowOnTouchClose(PopupWindow popupWindow, int screenWidth, int screenHeight, int centerWidth, int centerHeight) { + this.popupWindow = popupWindow; + this.screenWidth = screenWidth; + this.screenHeight = screenHeight; + this.centerWidth = centerWidth; + this.centerHeight = centerHeight; + } + + private boolean isValiedRegion(View v, MotionEvent event) { + int x = (int) event.getX(); + int y = (int) event.getY(); + double wndLeft = (screenWidth - centerWidth) / 2; + double wndTop = (screenHeight - centerHeight) / 2; + if (x < wndLeft || x > wndLeft + centerWidth || y < wndTop || y > wndTop + centerHeight) { + return false; + } + return true; + } + + @Override + public boolean onTouch(View v, MotionEvent event) { + + if (event.getAction() == MotionEvent.ACTION_DOWN) { + lastVailedDown = isValiedRegion(v, event); + } else if (event.getAction() == MotionEvent.ACTION_UP) { + if (!lastVailedDown) { + if (!isValiedRegion(v, event)) { + popupWindow.dismiss(); + } + } + } + return false; + } +} + + + diff --git a/PermissionManager/app/src/main/java/com/linux/permissionmanager/Model/SelectAppRecyclerItem.java b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Model/SelectAppRecyclerItem.java new file mode 100644 index 00000000..3c6b4f82 --- /dev/null +++ b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Model/SelectAppRecyclerItem.java @@ -0,0 +1,31 @@ +package com.linux.permissionmanager.Model; + +import android.content.Context; +import android.content.pm.PackageInfo; +import android.graphics.drawable.Drawable; + +public class SelectAppRecyclerItem { + private PackageInfo packageInfo; + + public SelectAppRecyclerItem(PackageInfo packageInfo){ + this.packageInfo = packageInfo; + } + + public PackageInfo getPackageInfo() { + return packageInfo; + } + + public String getShowName(Context ctx) { + String showName = this.packageInfo.applicationInfo.loadLabel(ctx.getPackageManager()).toString(); + return showName; + } + public String getPackageName() { + String packageName = this.packageInfo.applicationInfo.packageName; + return packageName; + } + public Drawable getDrawable(Context ctx) { + Drawable icon = this.packageInfo.applicationInfo.loadIcon(ctx.getPackageManager()); + return icon; + } + +} diff --git a/PermissionManager/app/src/main/java/com/linux/permissionmanager/Model/SelectFileRecyclerItem.java b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Model/SelectFileRecyclerItem.java new file mode 100644 index 00000000..bf15017b --- /dev/null +++ b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Model/SelectFileRecyclerItem.java @@ -0,0 +1,37 @@ +package com.linux.permissionmanager.Model; + +import android.graphics.Color; +import android.os.Build; + +import androidx.annotation.RequiresApi; + +import java.nio.file.Path; +import java.nio.file.Paths; + +public class SelectFileRecyclerItem { + private String filePath; + private String fileDesc; + private Color fileDescColor; + + public SelectFileRecyclerItem(String filePath, String fileDesc, Color fileDescColor){ + this.filePath = filePath; + this.fileDesc = fileDesc; + this.fileDescColor = fileDescColor; + } + + public String getFilePath() { + return this.filePath; + } + + public String getFileName() { + Path path = Paths.get(filePath); + Path fileName = path.getFileName(); + return fileName.toString(); + } + public String getFileDesc() { + return this.fileDesc; + } + public Color getFileDescColor() { + return this.fileDescColor; + } +} diff --git a/PermissionManager/app/src/main/java/com/linux/permissionmanager/Utils/ScreenInfoUtils.java b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Utils/ScreenInfoUtils.java new file mode 100644 index 00000000..5e50159e --- /dev/null +++ b/PermissionManager/app/src/main/java/com/linux/permissionmanager/Utils/ScreenInfoUtils.java @@ -0,0 +1,187 @@ +package com.linux.permissionmanager.Utils; + +import android.app.Activity; +import android.content.Context; +import android.content.res.Resources; +import android.util.DisplayMetrics; +import android.util.Log; +import android.util.TypedValue; +import android.view.Display; +import android.view.WindowManager; + +/** + * Get Screen Information Utils + * + * @author yh + * @date 2018/12/18. + */ +public class ScreenInfoUtils { + + private static final String TAG = "ScreenInfoUtils"; + + /** + * Get Screen Width + */ + public static int getScreenWidth(Context context) { + return getDisplayMetrics(context).widthPixels; + } + + /** + * Get Screen Height + */ + public static int getScreenHeight(Context context) { + return getDisplayMetrics(context).heightPixels; + } + + + /** + * Get Screen Real Height + * + * @param context Context + * @return Real Height + */ + public static int getRealHeight(Context context) { + Display display = getDisplay(context); + if (display == null) { + return 0; + } + DisplayMetrics dm = new DisplayMetrics(); + display.getRealMetrics(dm); + return dm.heightPixels; + } + + /** + * Get Screen Real Width + * + * @param context Context + * @return Real Width + */ + public static int getRealWidth(Context context) { + Display display = getDisplay(context); + if (display == null) { + return 0; + } + DisplayMetrics dm = new DisplayMetrics(); + display.getRealMetrics(dm); + return dm.widthPixels; + } + + /** + * Get StatusBar Height + */ + public static int getStatusBarHeight(Context mContext) { + int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android"); + if (resourceId > 0) { + return mContext.getResources().getDimensionPixelSize(resourceId); + } + return 0; + } + + /** + * Get ActionBar Height + */ + public static int getActionBarHeight(Context mContext) { + TypedValue tv = new TypedValue(); + if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { + return TypedValue.complexToDimensionPixelSize(tv.data, mContext.getResources().getDisplayMetrics()); + } + return 0; + } + + /** + * Get NavigationBar Height + */ + public static int getNavigationBarHeight(Context mContext) { + Resources resources = mContext.getResources(); + int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); + if (resourceId > 0) { + return resources.getDimensionPixelSize(resourceId); + } + return 0; + } + + /** + * Get Density + */ + private static float getDensity(Context context) { + return getDisplayMetrics(context).density; + } + + /** + * Get Dpi + */ + private static int getDpi(Context context) { + return getDisplayMetrics(context).densityDpi; + } + + /** + * Get Display + * + * @param context Context for get WindowManager + * @return Display + */ + private static Display getDisplay(Context context) { + WindowManager wm; + if (context instanceof Activity) { + Activity activity = (Activity) context; + wm = activity.getWindowManager(); + } else { + wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + } + if (wm != null) { + return wm.getDefaultDisplay(); + } + return null; + } + + /** + * Get DisplayMetrics + * + * @param context Context for get Resources + * @return DisplayMetrics + */ + private static DisplayMetrics getDisplayMetrics(Context context) { + return context.getResources().getDisplayMetrics(); + } + + + /** + * Get ScreenInfo + */ + private static String getScreenInfo(Context context) { + return " \n" + + "--------ScreenInfo--------" + "\n" + + "Screen Width : " + getScreenWidth(context) + "px\n" + + "Screen RealWidth :" + getRealWidth(context) + "px\n" + + "Screen Height: " + getScreenHeight(context) + "px\n" + + "Screen RealHeight: " + getRealHeight(context) + "px\n" + + "Screen StatusBar Height: " + getStatusBarHeight(context)+ "px\n" + + "Screen ActionBar Height: " + getActionBarHeight(context)+ "px\n" + + "Screen NavigationBar Height: " + getNavigationBarHeight(context)+ "px\n" + + "Screen Dpi: " + getDpi(context) + "\n" + + "Screen Density: " + getDensity(context) + "\n" + + "--------------------------"; + } + + + /** + * 根据手机的分辨率从 dp 的单位 转成为 px(像素) + */ + public static int dip2px(Context context, float dpValue) { + final float scale = context.getResources().getDisplayMetrics().density; + return (int) (dpValue * scale); + } + + /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */ + public static int px2dip(Context context, float pxValue) { + final float scale = context.getResources().getDisplayMetrics().density; + return (int) (pxValue / scale); + } + + /** + * Print screenInfo to logcat + */ + public static void printScreenInfo(Context context) { + Log.d(TAG, getScreenInfo(context)); + } +} diff --git a/PermissionManager/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/PermissionManager/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 00000000..2b068d11 --- /dev/null +++ b/PermissionManager/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/PermissionManager/app/src/main/res/drawable/ic_launcher_background.xml b/PermissionManager/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 00000000..07d5da9c --- /dev/null +++ b/PermissionManager/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PermissionManager/app/src/main/res/drawable/line.xml b/PermissionManager/app/src/main/res/drawable/line.xml new file mode 100644 index 00000000..597142f2 --- /dev/null +++ b/PermissionManager/app/src/main/res/drawable/line.xml @@ -0,0 +1,12 @@ + + + + + + + \ No newline at end of file diff --git a/PermissionManager/app/src/main/res/drawable/line_drawable.xml b/PermissionManager/app/src/main/res/drawable/line_drawable.xml new file mode 100644 index 00000000..4f79f470 --- /dev/null +++ b/PermissionManager/app/src/main/res/drawable/line_drawable.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/PermissionManager/app/src/main/res/drawable/ripple_grey.xml b/PermissionManager/app/src/main/res/drawable/ripple_grey.xml new file mode 100644 index 00000000..ac736de4 --- /dev/null +++ b/PermissionManager/app/src/main/res/drawable/ripple_grey.xml @@ -0,0 +1,6 @@ + +//点击时波纹的颜色 + //未点击时控件的背景(可以是图片,可以是颜色,也可以是drawable里的xml背景(比如圆角)) + \ No newline at end of file diff --git a/PermissionManager/app/src/main/res/drawable/shape_wnd_grey_corner.xml b/PermissionManager/app/src/main/res/drawable/shape_wnd_grey_corner.xml new file mode 100644 index 00000000..6bc80841 --- /dev/null +++ b/PermissionManager/app/src/main/res/drawable/shape_wnd_grey_corner.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/PermissionManager/app/src/main/res/drawable/thumb.xml b/PermissionManager/app/src/main/res/drawable/thumb.xml new file mode 100644 index 00000000..0646431b --- /dev/null +++ b/PermissionManager/app/src/main/res/drawable/thumb.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/PermissionManager/app/src/main/res/drawable/thumb_drawable.xml b/PermissionManager/app/src/main/res/drawable/thumb_drawable.xml new file mode 100644 index 00000000..a93b8b83 --- /dev/null +++ b/PermissionManager/app/src/main/res/drawable/thumb_drawable.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/PermissionManager/app/src/main/res/layout/activity_main.xml b/PermissionManager/app/src/main/res/layout/activity_main.xml new file mode 100644 index 00000000..0e89ddd8 --- /dev/null +++ b/PermissionManager/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,175 @@ + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ 输出信息: +
+ + +
+ +
+ + + +
+
+ +
+
+
+ + + + + +)***"; + +} + +void writeToLog(const std::string & message, const char* logFile = "/data/local/tmp/root_server.log") { + // FILE* file = fopen(logFile, "a"); + // if (file == nullptr) { + // perror("Error opening file"); + // return; + // } + // fwrite(message.c_str(), sizeof(char), message.length(), file); + // fwrite("\n", sizeof(char), 1, file); + // fclose(file); + //printf("%s\n", message.c_str()); +} + +std::string GetHttpHead_200(long lLen) { + std::stringstream sstrHead; + sstrHead << "HTTP/1.1 200 OK\r\n"; + sstrHead << "Access-Control-Allow-Origin: *\r\n"; + sstrHead << "Connection: keep-alive\r\n"; + sstrHead << "Content-Length: " << lLen << "\r\n"; + sstrHead << "Content-Type: text/html; charset=UTF-8\r\n"; + sstrHead << "\r\n"; + return sstrHead.str(); +} + + +#endif /* _ROOT_SERVER_H_ */ diff --git a/testRoot/jni/lib_root_server/lib_root_server_inline_key.h b/testRoot/jni/lib_root_server/lib_root_server_inline_key.h new file mode 100644 index 00000000..58a61ae4 --- /dev/null +++ b/testRoot/jni/lib_root_server/lib_root_server_inline_key.h @@ -0,0 +1,10 @@ +#ifndef _ROOT_SERVER_INLINE_KEY_H_ +#define _ROOT_SERVER_INLINE_KEY_H_ +#include +#include + +static volatile const char static_inline_root_key[4096] = { + "e21cb39dc7ba99c986deb830ff8b8141b29764128c47ef71ae2375a53a13b880e63262b0e5596c981a9c4ded86b39674d77632683c35adc163812368eb13e75b" +}; + +#endif /* _ROOT_SERVER_INLINE_KEY_H_ */ diff --git a/testRoot/jni/lib_root_server/lib_root_server_inline_so_name.h b/testRoot/jni/lib_root_server/lib_root_server_inline_so_name.h new file mode 100644 index 00000000..f5178a3a --- /dev/null +++ b/testRoot/jni/lib_root_server/lib_root_server_inline_so_name.h @@ -0,0 +1,10 @@ +#ifndef _ROOT_SERVER_INLINE_SO_NAME_H_ +#define _ROOT_SERVER_INLINE_SO_NAME_H_ +#include +#include + +static volatile const char static_inline_so_name[4096] = { + "4b354bade02c335b02dee14bc39a471573c100b7e2f4e14d54dc58749babc678d2e4c740c3bc37c96f85a0d9ec091a44419ff0e14efabc3a4fdba9a3e21f1422" +}; + +#endif /* _ROOT_SERVER_INLINE_SO_NAME_H_ */ diff --git a/testRoot/jni/lib_su_env/lib_su_env.cpp b/testRoot/jni/lib_su_env/lib_su_env.cpp new file mode 100644 index 00000000..6199c23f --- /dev/null +++ b/testRoot/jni/lib_su_env/lib_su_env.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include "lib_su_env_inline.h" + +void so_entry() { + std::string who = getenv("PATH"); + if(who.find(const_cast(static_inline_su_path)) != std::string::npos) { + return; + } + std::string newWho = const_cast(static_inline_su_path); + newWho += ":"; + newWho += who; + setenv("PATH", newWho.c_str(), 1); +} + +extern "C" void __attribute__((constructor)) inject_su_path_entry() { + so_entry(); +} diff --git a/testRoot/jni/lib_su_env/lib_su_env_inline.h b/testRoot/jni/lib_su_env/lib_su_env_inline.h new file mode 100644 index 00000000..3c7bd952 --- /dev/null +++ b/testRoot/jni/lib_su_env/lib_su_env_inline.h @@ -0,0 +1,10 @@ +#ifndef _INJECT_SU_PATH_INLINE_SU_PATH_H_ +#define _INJECT_SU_PATH_INLINE_SU_PATH_H_ +#include +#include + +static volatile const char static_inline_su_path[4096] = { + "c8e275b4fdfb24fb8d146202480377427d9ba1d03864c8b474e7f176648829e8c4014632cb1a1f0fbeb1d637eece7d07f8fad983e27ee99bff49f215177e7463" +}; + +#endif /* _INJECT_SU_PATH_INLINE_SU_PATH_H_ */ diff --git a/testRoot/jni/root_server/root_server.cpp b/testRoot/jni/root_server/root_server.cpp new file mode 100644 index 00000000..4b959188 --- /dev/null +++ b/testRoot/jni/root_server/root_server.cpp @@ -0,0 +1,580 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "root_server.h" +#include "root_server_inline_key.h" +#include "root_server_inline_so_name.h" +#include "../testRoot.h" +#include "../kernel_root_kit/kernel_root_kit_umbrella.h" +#include "../utils/stringUtils.h" +#include "../utils/jsonUtils.h" + +std::atomic g_firstRequest{false}; +std::atomic g_heartbeat{false}; + +class InjectSuInfo{ +public: + std::atomic working{false}; + std::atomic success{false}; + void set_app_name(const std::string & app_name) { + std::lock_guard guard(m_msgLock); + m_app_name = app_name; + } + std::string getAppName() { + std::lock_guard guard(m_msgLock); + return m_app_name; + } + + void append_console_msg(const std::string & console) { + std::lock_guard guard(m_msgLock); + m_consoleMsg = console + "\n"; + } + void clearConsoleMsg() { + std::lock_guard guard(m_msgLock); + m_consoleMsg = ""; + } + std::string getConsoleMsg() { + std::lock_guard guard(m_msgLock); + return m_consoleMsg; + } + +private: + std::string m_app_name; + std::string m_consoleMsg; + std::mutex m_msgLock; +} g_inject_su_info; + +namespace { +constexpr const char* k_su_base_path = "/data/local/tmp"; +} + +bool try_lock_file(const char* path) { + int fd = open(path, O_CREAT, 0666); + if (fd == -1) { + perror("Unable to open the lock file"); + return false; + } + + if (flock(fd, LOCK_EX | LOCK_NB) == -1) { + perror("Another instance is running"); + close(fd); // Don't forget to close the file descriptor + return false; + } + + // You might want to store the file descriptor somewhere if you plan to release the lock later. + // For now, we are not closing it, which means the lock will be held until the process terminates. + return true; +} + +std::set get_self_so_paths() { + char line[1024] = { 0 }; + std::set so_paths; + FILE* fp = fopen("/proc/self/maps", "r"); + if (fp != NULL) { + while (fgets(line, sizeof(line), fp)) { + if (strstr(line, ".so")) { + char* start = strstr(line, "/"); + if (start) { + char* end = strchr(start, '\n'); + if (end) { + *end = '\0'; + so_paths.insert(std::string(start)); + } + } + } + } + fclose(fp); + } + return so_paths; +} + +bool is_already_running() { + std::set current_so_paths = get_self_so_paths(); + std::string so_name = const_cast(static_inline_so_name); + std::string tmp_path_end = "/" + so_name; + size_t len = tmp_path_end.length(); + + auto it = std::find_if(current_so_paths.begin(), current_so_paths.end(), [len, &tmp_path_end](const std::string& path) { + return path.size() >= len && path.compare(path.size() - len, len, tmp_path_end) == 0; + }); + if(it == current_so_paths.end()) { + return true; + } + std::string lock_file_path = *it + ".lock"; + return !try_lock_file(lock_file_path.c_str()); +} + +std::string convert_2_json(const std::string & str, const std::map & appendParam = {}) { + std::string strJson; + cJSON *json = cJSON_CreateObject(); + if(json) { + cJSON_AddStringToObject(json, "content", str.c_str()); + for(const auto & param: appendParam) { + cJSON_AddStringToObject(json, param.first.c_str(), param.second.c_str()); + } + char *jsonString = cJSON_Print(json); + if(jsonString) { + strJson = jsonString; + free(jsonString); + } + cJSON_Delete(json); + } + return strJson; +} + +std::string convert_2_json_v(const std::vector &v, const std::map & appendParam = {}) { + std::string strJson; + cJSON *json = cJSON_CreateObject(); + if (json) { + cJSON *jsonArray = cJSON_CreateArray(); + for (const std::string &str : v) { + cJSON_AddItemToArray(jsonArray, cJSON_CreateString(str.c_str())); + } + cJSON_AddItemToObject(json, "content", jsonArray); + for(const auto & param: appendParam) { + cJSON_AddStringToObject(json, param.first.c_str(), param.second.c_str()); + } + char *jsonString = cJSON_Print(json); + if (jsonString) { + strJson = jsonString; + free(jsonString); + } + + cJSON_Delete(json); + } + return strJson; +} + +std::string handle_index() { + std::string strIndexHtml = HTML_CONTENT; + replaceAllOccurrences(strIndexHtml, "11945efd3337ff4cd1168d98bc108cae", std::to_string(PORT)); + replaceAllOccurrences(strIndexHtml, "6a181c88b7d5b51ff84fb344acbcee86", POST_KEY); + return strIndexHtml; +} + +std::string handle_heartbeat(const std::string & userName) { + g_heartbeat = true; + return convert_2_json(userName); +} + +std::string handle_test_root() { + std::stringstream sstr; + sstr << "get_root:" << kernel_root::get_root(const_cast(static_inline_root_key)) << std::endl << std::endl; + sstr << get_capability_info(); + return convert_2_json(sstr.str()); +} + +std::string handle_run_root_cmd(const std::string & cmd) { + ssize_t err = 0; + std::string result = kernel_root::run_root_cmd(const_cast(static_inline_root_key), cmd.c_str(), err); + + std::stringstream sstr; + sstr << "run_root_cmd err:" << err << ", result:" << result; + return convert_2_json(sstr.str()); +} + +std::string handle_run_kernel_cmd(const std::string & cmd) { + ssize_t err = 0; + std::string result = kernel_root::run_init64_cmd_wrapper(const_cast(static_inline_root_key), cmd.c_str(), err); + + std::stringstream sstr; + sstr << "run_init64_cmd_wrapper err:" << err << ", result:" << result; + return convert_2_json(sstr.str()); +} + +std::string handle_install_su() { + ssize_t err = 0; + std::string su_hide_full_path = kernel_root::install_su(const_cast(static_inline_root_key), SU_BASE_PATH, err); + std::stringstream sstr; + sstr << "install su err:" << err<<", su_hide_full_path:" << su_hide_full_path << std::endl; + + if (err == 0) { + sstr << "installSu done."<< std::endl; + } + std::map param; + param["su_hide_full_path"] = su_hide_full_path; + param["err"] = std::to_string(err); + return convert_2_json(sstr.str(), param); +} + +std::string handle_uninstall_su() { + + ssize_t err = kernel_root::safe_uninstall_su(const_cast(static_inline_root_key), SU_BASE_PATH); + std::stringstream sstr; + sstr << "uninstallSu err:" << err << std::endl; + if (err != 0) { + return convert_2_json(sstr.str()); + } + sstr << "uninstallSu done."; + + std::map param; + param["err"] = std::to_string(err); + return convert_2_json(sstr.str(), param); +} + +std::string handle_get_app_list(bool isShowSystemApp, bool isShowThirtyApp, bool isShowRunningAPP) { + std::vector packageNames; + std::string cmd; + if(isShowSystemApp && isShowThirtyApp) { + cmd = "pm list packages"; + } else if(isShowSystemApp) { + cmd = "pm list packages -s"; + } else if(isShowThirtyApp) { + cmd = "pm list packages -3"; + } + ssize_t err = 0; + std::string packages = kernel_root::run_root_cmd(const_cast(static_inline_root_key), cmd.c_str(), err); + if(err != 0) { + return convert_2_json_v(packageNames); + } + + std::map pid_map; + if(isShowRunningAPP) { + err = kernel_root::get_all_cmdline_process(const_cast(static_inline_root_key), pid_map); + if(err != 0) { + return convert_2_json_v(packageNames); + } + + } + // remove "package:" flag + std::istringstream iss(packages); + std::string line; + while (getline(iss, line)) { + size_t pos = line.find("package:"); + if (pos != std::string::npos) { + line.erase(pos, std::string("package:").length()); + } + if(isShowRunningAPP) { + + bool isFound = false; + for(auto & item : pid_map) { + if(item.second.find(line) != std::string::npos) { + isFound = true; + break; + } + } + if(!isFound) { + continue; + } + } + packageNames.push_back(line); + } + return convert_2_json_v(packageNames); +} + +void inject_su_thread() { + writeToLog("inject_su_thread enter"); + + // 1.获取su_xxx隐藏目录 + std::string su_hide_path = kernel_root::su::find_su_hide_folder_path(k_su_base_path, "su"); + g_inject_su_info.append_console_msg("su_hide_path ret val:" + su_hide_path); + + if (su_hide_path.empty()) { + g_inject_su_info.working = false; + return; + } + + // 2.杀光所有历史进程 + std::set out; + ssize_t err = kernel_root::find_all_cmdline_process(const_cast(static_inline_root_key), g_inject_su_info.getAppName().c_str(), out); + g_inject_su_info.append_console_msg("find_all_cmdline_process err:" + std::to_string(err) + ", cnt:" + std::to_string(out.size())); + if (err) { + g_inject_su_info.working = false; + return; + } + + for (pid_t pid : out) { kernel_root::kill_process(const_cast(static_inline_root_key), pid); } + + // 3.注入su环境变量到指定进程 + g_inject_su_info.append_console_msg("waiting for process creation:" + g_inject_su_info.getAppName()); + + pid_t pid; + err = kernel_root::wait_and_find_cmdline_process( + const_cast(static_inline_root_key), g_inject_su_info.getAppName().c_str(), 60 * 1000, pid); + g_inject_su_info.append_console_msg("waiting for process creation err:" + std::to_string(err)); + if (err) { + g_inject_su_info.working = false; + return; + } + err = kernel_root::inject_process_env64_PATH_wrapper(const_cast(static_inline_root_key), pid, + su_hide_path.c_str(), kernel_root::api_offset_read_mode::only_read_file); + g_inject_su_info.append_console_msg("inject su err:" + std::to_string(err) + ", errmsg:" + strerror(errno)); + g_inject_su_info.success = true; + g_inject_su_info.working = false; +} + +std::string handle_inject_su(const std::string & app_name) { + std::map param; + param["errcode"] = "0"; + if(g_inject_su_info.working) { + param["errcode"] = "-1"; + return convert_2_json("inject su thread already running.", param); + } + + if(app_name.empty()) { + param["errcode"] = "-2"; + return convert_2_json("app name is empty.", param); + } + g_inject_su_info.set_app_name(app_name); + writeToLog("start inject su thread, app name: " + app_name); + g_inject_su_info.working = true; + g_inject_su_info.success = false; + std::thread td(inject_su_thread); + td.detach(); + return convert_2_json("ok", param); +} + +std::string handle_get_inject_su_result() { + //writeToLog("handle_get_inject_su_result enter"); + std::map param; + param["working"] = g_inject_su_info.working ? "1" : "0"; + param["success"] = g_inject_su_info.success ? "1" : "0"; + + std::string console = g_inject_su_info.getConsoleMsg(); + g_inject_su_info.clearConsoleMsg(); + + return convert_2_json(console, param); +} + +std::string handle_unknow_type() { + return convert_2_json("unknow command type."); +} + +std::string handle_post_action(std::string_view post_data) { + std::string type; + std::string cmd; + std::string userName; + std::string name; + bool showSystemApp = false; + bool showThirdApp = false; + bool showRunningApp = false; + std::string client_json = GetMiddleJsonString(post_data); + // printf("responseJson:%s\n", responseJson.c_str()); + + cJSON* parsed_json = cJSON_Parse(client_json.c_str()); + if (!parsed_json) { + return handle_unknow_type(); + } + cJSON* j_type = cJSON_GetObjectItem(parsed_json, "type"); + cJSON* j_userName = cJSON_GetObjectItem(parsed_json, "userName"); + if (!j_type || !j_userName) { + return handle_unknow_type(); + } + type = j_type->valuestring; + userName = j_userName->valuestring; + + + cJSON* j_cmd = cJSON_GetObjectItem(parsed_json, "cmd"); + if(j_cmd) { + cmd = j_cmd->valuestring; + } + + cJSON* j_showSystemApp = cJSON_GetObjectItem(parsed_json, "showSystemApp"); + if(j_showSystemApp) { + showSystemApp = !!j_showSystemApp->valueint; + } + + cJSON* j_showThirdApp = cJSON_GetObjectItem(parsed_json, "showThirdApp"); + if(j_showThirdApp) { + showThirdApp = !!j_showThirdApp->valueint; + } + + cJSON* j_showRunningApp = cJSON_GetObjectItem(parsed_json, "showRunningApp"); + if(j_showRunningApp) { + showRunningApp = !!j_showRunningApp->valueint; + } + + cJSON* j_name = cJSON_GetObjectItem(parsed_json, "name"); + if(j_name) { + name = j_name->valuestring; + } + + //writeToLog("Handle post action:" + type); + + if(type == "heartbeat") { + return handle_heartbeat(userName); + } + if(type == "testRoot") { + return handle_test_root(); + } + if(type == "runRootCmd") { + return handle_run_root_cmd(cmd); + } + if(type == "runKernelCmd") { + return handle_run_kernel_cmd(cmd); + } + if(type == "installSu") { + return handle_install_su(); + } + if(type == "uninstallSu") { + return handle_uninstall_su(); + } + if(type == "getAppList") { + return handle_get_app_list(showSystemApp, showThirdApp, showRunningApp); + } + if(type == "injectSu") { + return handle_inject_su(name); + } + if(type == "getInjectSuResult") { + return handle_get_inject_su_result(); + } + return handle_unknow_type(); +} + +void handle_client(int client_socket) { + kernel_root::get_root(const_cast(static_inline_root_key)); + + // Set a timeout for the receive operation + struct timeval timeout; + timeout.tv_sec = 0; // 0 seconds + timeout.tv_usec = 500 * 1000; // 500 milliseconds + if (setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) { + perror("setsockopt"); + close(client_socket); + return; + } + + char buffer[4096] = {0}; + int bytes_read = read(client_socket, buffer, sizeof(buffer) - 1); + + if (bytes_read > 0) { + std::string request(buffer, bytes_read); + std::string response_body; + if (request.length() > 3 && request.substr(0, 3) == "GET") { + g_firstRequest = true; + response_body = handle_index(); + } else if (request.length() > 4 && request.substr(0, 4) == "POST") { + if (request.find(POST_KEY) != std::string::npos) { + g_firstRequest = true; + response_body = handle_post_action(request); + } + } + + if(response_body.length()) { + // Note: I'm assuming that the first part of each "handle_" function result + // is the header, and the rest is the body. This could be modified based on actual structure. + std::string response_header = GetHttpHead_200(response_body.length()); + + // Send header + send(client_socket, response_header.c_str(), response_header.size(), 0); + + // Send body + send(client_socket, response_body.c_str(), response_body.size(), 0); + } + } + + shutdown(client_socket, SHUT_RDWR); + close(client_socket); +} + +void checkTimeoutActive() { + //首次请求必须在10秒内发起 + for(int i = 0; i < 10; i++) { + sleep(1); + if(g_firstRequest) { + break; + } + } + if(!g_firstRequest) { + _exit(0); //安全结束服务器 + } + // 20秒内无心跳自动退出 + for(int i = 0; i < 20; i++) { + sleep(1); + if(g_heartbeat) { + g_heartbeat = false; + i = 0; + } + } + _exit(0); +} + +int server_main() { + writeToLog("server_main enter"); + int server_socket, client_socket; + struct sockaddr_in server_addr, client_addr; + socklen_t client_len; + server_socket = socket(AF_INET, SOCK_STREAM, 0); + if (server_socket == -1) { + // std::cerr << "Could not create socket." << std::endl; + writeToLog("Could not create socket."); + return -1; + } + server_addr.sin_family = AF_INET; + server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); + server_addr.sin_port = htons(PORT); + + int opt = 1; + setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + + if (bind(server_socket, (struct sockaddr*)&server_addr, + sizeof(server_addr)) < 0) { + writeToLog("Bind failed."); + return -1; + } + listen(server_socket, 5); + + std::thread heartbeatThread(checkTimeoutActive); + heartbeatThread.detach(); + + writeToLog("Server listening"); + while ((client_socket = accept( + server_socket, (struct sockaddr*)&client_addr, &client_len))) { + std::thread client_thread(handle_client, client_socket); + client_thread.detach(); + } + close(server_socket); + return 0; +} + +void open_server_url() { + ssize_t err = 0; + std::string openUrlCmd = "am start -a android.intent.action.VIEW -d http://127.0.0.1:" + std::to_string(PORT); + kernel_root::run_root_cmd(const_cast(static_inline_root_key), openUrlCmd.c_str(), err); +} + +void fork_child_main() { + if (kernel_root::get_root(const_cast(static_inline_root_key))) { + return; + } + writeToLog("fork_child_main server enter"); + if(is_already_running()) { return; } + writeToLog("fork_child_main server main"); + server_main(); +} + +static bool isLoaded = false; +extern "C" void __attribute__((constructor)) root_server_entry() { + if (isLoaded) { return; } + isLoaded = true; + + { + pid_t pid = fork(); + if (pid == 0) { + sleep(1); // wait app init + open_server_url(); + _exit(0); + } + } + + { + pid_t pid = fork(); + if (pid == 0) { + writeToLog("1111111111111111 listening"); + fork_child_main(); + _exit(0); + } + } + +} diff --git a/testRoot/jni/root_server/root_server.h b/testRoot/jni/root_server/root_server.h new file mode 100644 index 00000000..d9eff864 --- /dev/null +++ b/testRoot/jni/root_server/root_server.h @@ -0,0 +1,701 @@ +#ifndef _ROOT_SERVER_H_ +#define _ROOT_SERVER_H_ +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { +const int PORT = 33445; +const char *POST_KEY = "9c5a503d973f104fc607aaf7f61ddb2cbc7af6fde95fc0e9"; +const char *SU_BASE_PATH = "/data/local/tmp"; + +const char* HTML_CONTENT = R"***( + + + + + + + SKRoot Implanted Web Server + + + + + + + +
+ Super Kernel Root内核级完美隐藏ROOT演示 + 新一代SKRoot,挑战全网root检测手段,跟面具完全不同思路,摆脱面具被检测的弱点,完美隐藏root功能,全程不需要暂停SELinux,实现真正的SELinux 0%触碰,通用性强,通杀所有内核,不需要内核源码,直接patch内核,兼容安卓APP直接JNI调用,稳定、流畅、不闪退。 + 当前是寄生模式,您可卸载原来的管理APP,避免管理APP被侦测 +
+
+ +
+ +
+
+ 输出信息: +
+ + +
+ +
+
+ +
+
+ +
+
+
+ + + + + +)***"; + +} + +void writeToLog(const std::string & message, const char* logFile = "/data/local/tmp/root_server.log") { + // FILE* file = fopen(logFile, "a"); + // if (file == nullptr) { + // perror("Error opening file"); + // return; + // } + // fwrite(message.c_str(), sizeof(char), message.length(), file); + // fwrite("\n", sizeof(char), 1, file); + // fclose(file); +} + +std::string GetHttpHead_200(long lLen) { + std::stringstream sstrHead; + sstrHead << "HTTP/1.1 200 OK\r\n"; + sstrHead << "Access-Control-Allow-Origin: *\r\n"; + sstrHead << "Connection: keep-alive\r\n"; + sstrHead << "Content-Length: " << lLen << "\r\n"; + sstrHead << "Content-Type: text/html; charset=UTF-8\r\n"; + sstrHead << "\r\n"; + return sstrHead.str(); +} + + +#endif /* _ROOT_SERVER_H_ */ diff --git a/testRoot/jni/root_server/root_server_inline_key.h b/testRoot/jni/root_server/root_server_inline_key.h new file mode 100644 index 00000000..58a61ae4 --- /dev/null +++ b/testRoot/jni/root_server/root_server_inline_key.h @@ -0,0 +1,10 @@ +#ifndef _ROOT_SERVER_INLINE_KEY_H_ +#define _ROOT_SERVER_INLINE_KEY_H_ +#include +#include + +static volatile const char static_inline_root_key[4096] = { + "e21cb39dc7ba99c986deb830ff8b8141b29764128c47ef71ae2375a53a13b880e63262b0e5596c981a9c4ded86b39674d77632683c35adc163812368eb13e75b" +}; + +#endif /* _ROOT_SERVER_INLINE_KEY_H_ */ diff --git a/testRoot/jni/root_server/root_server_inline_so_name.h b/testRoot/jni/root_server/root_server_inline_so_name.h new file mode 100644 index 00000000..f5178a3a --- /dev/null +++ b/testRoot/jni/root_server/root_server_inline_so_name.h @@ -0,0 +1,10 @@ +#ifndef _ROOT_SERVER_INLINE_SO_NAME_H_ +#define _ROOT_SERVER_INLINE_SO_NAME_H_ +#include +#include + +static volatile const char static_inline_so_name[4096] = { + "4b354bade02c335b02dee14bc39a471573c100b7e2f4e14d54dc58749babc678d2e4c740c3bc37c96f85a0d9ec091a44419ff0e14efabc3a4fdba9a3e21f1422" +}; + +#endif /* _ROOT_SERVER_INLINE_SO_NAME_H_ */ diff --git a/testRoot/jni/su/encryptor.h b/testRoot/jni/su/encryptor.h new file mode 100644 index 00000000..6bf32ba0 --- /dev/null +++ b/testRoot/jni/su/encryptor.h @@ -0,0 +1,96 @@ +#ifndef _SU_ENCRYPTOR_H_ +#define _SU_ENCRYPTOR_H_ +#include +#include +#include +#include +#include +namespace { +static void rand_str(char* dest, int n) { + int i, randno; + char stardstring[63] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + srand((unsigned)time(NULL)); + for (i = 0; i < n; i++) { + randno = rand() % 62; + *dest = stardstring[randno]; + dest++; + } +} +static std::string encryp_string(const std::string& src, const std::string& key, bool random = true) { + int KeyPos = -1; + int SrcAsc = 0; + time_t t; + + int KeyLen = key.length(); + if (KeyLen == 0) + return ""; + + + int offset; + if(random) { + srand((unsigned)time(&t)); + offset = rand() % 255; + } else { + offset = 128; + } + + std::stringstream ss; + ss << std::hex << std::setw(2) << std::setfill('0') << offset; + + for (int i = 0; i < src.length(); i++) { + SrcAsc = (src[i] + offset) % 255; + + if (KeyPos < KeyLen - 1) + KeyPos++; + else + KeyPos = 0; + + SrcAsc = SrcAsc ^ key[KeyPos]; + + ss << std::hex << std::setw(2) << std::setfill('0') << SrcAsc; + + offset = SrcAsc; + } + return ss.str(); +} + +static std::string uncryp_string(const std::string& src, const std::string& key) { + int KeyLen = key.length(); + if (KeyLen == 0) + return {}; + + int KeyPos = -1; + int offset = 0; + std::string dest; + int SrcAsc = 0; + int TmpSrcAsc = 0; + + std::stringstream ss; + ss << std::hex << src.substr(0, 2); + ss >> offset; + int SrcPos = 2; + while (SrcPos < src.length()) { + ss.clear(); + ss << std::hex << src.substr(SrcPos, 2); + ss >> SrcAsc; + if (KeyPos < KeyLen - 1) + KeyPos++; + else + KeyPos = 0; + + TmpSrcAsc = SrcAsc ^ key[KeyPos]; + + if (TmpSrcAsc <= offset) + TmpSrcAsc = 255 + TmpSrcAsc - offset; + else + TmpSrcAsc = TmpSrcAsc - offset; + + dest += char(TmpSrcAsc); + offset = SrcAsc; + SrcPos += 2; + } + + return dest; +} +} +#endif /* _SU_ENCRYPTOR_H_ */ diff --git a/testRoot/jni/su/su.cpp b/testRoot/jni/su/su.cpp new file mode 100644 index 00000000..be5a1af0 --- /dev/null +++ b/testRoot/jni/su/su.cpp @@ -0,0 +1,191 @@ +#include "su.h" +#include "su_log.h" +#include "su_hide_path_utils.h" +#include "kernel_root_kit/kernel_root_kit_command.h" +#include + +namespace { + std::string get_executable_directory() { + char processdir[4096] = { 0 }; + ssize_t path_len = readlink("/proc/self/exe", processdir, sizeof(processdir)); + if(path_len > 0) { + std::filesystem::path path(processdir); + return path.parent_path().string(); + } + return {}; + } + + /* + * Bionic's atoi runs through strtol(). + * Use our own implementation for faster conversion. + */ + int parse_int(const char* s) { + int val = 0; + char c; + while ((c = *(s++))) { + if (c > '9' || c < '0') + return -1; + val = val * 10 + c - '0'; + } + return val; + } + + // Set effective uid back to root, otherwise setres[ug]id will fail if uid isn't root + void set_identity(unsigned uid) { + if (seteuid(0)) { + SU_PRINTF("seteuid (root)"); + } + if (setresgid(uid, uid, uid)) { + SU_PRINTF("setresgid (%u)", uid); + } + if (setresuid(uid, uid, uid)) { + SU_PRINTF("setresuid (%u)", uid); + } + } +} + +void usage(int status) { + FILE* stream = (status == EXIT_SUCCESS) ? stdout : stderr; + + fprintf(stream, + "linux kernel root\n\n" + "Usage: su [options] [-] [user [argument...]]\n\n" + "Options:\n" + " -c, --command COMMAND pass COMMAND to the invoked shell\n" + " -h, --help display this help message and exit\n" + " -, -l, --login pretend the shell to be a login shell\n" + " -m, -p,\n" + " --preserve-environment preserve the entire environment\n" + " -s, --shell SHELL use SHELL instead of the default " DEFAULT_SHELL "\n" + " -v, --version display version number and exit\n" + " -V display version code and exit\n" + " -mm, -M,\n" + " --mount-master force run in the global mount namespace\n\n"); + exit(status); +} + +static inline std::string get_root_key() { + std::string myself_path = get_executable_directory(); + std::string str_root_key = kernel_root::su::parse_root_key_by_su_path(myself_path.c_str()); + return str_root_key; +} + +int su_client_main(int argc, char* argv[]) { + int c; + struct option long_opts[] = { + { "command", required_argument, nullptr, 'c' }, + { "help", no_argument, nullptr, 'h' }, + { "login", no_argument, nullptr, 'l' }, + { "preserve-environment", no_argument, nullptr, 'p' }, + { "shell", required_argument, nullptr, 's' }, + { "version", no_argument, nullptr, 'v' }, + { "context", required_argument, nullptr, 'z' }, + { "mount-master", no_argument, nullptr, 'M' }, + { nullptr, 0, nullptr, 0 }, + }; + + su_request su_req; + + for (int i = 0; i < argc; i++) { + // Replace -cn with -z, -mm with -M for supporting getopt_long + if (strcmp(argv[i], "-cn") == 0) + strcpy(argv[i], "-z"); + else if (strcmp(argv[i], "-mm") == 0) + strcpy(argv[i], "-M"); + } + + while ((c = getopt_long(argc, argv, "c:hlmps:Vvuz:M", long_opts, nullptr)) != -1) { + switch (c) { + case 'c': + for (int i = optind - 1; i < argc; ++i) { + if (!su_req.command.empty()) + su_req.command += ' '; + su_req.command += argv[i]; + } + optind = argc; + break; + case 'h': + usage(EXIT_SUCCESS); + break; + case 'l': + su_req.login = true; + break; + case 'm': + case 'p': + su_req.keepenv = true; + break; + case 's': + su_req.shell = optarg; + break; + case 'V': + printf("%d\n", ROOT_VER_CODE); + exit(EXIT_SUCCESS); + case 'v': + printf("%s\n", ROOT_VERSION); + exit(EXIT_SUCCESS); + case 'z': + // Do nothing, placed here for legacy support :) + break; + case 'M': + su_req.mount_master = true; + break; + default: + /* Bionic getopt_long doesn't terminate its error output by newline */ + fprintf(stderr, "\n"); + usage(2); + } + } + + if (optind < argc && strcmp(argv[optind], "-") == 0) { + su_req.login = true; + optind++; + } + + std::string root_key = get_root_key(); + SU_PRINTF("root_key:%s\n", root_key.c_str()); + + if (fork() == 0) { + kernel_root::get_root(root_key.c_str()); + SU_PRINTF("current uid:%d\n", getuid()); + + /* username or uid */ + if (optind < argc) { + struct passwd* pw; + pw = getpwnam(argv[optind]); + if (pw) { + su_req.uid = pw->pw_uid; + } else { + su_req.uid = parse_int(argv[optind]); + } + optind++; + } + struct passwd* pw = getpwuid(su_req.uid); + if (pw) { + setenv("HOME", pw->pw_dir, 1); + setenv("USER", pw->pw_name, 1); + setenv("LOGNAME", pw->pw_name, 1); + setenv("SHELL", su_req.shell.data(), 1); + } + + const char* new_argv[4] = { nullptr }; + new_argv[0] = su_req.login ? "-" : su_req.shell.data(); + + if (!su_req.command.empty()) { + new_argv[1] = "-c"; + new_argv[2] = su_req.command.data(); + } + + // If you need it, you can unblock this line of code yourself + //set_identity(su_req.uid); + + execvp(su_req.shell.data(), (char**)new_argv); + } else { + wait(NULL); + } + exit(0); + return 0; +} + +int main(int argc, char* argv[]) { + return su_client_main(argc, argv); +} diff --git a/testRoot/jni/su/su.h b/testRoot/jni/su/su.h new file mode 100644 index 00000000..6626147a --- /dev/null +++ b/testRoot/jni/su/su.h @@ -0,0 +1,62 @@ +#ifndef _SU_H_ +#define _SU_H_ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEFAULT_SHELL "/system/bin/sh" + +// Constants for atty +#define ATTY_IN (1 << 0) +#define ATTY_OUT (1 << 1) +#define ATTY_ERR (1 << 2) + +#define UID_ROOT 0 +#define UID_SHELL 2000 + +#define ROOT_VER_CODE 1 +#define ROOT_VERSION "1.0" + +struct su_req_base { + int uid = UID_ROOT; + bool login = false; + bool keepenv = false; + bool mount_master = false; +} __attribute__((packed)); + +struct su_request : public su_req_base { + std::string shell = DEFAULT_SHELL; + std::string command; +}; + +#endif /* _SU_H_ */ diff --git a/testRoot/jni/su/su_hide_path_utils.h b/testRoot/jni/su/su_hide_path_utils.h new file mode 100644 index 00000000..688fe0d0 --- /dev/null +++ b/testRoot/jni/su/su_hide_path_utils.h @@ -0,0 +1,105 @@ +#ifndef _SU_HIDDEN_FOLDER_PATH_UTILS_H_ +#define _SU_HIDDEN_FOLDER_PATH_UTILS_H_ +#include +#include +#include +#include +#include +#include +#include "../utils/base64.h" +#include "encryptor.h" + +#define RANDOM_GUID_LEN 8 +#define ROOT_KEY_LEN 48 +#define ENCRYKEY "ECC08B04-B9FF-40B5-9596-4408626181D5" + +namespace kernel_root{ +namespace su{ + +static std::string find_su_hide_folder_path( + const char* base_path, + const char* su_hide_folder_head_flag = "su") { + std::string id; + DIR* dir; + struct dirent* entry; + const char* su_head = su_hide_folder_head_flag; + + dir = opendir(base_path); + if (dir == NULL) + return id; + + while ((entry = readdir(dir)) != NULL) { + if ((strcmp(entry->d_name, ".") == 0) || + (strcmp(entry->d_name, "..") == 0)) { + continue; + } else if (entry->d_type != DT_DIR) { + continue; + } else if (strlen(entry->d_name) <= strlen(su_head)) { + continue; + } + if (!strstr(entry->d_name, su_head)) { + continue; + } + id = base_path; + id += "/"; + id += entry->d_name; + break; + } + closedir(dir); + return id; +} + +static std::string create_su_hide_folder(const char* str_root_key, + const char* base_path, + const char* su_hide_folder_head_flag) { + char guid[RANDOM_GUID_LEN] = {0}; + rand_str(guid, sizeof(guid)); + std::string encodeRootKey(guid, sizeof(guid)); + encodeRootKey += str_root_key; + + encodeRootKey = base64_encode((const unsigned char*)encodeRootKey.c_str(), + encodeRootKey.length()); + encodeRootKey = encryp_string(encodeRootKey, ENCRYKEY); + + std::string file_path = base_path; + file_path += "/"; + file_path += su_hide_folder_head_flag; + file_path += encodeRootKey; + file_path += "/"; + if (mkdir(file_path.c_str(), 0755)) { + return {}; + } + if (chmod(file_path.c_str(), 0777)) { + return {}; + } + return file_path; +} + +static inline std::string parse_root_key_by_su_path( + const char* su_path) { + std::string path = su_path; + if (path.empty()) { + return {}; + } + int n = path.find_last_of("_"); + if (n == -1) { + return {}; + } + path = path.substr(++n); + n = path.find("/"); + if (n != -1) { + path.substr(0, n); + } + + std::string decodeRootKey = uncryp_string(path, ENCRYKEY); + + decodeRootKey = base64_decode(decodeRootKey); + + if (decodeRootKey.length() < (RANDOM_GUID_LEN + ROOT_KEY_LEN)) { + return {}; + } + return decodeRootKey.substr(decodeRootKey.length() - ROOT_KEY_LEN); +} +} +} +#endif /* _SU_HIDDEN_FOLDER_PATH_UTILS_H_ */ diff --git a/testRoot/jni/su/su_log.h b/testRoot/jni/su/su_log.h new file mode 100644 index 00000000..16f77a47 --- /dev/null +++ b/testRoot/jni/su/su_log.h @@ -0,0 +1,16 @@ +#ifndef _LOG_H_ +#define _LOG_H_ +#include +#include +#include + +//安静输出模式 +#define QUIET_PRINTF + +#ifdef QUIET_PRINTF +#define SU_PRINTF(fmt, ...) +#else +#define SU_PRINTF(fmt, ...) printf(fmt, ##__VA_ARGS__) +#endif + +#endif /* _LOG_H_ */ diff --git a/testRoot/jni/testRoot.cpp b/testRoot/jni/testRoot.cpp new file mode 100644 index 00000000..ec94257c --- /dev/null +++ b/testRoot/jni/testRoot.cpp @@ -0,0 +1,324 @@ +#include "testRoot.h" + +// TODO:请将此处的KEY替换为你的内核ROOT KEY +#define ROOT_KEY "bz4kKoPVSAG1tnwlcs1PJ1qp6HtVymj60CoTgsjmMd1UALve" + +namespace { +constexpr const char* k_su_base_path = "/data/local/tmp"; +constexpr const char* recommend_files[] = {"libc++_shared.so"}; + +} +void test_root() { + // 获取ROOT权限 + printf("%s\n", get_capability_info().c_str()); + + printf("get_root ret:%zd\n", kernel_root::get_root(ROOT_KEY)); + + printf("%s\n", get_capability_info().c_str()); + return; +} + +void test_run_root_cmd(int argc, char* argv[]) { + // 执行ROOT命令 + std::stringstream sstrCmd; + for (int i = 0; i < argc; i++) { + sstrCmd << argv[i]; + if (i != (argc - 1)) { + sstrCmd << " "; + } + } + printf("test_run_root_cmd(%s)\n", sstrCmd.str().c_str()); + + ssize_t err; + std::string result = kernel_root::run_root_cmd(ROOT_KEY, sstrCmd.str().c_str(), err); + printf("test_run_root_cmd err:%zd\n", err); + printf("test_run_root_cmd result:%s\n", result.c_str()); +} + +void test_run_init64_cmd(int argc, char* argv[]) { + // 执行原生内核命令 + std::stringstream sstrCmd; + for (int i = 0; i < argc; i++) { + sstrCmd << argv[i]; + if (i != (argc - 1)) { + sstrCmd << " "; + } + } + printf("test_run_init64_cmd(%s)\n", sstrCmd.str().c_str()); + + ssize_t err; + std::string result = kernel_root::run_init64_cmd_wrapper(ROOT_KEY, sstrCmd.str().c_str(), err); + printf("run_init64_cmd_wrapper err:%zd\n", err); + printf("run_init64_cmd_wrapper result:%s\n", result.c_str()); +} + +void test_install_su_env() { + // 安装部署su + ssize_t err; + std::string su_hide_name = kernel_root::install_su(ROOT_KEY, k_su_base_path, err); + printf("install su hide full path:%s, err:%zd\n", su_hide_name.c_str(), + err); +} + +void test_su_env_temp_inject(const char* target_pid_cmdline) { + if (kernel_root::get_root(ROOT_KEY) != 0) { + return; + } + + // 1.获取su_xxx隐藏目录 + std::string su_hide_path = kernel_root::su::find_su_hide_folder_path(k_su_base_path, "su"); + printf("su_hide_path ret val:%s\n", su_hide_path.c_str()); + if (su_hide_path.empty()) { return; } + + // 2.杀光所有历史进程 + std::set out; + ssize_t err = kernel_root::find_all_cmdline_process( + ROOT_KEY, target_pid_cmdline, out); + printf("find_all_cmdline_process err:%zd, cnt:%zu\n", err, out.size()); + if (err) { return; } + for (pid_t pid : out) { kernel_root::kill_process(ROOT_KEY, pid); } + + // 3.注入su环境变量到指定进程 + printf("test_auto_su_env_inject Waiting for process creation(%s)\n", target_pid_cmdline); + pid_t pid; + err = kernel_root::wait_and_find_cmdline_process( + ROOT_KEY, target_pid_cmdline, 60 * 1000, pid); + printf("wait_and_find_cmdline_process(%zd)\n", err); + + err = kernel_root::inject_process_env64_PATH_wrapper(ROOT_KEY, pid, + su_hide_path.c_str()); + printf("inject_process_env64_PATH_wrapper ret val:%zd, error:%s\n", err, + strerror(errno)); +} + +void test_su_env_forever_inject(const char* target_pid_cmdline) { + if (kernel_root::get_root(ROOT_KEY) != 0) { + return; + } + // 1.获取su_xxx隐藏目录 + std::string su_hide_path = kernel_root::su::find_su_hide_folder_path(k_su_base_path, "su"); + printf("su_hide_path ret val:%s\n", su_hide_path.c_str()); + if (su_hide_path.empty()) { return; } + + // 2.寄生预检目标APP + std::set pid_arr; + ssize_t err = kernel_root::find_all_cmdline_process(ROOT_KEY, target_pid_cmdline, pid_arr); + if (err) { + printf("find_all_cmdline_process err:%zd\n", err); + return; + } + if (pid_arr.size() == 0) { + printf("请先运行目标APP: %s\n", target_pid_cmdline); + return; + } + std::map so_path_list; + err = kernel_root::parasite_precheck_app(ROOT_KEY, target_pid_cmdline, so_path_list); + if (err) { + printf("parasite_precheck_app error:%zd\n", err); + if(err == -9904) { + printf("此目标APP为32位应用,无法寄生\n"); + } + return; + } + if (!so_path_list.size()) { + printf("无法检测到目标APP的JNI环境,目标APP暂不可被寄生;您可重新运行目标APP后重试;或将APP进行手动加固(加壳),因为加固(加壳)APP后,APP会被产生JNI环境,方可寄生!\n"); + return; + } + printf("请在以下的目标APP文件列表中选择一个即将要被寄生的文件:\n"); + + std::vector> sort_printf; + for (const auto& item : so_path_list) { + if(item.second != kernel_root::app_so_status::running) { + continue; + } + sort_printf.push_back({item.first, item.second}); + } + for (const auto& item : so_path_list) { + if(item.second != kernel_root::app_so_status::not_running) { + continue; + } + sort_printf.push_back({item.first, item.second}); + } + for (const auto& item : sort_printf) { + auto file_path = std::get<0>(item); + auto app_so_status = std::get<1>(item); + std::filesystem::path filePath(file_path); + std::string file_name = filePath.filename().string(); + std::string status = app_so_status == kernel_root::app_so_status::running ? "(正在运行)" : "(未运行)"; + if(app_so_status == kernel_root::app_so_status::running) { + for(auto x = 0; x < sizeof(recommend_files) / sizeof(recommend_files[0]); x++) { + if(file_name == recommend_files[x]) { + status = "(推荐, 正在运行)"; + } + } + } + printf("\t%s %s\n", file_name.c_str(), status.c_str()); + } + printf("\n"); + printf("请输入将要被寄生的文件名称: "); + std::string user_input_so_name; + std::getline(std::cin, user_input_so_name); + printf("\n"); + auto it = std::find_if(so_path_list.begin(), so_path_list.end(), + [&](const auto& s) { return s.first.find(user_input_so_name) != std::string::npos; }); + if (it == so_path_list.end()) { + printf("Not found: %s\n", user_input_so_name.c_str()); + return; + } + + // 3.寄生植入目标APP + err = kernel_root::parasite_implant_su_env(ROOT_KEY, target_pid_cmdline, it->first.c_str(), su_hide_path); + printf("parasite_implant_su_env err:%zd\n", err); + if(err) { return; } + + // 4.杀光所有历史进程 + for (pid_t pid : pid_arr) { kernel_root::kill_process(ROOT_KEY, pid); } +} + +void test_clean_su_env() { + // 完全卸载清理su + ssize_t err = kernel_root::uninstall_su(ROOT_KEY, k_su_base_path, "su"); + printf("uninstall_su err:%zd\n", err); +} + +void test_implant_app(const char* target_pid_cmdline) { + // 1.寄生预检目标APP + std::set pid_arr; + ssize_t err = kernel_root::find_all_cmdline_process(ROOT_KEY, target_pid_cmdline, pid_arr); + if (err) { + printf("find_all_cmdline_process err:%zd\n", err); + return; + } + if (pid_arr.size() == 0) { + printf("请先运行目标APP: %s\n", target_pid_cmdline); + return; + } + std::map so_path_list; + err = kernel_root::parasite_precheck_app(ROOT_KEY, target_pid_cmdline, so_path_list); + if (err) { + printf("parasite_precheck_app error:%zd\n", err); + if(err == -9904) { + printf("此目标APP为32位应用,无法寄生\n"); + } + return; + } + if (!so_path_list.size()) { + printf("无法检测到目标APP的JNI环境,目标APP暂不可被寄生;您可重新运行目标APP后重试;或将APP进行手动加固(加壳),因为加固(加壳)APP后,APP会被产生JNI环境,方可寄生!\n"); + return; + } + printf("请在以下的目标APP文件列表中选择一个即将要被寄生的文件:\n"); + + std::vector> sort_printf; + for (const auto& item : so_path_list) { + if(item.second != kernel_root::app_so_status::running) { + continue; + } + sort_printf.push_back({item.first, item.second}); + } + for (const auto& item : so_path_list) { + if(item.second != kernel_root::app_so_status::not_running) { + continue; + } + sort_printf.push_back({item.first, item.second}); + } + for (const auto& item : sort_printf) { + auto file_path = std::get<0>(item); + auto app_so_status = std::get<1>(item); + std::filesystem::path filePath(file_path); + std::string file_name = filePath.filename().string(); + std::string status = app_so_status == kernel_root::app_so_status::running ? "(正在运行)" : "(未运行)"; + if(app_so_status == kernel_root::app_so_status::running) { + for(auto x = 0; x < sizeof(recommend_files) / sizeof(recommend_files[0]); x++) { + if(file_name == recommend_files[x]) { + status = "(推荐, 正在运行)"; + } + } + } + printf("\t%s %s\n", file_name.c_str(), status.c_str()); + } + printf("\n"); + printf("请输入将要被寄生的文件名称: "); + std::string user_input_so_name; + std::getline(std::cin, user_input_so_name); + printf("\n"); + auto it = std::find_if(so_path_list.begin(), so_path_list.end(), + [&](const auto& s) { return s.first.find(user_input_so_name) != std::string::npos; }); + if (it == so_path_list.end()) { + printf("Not found: %s\n", user_input_so_name.c_str()); + return; + } + + // 2.寄生植入目标APP + err = kernel_root::parasite_implant_app(ROOT_KEY, target_pid_cmdline, it->first.c_str()); + printf("parasite_implant_app err:%zd\n", err); + if(err) { return; } + + // 3.杀光所有历史进程 + for (pid_t pid : pid_arr) { kernel_root::kill_process(ROOT_KEY, pid); } +} + +int main(int argc, char* argv[]) { + printf( + "======================================================\n" + "本工具名称: Linux ARM64 完美隐藏ROOT演示\n\n" + "本工具功能列表:\n" + + "1. 显示自身权限信息\n" + "\tUsage: testRoot id\n\n" + + "2. 获取ROOT权限\n" + "\tUsage: testRoot get\n\n" + + "3. 执行ROOT命令\n" + "\tUsage: testRoot cmd \n\n" + + "4. 执行原生内核命令\n" + "\tUsage: testRoot init \n\n" + + "5. 安装部署su\n" + "\tUsage: testRoot su\n\n" + + "6. 临时注入su到指定进程\n" + "\tUsage: testRoot process \n\n" + + "7. 永久注入su到指定进程\n" + "\tUsage: testRoot implantSu \n\n" + + "8. 完全卸载清理su\n" + "\tUsage: testRoot cleansu\n\n" + + "9. 寄生目标APP\n" + "\tUsage: testRoot implantApp [so-name]\n\n" + + "本工具特点:\n" + "新一代SKRoot,跟面具完全不同思路,摆脱面具被检测的弱点,完美隐藏root功能,兼容安卓APP直接JNI稳定调用。\n" + "======================================================\n" + "为了实现最佳隐蔽性,推荐使用 [寄生目标APP] 功能,寄生到能常驻后台且联网的APP上,如音乐类、播放器类、运动类、广播类、社交聊天类APP\n\n" + "如需帮助,请使用对应的命令,或者查看上面的菜单。\n\n"); + + ++argv; + --argc; + if (argc == 0 || strcmp(argv[0], "id") == 0) { // 1.显示自身权限信息 + printf("%s\n", get_capability_info().c_str()); + } else if (strcmp(argv[0], "get") == 0) { // 2.获取ROOT权限 + test_root(); + } else if (argc >= 2 && strcmp(argv[0], "cmd") == 0) { // 3.执行ROOT命令 + test_run_root_cmd(argc - 1, argv + 1); + } else if (argc >= 2 && strcmp(argv[0], "init") == 0) { // 4.执行原生内核命令 + test_run_init64_cmd(argc - 1, argv + 1); + } else if (strcmp(argv[0], "su") == 0) { // 5.安装部署su + test_install_su_env(); + } else if (argc > 1 && strcmp(argv[0], "suTemp") == 0) { // 6.临时注入su到指定进程 + test_su_env_temp_inject(argv[1]); + } else if (argc > 1 && strcmp(argv[0], "suForever") == 0) { // 7.永久注入su到指定进程 + test_su_env_forever_inject(argv[1]); + } else if (strcmp(argv[0], "cleansu") == 0) { // 8.完全卸载清理su + test_clean_su_env(); + } else if (argc > 1 && strcmp(argv[0], "implantApp") == 0) { // 9.寄生目标APP + test_implant_app(argv[1]); + } else { + printf("unknown command.\n"); + return 1; + } + return 0; +} \ No newline at end of file diff --git a/testRoot/jni/testRoot.h b/testRoot/jni/testRoot.h new file mode 100644 index 00000000..bc5e77a7 --- /dev/null +++ b/testRoot/jni/testRoot.h @@ -0,0 +1,59 @@ +#ifndef _TEST_ROOT_H_ +#define _TEST_ROOT_H_ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "kernel_root_kit/kernel_root_kit_umbrella.h" +#include "su/su_hide_path_utils.h" + +static std::string get_capability_info() { + __uid_t now_uid, now_euid, now_suid; + __gid_t now_gid, now_egid, now_sgid; + if (getresuid(&now_uid, &now_euid, &now_suid)) { + return "FAILED getresuid()"; + } + if (getresgid(&now_gid, &now_egid, &now_sgid)) { + return "FAILED getresgid()"; + } + + std::stringstream sstrCapInfo; + sstrCapInfo<< "Current process information:\n"; + sstrCapInfo<< "uid:"<pid = getpid(); + cap_header->version = _LINUX_CAPABILITY_VERSION_3; //_1、_2、_3 + + if (capget(cap_header, cap_data) < 0) { + return "FAILED capget()"; + // perror("FAILED capget()"); + //exit(1); + } + sstrCapInfo << "cap effective:" << std::hex <effective << "," << std::endl + <<"cap permitted:"<< std::hex << cap_data->permitted<< "," << std::endl + <<"cap inheritable:"<< std::hex <inheritable<< std::endl; + FILE * fp = popen("getenforce", "r"); + if (fp) { + char cmd[512] = { 0 }; + fread(cmd, 1, sizeof(cmd), fp); + pclose(fp); + sstrCapInfo<< "read system SELinux status:"<< cmd; + } + return sstrCapInfo.str(); +} + + +#endif /* _TEST_ROOT_H_ */ diff --git a/testRoot/jni/utils/base64.h b/testRoot/jni/utils/base64.h new file mode 100644 index 00000000..555f0d75 --- /dev/null +++ b/testRoot/jni/utils/base64.h @@ -0,0 +1,100 @@ +#ifndef BASE_64_H +#define BASE_64_H +#include +#include +#include +static const std::string base64_chars = +"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +"abcdefghijklmnopqrstuvwxyz" +"0123456789+/"; + + +static inline bool is_base64(unsigned char c) { + return (isalnum(c) || (c == '+') || (c == '/')); +} + +static std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { + std::string ret; + int i = 0; + int j = 0; + unsigned char char_array_3[3]; + unsigned char char_array_4[4]; + + while (in_len--) { + char_array_3[i++] = *(bytes_to_encode++); + if (i == 3) { + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for (i = 0; (i < 4); i++) + ret += base64_chars[char_array_4[i]]; + i = 0; + } + } + + if (i) + { + for (j = i; j < 3; j++) + char_array_3[j] = '\0'; + + char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; + char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); + char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); + char_array_4[3] = char_array_3[2] & 0x3f; + + for (j = 0; (j < i + 1); j++) + ret += base64_chars[char_array_4[j]]; + + while ((i++ < 3)) + ret += '='; + + } + + return ret; + +} + +static std::string base64_decode(std::string const& encoded_string) { + int in_len = encoded_string.size(); + int i = 0; + int j = 0; + int in_ = 0; + unsigned char char_array_4[4], char_array_3[3]; + std::string ret; + + while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { + char_array_4[i++] = encoded_string[in_]; in_++; + if (i == 4) { + for (i = 0; i < 4; i++) + char_array_4[i] = base64_chars.find(char_array_4[i]); + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + + for (i = 0; (i < 3); i++) + ret += char_array_3[i]; + i = 0; + } + } + + if (i) { + for (j = i; j < 4; j++) + char_array_4[j] = 0; + + for (j = 0; j < 4; j++) + char_array_4[j] = base64_chars.find(char_array_4[j]); + + char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); + char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); + char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + + for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; + } + + return ret; +} + +#endif diff --git a/testRoot/jni/utils/cJSON.cpp b/testRoot/jni/utils/cJSON.cpp new file mode 100644 index 00000000..f6dd11c5 --- /dev/null +++ b/testRoot/jni/utils/cJSON.cpp @@ -0,0 +1,3119 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +/* cJSON */ +/* JSON parser in C. */ + +/* disable warnings about old C89 functions in MSVC */ +#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +#define _CRT_SECURE_NO_DEPRECATE +#endif + +#ifdef __GNUC__ +#pragma GCC visibility push(default) +#endif +#if defined(_MSC_VER) +#pragma warning (push) +/* disable warning about single line comments in system headers */ +#pragma warning (disable : 4001) +#endif + +#include +#include +#include +#include +#include +#include +#include + +#ifdef ENABLE_LOCALES +#include +#endif + +#if defined(_MSC_VER) +#pragma warning (pop) +#endif +#ifdef __GNUC__ +#pragma GCC visibility pop +#endif + +#include "cJSON.h" + +/* define our own boolean type */ +#ifdef true +#undef true +#endif +#define true ((cJSON_bool)1) + +#ifdef false +#undef false +#endif +#define false ((cJSON_bool)0) + +/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */ +#ifndef isinf +#define isinf(d) (isnan((d - d)) && !isnan(d)) +#endif +#ifndef isnan +#define isnan(d) (d != d) +#endif + +#ifndef NAN +#ifdef _WIN32 +#define NAN sqrt(-1.0) +#else +#define NAN 0.0/0.0 +#endif +#endif + +typedef struct { + const unsigned char *json; + size_t position; +} error; +static error global_error = { NULL, 0 }; + +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void) +{ + return (const char*) (global_error.json + global_error.position); +} + +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item) +{ + if (!cJSON_IsString(item)) + { + return NULL; + } + + return item->valuestring; +} + +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item) +{ + if (!cJSON_IsNumber(item)) + { + return (double) NAN; + } + + return item->valuedouble; +} + +/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */ +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || (CJSON_VERSION_PATCH != 16) + #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. +#endif + +CJSON_PUBLIC(const char*) cJSON_Version(void) +{ + static char version[15]; + sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + + return version; +} + +/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */ +static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2) +{ + if ((string1 == NULL) || (string2 == NULL)) + { + return 1; + } + + if (string1 == string2) + { + return 0; + } + + for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++) + { + if (*string1 == '\0') + { + return 0; + } + } + + return tolower(*string1) - tolower(*string2); +} + +typedef struct internal_hooks +{ + void *(CJSON_CDECL *allocate)(size_t size); + void (CJSON_CDECL *deallocate)(void *pointer); + void *(CJSON_CDECL *reallocate)(void *pointer, size_t size); +} internal_hooks; + +#if defined(_MSC_VER) +/* work around MSVC error C2322: '...' address of dllimport '...' is not static */ +static void * CJSON_CDECL internal_malloc(size_t size) +{ + return malloc(size); +} +static void CJSON_CDECL internal_free(void *pointer) +{ + free(pointer); +} +static void * CJSON_CDECL internal_realloc(void *pointer, size_t size) +{ + return realloc(pointer, size); +} +#else +#define internal_malloc malloc +#define internal_free free +#define internal_realloc realloc +#endif + +/* strlen of character literals resolved at compile time */ +#define static_strlen(string_literal) (sizeof(string_literal) - sizeof("")) + +static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; + +static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) +{ + size_t length = 0; + unsigned char *copy = NULL; + + if (string == NULL) + { + return NULL; + } + + length = strlen((const char*)string) + sizeof(""); + copy = (unsigned char*)hooks->allocate(length); + if (copy == NULL) + { + return NULL; + } + memcpy(copy, string, length); + + return copy; +} + +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks) +{ + if (hooks == NULL) + { + /* Reset hooks */ + global_hooks.allocate = malloc; + global_hooks.deallocate = free; + global_hooks.reallocate = realloc; + return; + } + + global_hooks.allocate = malloc; + if (hooks->malloc_fn != NULL) + { + global_hooks.allocate = hooks->malloc_fn; + } + + global_hooks.deallocate = free; + if (hooks->free_fn != NULL) + { + global_hooks.deallocate = hooks->free_fn; + } + + /* use realloc only if both free and malloc are used */ + global_hooks.reallocate = NULL; + if ((global_hooks.allocate == malloc) && (global_hooks.deallocate == free)) + { + global_hooks.reallocate = realloc; + } +} + +/* Internal constructor. */ +static cJSON *cJSON_New_Item(const internal_hooks * const hooks) +{ + cJSON* node = (cJSON*)hooks->allocate(sizeof(cJSON)); + if (node) + { + memset(node, '\0', sizeof(cJSON)); + } + + return node; +} + +/* Delete a cJSON structure. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item) +{ + cJSON *next = NULL; + while (item != NULL) + { + next = item->next; + if (!(item->type & cJSON_IsReference) && (item->child != NULL)) + { + cJSON_Delete(item->child); + } + if (!(item->type & cJSON_IsReference) && (item->valuestring != NULL)) + { + global_hooks.deallocate(item->valuestring); + } + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + global_hooks.deallocate(item->string); + } + global_hooks.deallocate(item); + item = next; + } +} + +/* get the decimal point character of the current locale */ +static unsigned char get_decimal_point(void) +{ +#ifdef ENABLE_LOCALES + struct lconv *lconv = localeconv(); + return (unsigned char) lconv->decimal_point[0]; +#else + return '.'; +#endif +} + +typedef struct +{ + const unsigned char *content; + size_t length; + size_t offset; + size_t depth; /* How deeply nested (in arrays/objects) is the input at the current offset. */ + internal_hooks hooks; +} parse_buffer; + +/* check if the given size is left to read in a given parse buffer (starting with 1) */ +#define can_read(buffer, size) ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) +/* check if the buffer can be accessed at the given index (starting with 0) */ +#define can_access_at_index(buffer, index) ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) +#define cannot_access_at_index(buffer, index) (!can_access_at_index(buffer, index)) +/* get a pointer to the buffer at the position */ +#define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) + +/* Parse the input text to generate a number, and populate the result into item. */ +static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) +{ + double number = 0; + unsigned char *after_end = NULL; + unsigned char number_c_string[64]; + unsigned char decimal_point = get_decimal_point(); + size_t i = 0; + + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; + } + + /* copy the number into a temporary buffer and replace '.' with the decimal point + * of the current locale (for strtod) + * This also takes care of '\0' not necessarily being available for marking the end of the input */ + for (i = 0; (i < (sizeof(number_c_string) - 1)) && can_access_at_index(input_buffer, i); i++) + { + switch (buffer_at_offset(input_buffer)[i]) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + case '-': + case 'e': + case 'E': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + break; + + case '.': + number_c_string[i] = decimal_point; + break; + + default: + goto loop_end; + } + } +loop_end: + number_c_string[i] = '\0'; + + number = strtod((const char*)number_c_string, (char**)&after_end); + if (number_c_string == after_end) + { + return false; /* parse_error */ + } + + item->valuedouble = number; + + /* use saturation in case of overflow */ + if (number >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)number; + } + + item->type = cJSON_Number; + + input_buffer->offset += (size_t)(after_end - number_c_string); + return true; +} + +/* don't ask me, but the original cJSON_SetNumberValue returns an integer or double */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number) +{ + if (number >= INT_MAX) + { + object->valueint = INT_MAX; + } + else if (number <= (double)INT_MIN) + { + object->valueint = INT_MIN; + } + else + { + object->valueint = (int)number; + } + + return object->valuedouble = number; +} + +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) +{ + char *copy = NULL; + /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ + if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference)) + { + return NULL; + } + if (strlen(valuestring) <= strlen(object->valuestring)) + { + strcpy(object->valuestring, valuestring); + return object->valuestring; + } + copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); + if (copy == NULL) + { + return NULL; + } + if (object->valuestring != NULL) + { + cJSON_free(object->valuestring); + } + object->valuestring = copy; + + return copy; +} + +typedef struct +{ + unsigned char *buffer; + size_t length; + size_t offset; + size_t depth; /* current nesting depth (for formatted printing) */ + cJSON_bool noalloc; + cJSON_bool format; /* is this print a formatted print */ + internal_hooks hooks; +} printbuffer; + +/* realloc printbuffer if necessary to have at least "needed" bytes more */ +static unsigned char* ensure(printbuffer * const p, size_t needed) +{ + unsigned char *newbuffer = NULL; + size_t newsize = 0; + + if ((p == NULL) || (p->buffer == NULL)) + { + return NULL; + } + + if ((p->length > 0) && (p->offset >= p->length)) + { + /* make sure that offset is valid */ + return NULL; + } + + if (needed > INT_MAX) + { + /* sizes bigger than INT_MAX are currently not supported */ + return NULL; + } + + needed += p->offset + 1; + if (needed <= p->length) + { + return p->buffer + p->offset; + } + + if (p->noalloc) { + return NULL; + } + + /* calculate new buffer size */ + if (needed > (INT_MAX / 2)) + { + /* overflow of int, use INT_MAX if possible */ + if (needed <= INT_MAX) + { + newsize = INT_MAX; + } + else + { + return NULL; + } + } + else + { + newsize = needed * 2; + } + + if (p->hooks.reallocate != NULL) + { + /* reallocate with realloc if available */ + newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize); + if (newbuffer == NULL) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + } + else + { + /* otherwise reallocate manually */ + newbuffer = (unsigned char*)p->hooks.allocate(newsize); + if (!newbuffer) + { + p->hooks.deallocate(p->buffer); + p->length = 0; + p->buffer = NULL; + + return NULL; + } + + memcpy(newbuffer, p->buffer, p->offset + 1); + p->hooks.deallocate(p->buffer); + } + p->length = newsize; + p->buffer = newbuffer; + + return newbuffer + p->offset; +} + +/* calculate the new length of the string in a printbuffer and update the offset */ +static void update_offset(printbuffer * const buffer) +{ + const unsigned char *buffer_pointer = NULL; + if ((buffer == NULL) || (buffer->buffer == NULL)) + { + return; + } + buffer_pointer = buffer->buffer + buffer->offset; + + buffer->offset += strlen((const char*)buffer_pointer); +} + +/* securely comparison of floating-point variables */ +static cJSON_bool compare_double(double a, double b) +{ + double maxVal = fabs(a) > fabs(b) ? fabs(a) : fabs(b); + return (fabs(a - b) <= maxVal * DBL_EPSILON); +} + +/* Render the number nicely from the given item into a string. */ +static cJSON_bool print_number(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + double d = item->valuedouble; + int length = 0; + size_t i = 0; + unsigned char number_buffer[26] = {0}; /* temporary buffer to print the number into */ + unsigned char decimal_point = get_decimal_point(); + double test = 0.0; + + if (output_buffer == NULL) + { + return false; + } + + /* This checks for NaN and Infinity */ + if (isnan(d) || isinf(d)) + { + length = sprintf((char*)number_buffer, "null"); + } + else if(d == (double)item->valueint) + { + length = sprintf((char*)number_buffer, "%d", item->valueint); + } + else + { + /* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */ + length = sprintf((char*)number_buffer, "%1.15g", d); + + /* Check whether the original double can be recovered */ + if ((sscanf((char*)number_buffer, "%lg", &test) != 1) || !compare_double((double)test, d)) + { + /* If not, print with 17 decimal places of precision */ + length = sprintf((char*)number_buffer, "%1.17g", d); + } + } + + /* sprintf failed or buffer overrun occurred */ + if ((length < 0) || (length > (int)(sizeof(number_buffer) - 1))) + { + return false; + } + + /* reserve appropriate space in the output */ + output_pointer = ensure(output_buffer, (size_t)length + sizeof("")); + if (output_pointer == NULL) + { + return false; + } + + /* copy the printed number to the output and replace locale + * dependent decimal point with '.' */ + for (i = 0; i < ((size_t)length); i++) + { + if (number_buffer[i] == decimal_point) + { + output_pointer[i] = '.'; + continue; + } + + output_pointer[i] = number_buffer[i]; + } + output_pointer[i] = '\0'; + + output_buffer->offset += (size_t)length; + + return true; +} + +/* parse 4 digit hexadecimal number */ +static unsigned parse_hex4(const unsigned char * const input) +{ + unsigned int h = 0; + size_t i = 0; + + for (i = 0; i < 4; i++) + { + /* parse digit */ + if ((input[i] >= '0') && (input[i] <= '9')) + { + h += (unsigned int) input[i] - '0'; + } + else if ((input[i] >= 'A') && (input[i] <= 'F')) + { + h += (unsigned int) 10 + input[i] - 'A'; + } + else if ((input[i] >= 'a') && (input[i] <= 'f')) + { + h += (unsigned int) 10 + input[i] - 'a'; + } + else /* invalid */ + { + return 0; + } + + if (i < 3) + { + /* shift left to make place for the next nibble */ + h = h << 4; + } + } + + return h; +} + +/* converts a UTF-16 literal to UTF-8 + * A literal can be one or two sequences of the form \uXXXX */ +static unsigned char utf16_literal_to_utf8(const unsigned char * const input_pointer, const unsigned char * const input_end, unsigned char **output_pointer) +{ + long unsigned int codepoint = 0; + unsigned int first_code = 0; + const unsigned char *first_sequence = input_pointer; + unsigned char utf8_length = 0; + unsigned char utf8_position = 0; + unsigned char sequence_length = 0; + unsigned char first_byte_mark = 0; + + if ((input_end - first_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + /* get the first utf16 sequence */ + first_code = parse_hex4(first_sequence + 2); + + /* check that the code is valid */ + if (((first_code >= 0xDC00) && (first_code <= 0xDFFF))) + { + goto fail; + } + + /* UTF16 surrogate pair */ + if ((first_code >= 0xD800) && (first_code <= 0xDBFF)) + { + const unsigned char *second_sequence = first_sequence + 6; + unsigned int second_code = 0; + sequence_length = 12; /* \uXXXX\uXXXX */ + + if ((input_end - second_sequence) < 6) + { + /* input ends unexpectedly */ + goto fail; + } + + if ((second_sequence[0] != '\\') || (second_sequence[1] != 'u')) + { + /* missing second half of the surrogate pair */ + goto fail; + } + + /* get the second utf16 sequence */ + second_code = parse_hex4(second_sequence + 2); + /* check that the code is valid */ + if ((second_code < 0xDC00) || (second_code > 0xDFFF)) + { + /* invalid second half of the surrogate pair */ + goto fail; + } + + + /* calculate the unicode codepoint from the surrogate pair */ + codepoint = 0x10000 + (((first_code & 0x3FF) << 10) | (second_code & 0x3FF)); + } + else + { + sequence_length = 6; /* \uXXXX */ + codepoint = first_code; + } + + /* encode as UTF-8 + * takes at maximum 4 bytes to encode: + * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ + if (codepoint < 0x80) + { + /* normal ascii, encoding 0xxxxxxx */ + utf8_length = 1; + } + else if (codepoint < 0x800) + { + /* two bytes, encoding 110xxxxx 10xxxxxx */ + utf8_length = 2; + first_byte_mark = 0xC0; /* 11000000 */ + } + else if (codepoint < 0x10000) + { + /* three bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx */ + utf8_length = 3; + first_byte_mark = 0xE0; /* 11100000 */ + } + else if (codepoint <= 0x10FFFF) + { + /* four bytes, encoding 1110xxxx 10xxxxxx 10xxxxxx 10xxxxxx */ + utf8_length = 4; + first_byte_mark = 0xF0; /* 11110000 */ + } + else + { + /* invalid unicode codepoint */ + goto fail; + } + + /* encode as utf8 */ + for (utf8_position = (unsigned char)(utf8_length - 1); utf8_position > 0; utf8_position--) + { + /* 10xxxxxx */ + (*output_pointer)[utf8_position] = (unsigned char)((codepoint | 0x80) & 0xBF); + codepoint >>= 6; + } + /* encode first byte */ + if (utf8_length > 1) + { + (*output_pointer)[0] = (unsigned char)((codepoint | first_byte_mark) & 0xFF); + } + else + { + (*output_pointer)[0] = (unsigned char)(codepoint & 0x7F); + } + + *output_pointer += utf8_length; + + return sequence_length; + +fail: + return 0; +} + +/* Parse the input text into an unescaped cinput, and populate item. */ +static cJSON_bool parse_string(cJSON * const item, parse_buffer * const input_buffer) +{ + const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; + const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; + unsigned char *output_pointer = NULL; + unsigned char *output = NULL; + + /* not a string */ + if (buffer_at_offset(input_buffer)[0] != '\"') + { + goto fail; + } + + { + /* calculate approximate size of the output (overestimate) */ + size_t allocation_length = 0; + size_t skipped_bytes = 0; + while (((size_t)(input_end - input_buffer->content) < input_buffer->length) && (*input_end != '\"')) + { + /* is escape sequence */ + if (input_end[0] == '\\') + { + if ((size_t)(input_end + 1 - input_buffer->content) >= input_buffer->length) + { + /* prevent buffer overflow when last input character is a backslash */ + goto fail; + } + skipped_bytes++; + input_end++; + } + input_end++; + } + if (((size_t)(input_end - input_buffer->content) >= input_buffer->length) || (*input_end != '\"')) + { + goto fail; /* string ended unexpectedly */ + } + + /* This is at most how much we need for the output */ + allocation_length = (size_t) (input_end - buffer_at_offset(input_buffer)) - skipped_bytes; + output = (unsigned char*)input_buffer->hooks.allocate(allocation_length + sizeof("")); + if (output == NULL) + { + goto fail; /* allocation failure */ + } + } + + output_pointer = output; + /* loop through the string literal */ + while (input_pointer < input_end) + { + if (*input_pointer != '\\') + { + *output_pointer++ = *input_pointer++; + } + /* escape sequence */ + else + { + unsigned char sequence_length = 2; + if ((input_end - input_pointer) < 1) + { + goto fail; + } + + switch (input_pointer[1]) + { + case 'b': + *output_pointer++ = '\b'; + break; + case 'f': + *output_pointer++ = '\f'; + break; + case 'n': + *output_pointer++ = '\n'; + break; + case 'r': + *output_pointer++ = '\r'; + break; + case 't': + *output_pointer++ = '\t'; + break; + case '\"': + case '\\': + case '/': + *output_pointer++ = input_pointer[1]; + break; + + /* UTF-16 literal */ + case 'u': + sequence_length = utf16_literal_to_utf8(input_pointer, input_end, &output_pointer); + if (sequence_length == 0) + { + /* failed to convert UTF16-literal to UTF-8 */ + goto fail; + } + break; + + default: + goto fail; + } + input_pointer += sequence_length; + } + } + + /* zero terminate the output */ + *output_pointer = '\0'; + + item->type = cJSON_String; + item->valuestring = (char*)output; + + input_buffer->offset = (size_t) (input_end - input_buffer->content); + input_buffer->offset++; + + return true; + +fail: + if (output != NULL) + { + input_buffer->hooks.deallocate(output); + } + + if (input_pointer != NULL) + { + input_buffer->offset = (size_t)(input_pointer - input_buffer->content); + } + + return false; +} + +/* Render the cstring provided to an escaped version that can be printed. */ +static cJSON_bool print_string_ptr(const unsigned char * const input, printbuffer * const output_buffer) +{ + const unsigned char *input_pointer = NULL; + unsigned char *output = NULL; + unsigned char *output_pointer = NULL; + size_t output_length = 0; + /* numbers of additional characters needed for escaping */ + size_t escape_characters = 0; + + if (output_buffer == NULL) + { + return false; + } + + /* empty string */ + if (input == NULL) + { + output = ensure(output_buffer, sizeof("\"\"")); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "\"\""); + + return true; + } + + /* set "flag" to 1 if something needs to be escaped */ + for (input_pointer = input; *input_pointer; input_pointer++) + { + switch (*input_pointer) + { + case '\"': + case '\\': + case '\b': + case '\f': + case '\n': + case '\r': + case '\t': + /* one character escape sequence */ + escape_characters++; + break; + default: + if (*input_pointer < 32) + { + /* UTF-16 escape sequence uXXXX */ + escape_characters += 5; + } + break; + } + } + output_length = (size_t)(input_pointer - input) + escape_characters; + + output = ensure(output_buffer, output_length + sizeof("\"\"")); + if (output == NULL) + { + return false; + } + + /* no characters have to be escaped */ + if (escape_characters == 0) + { + output[0] = '\"'; + memcpy(output + 1, input, output_length); + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; + } + + output[0] = '\"'; + output_pointer = output + 1; + /* copy the string */ + for (input_pointer = input; *input_pointer != '\0'; (void)input_pointer++, output_pointer++) + { + if ((*input_pointer > 31) && (*input_pointer != '\"') && (*input_pointer != '\\')) + { + /* normal character, copy */ + *output_pointer = *input_pointer; + } + else + { + /* character needs to be escaped */ + *output_pointer++ = '\\'; + switch (*input_pointer) + { + case '\\': + *output_pointer = '\\'; + break; + case '\"': + *output_pointer = '\"'; + break; + case '\b': + *output_pointer = 'b'; + break; + case '\f': + *output_pointer = 'f'; + break; + case '\n': + *output_pointer = 'n'; + break; + case '\r': + *output_pointer = 'r'; + break; + case '\t': + *output_pointer = 't'; + break; + default: + /* escape and print as unicode codepoint */ + sprintf((char*)output_pointer, "u%04x", *input_pointer); + output_pointer += 4; + break; + } + } + } + output[output_length + 1] = '\"'; + output[output_length + 2] = '\0'; + + return true; +} + +/* Invoke print_string_ptr (which is useful) on an item. */ +static cJSON_bool print_string(const cJSON * const item, printbuffer * const p) +{ + return print_string_ptr((unsigned char*)item->valuestring, p); +} + +/* Predeclare these prototypes. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer); +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer); +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer); + +/* Utility to jump whitespace and cr/lf */ +static parse_buffer *buffer_skip_whitespace(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL)) + { + return NULL; + } + + if (cannot_access_at_index(buffer, 0)) + { + return buffer; + } + + while (can_access_at_index(buffer, 0) && (buffer_at_offset(buffer)[0] <= 32)) + { + buffer->offset++; + } + + if (buffer->offset == buffer->length) + { + buffer->offset--; + } + + return buffer; +} + +/* skip the UTF-8 BOM (byte order mark) if it is at the beginning of a buffer */ +static parse_buffer *skip_utf8_bom(parse_buffer * const buffer) +{ + if ((buffer == NULL) || (buffer->content == NULL) || (buffer->offset != 0)) + { + return NULL; + } + + if (can_access_at_index(buffer, 4) && (strncmp((const char*)buffer_at_offset(buffer), "\xEF\xBB\xBF", 3) == 0)) + { + buffer->offset += 3; + } + + return buffer; +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + size_t buffer_length; + + if (NULL == value) + { + return NULL; + } + + /* Adding null character size due to require_null_terminated. */ + buffer_length = strlen(value) + sizeof(""); + + return cJSON_ParseWithLengthOpts(value, buffer_length, return_parse_end, require_null_terminated); +} + +/* Parse an object - create a new root, and populate. */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated) +{ + parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; + cJSON *item = NULL; + + /* reset error position */ + global_error.json = NULL; + global_error.position = 0; + + if (value == NULL || 0 == buffer_length) + { + goto fail; + } + + buffer.content = (const unsigned char*)value; + buffer.length = buffer_length; + buffer.offset = 0; + buffer.hooks = global_hooks; + + item = cJSON_New_Item(&global_hooks); + if (item == NULL) /* memory fail */ + { + goto fail; + } + + if (!parse_value(item, buffer_skip_whitespace(skip_utf8_bom(&buffer)))) + { + /* parse failure. ep is set. */ + goto fail; + } + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) + { + buffer_skip_whitespace(&buffer); + if ((buffer.offset >= buffer.length) || buffer_at_offset(&buffer)[0] != '\0') + { + goto fail; + } + } + if (return_parse_end) + { + *return_parse_end = (const char*)buffer_at_offset(&buffer); + } + + return item; + +fail: + if (item != NULL) + { + cJSON_Delete(item); + } + + if (value != NULL) + { + error local_error; + local_error.json = (const unsigned char*)value; + local_error.position = 0; + + if (buffer.offset < buffer.length) + { + local_error.position = buffer.offset; + } + else if (buffer.length > 0) + { + local_error.position = buffer.length - 1; + } + + if (return_parse_end != NULL) + { + *return_parse_end = (const char*)local_error.json + local_error.position; + } + + global_error = local_error; + } + + return NULL; +} + +/* Default options for cJSON_Parse */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value) +{ + return cJSON_ParseWithOpts(value, 0, 0); +} + +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length) +{ + return cJSON_ParseWithLengthOpts(value, buffer_length, 0, 0); +} + +#define cjson_min(a, b) (((a) < (b)) ? (a) : (b)) + +static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks) +{ + static const size_t default_buffer_size = 256; + printbuffer buffer[1]; + unsigned char *printed = NULL; + + memset(buffer, 0, sizeof(buffer)); + + /* create buffer */ + buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size); + buffer->length = default_buffer_size; + buffer->format = format; + buffer->hooks = *hooks; + if (buffer->buffer == NULL) + { + goto fail; + } + + /* print the value */ + if (!print_value(item, buffer)) + { + goto fail; + } + update_offset(buffer); + + /* check if reallocate is available */ + if (hooks->reallocate != NULL) + { + printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1); + if (printed == NULL) { + goto fail; + } + buffer->buffer = NULL; + } + else /* otherwise copy the JSON over to a new buffer */ + { + printed = (unsigned char*) hooks->allocate(buffer->offset + 1); + if (printed == NULL) + { + goto fail; + } + memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1)); + printed[buffer->offset] = '\0'; /* just to be sure */ + + /* free the buffer */ + hooks->deallocate(buffer->buffer); + } + + return printed; + +fail: + if (buffer->buffer != NULL) + { + hooks->deallocate(buffer->buffer); + } + + if (printed != NULL) + { + hooks->deallocate(printed); + } + + return NULL; +} + +/* Render a cJSON item/entity/structure to text. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item) +{ + return (char*)print(item, true, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item) +{ + return (char*)print(item, false, &global_hooks); +} + +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if (prebuffer < 0) + { + return NULL; + } + + p.buffer = (unsigned char*)global_hooks.allocate((size_t)prebuffer); + if (!p.buffer) + { + return NULL; + } + + p.length = (size_t)prebuffer; + p.offset = 0; + p.noalloc = false; + p.format = fmt; + p.hooks = global_hooks; + + if (!print_value(item, &p)) + { + global_hooks.deallocate(p.buffer); + return NULL; + } + + return (char*)p.buffer; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format) +{ + printbuffer p = { 0, 0, 0, 0, 0, 0, { 0, 0, 0 } }; + + if ((length < 0) || (buffer == NULL)) + { + return false; + } + + p.buffer = (unsigned char*)buffer; + p.length = (size_t)length; + p.offset = 0; + p.noalloc = true; + p.format = format; + p.hooks = global_hooks; + + return print_value(item, &p); +} + +/* Parser core - when encountering text, process appropriately. */ +static cJSON_bool parse_value(cJSON * const item, parse_buffer * const input_buffer) +{ + if ((input_buffer == NULL) || (input_buffer->content == NULL)) + { + return false; /* no input */ + } + + /* parse the different types of values */ + /* null */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "null", 4) == 0)) + { + item->type = cJSON_NULL; + input_buffer->offset += 4; + return true; + } + /* false */ + if (can_read(input_buffer, 5) && (strncmp((const char*)buffer_at_offset(input_buffer), "false", 5) == 0)) + { + item->type = cJSON_False; + input_buffer->offset += 5; + return true; + } + /* true */ + if (can_read(input_buffer, 4) && (strncmp((const char*)buffer_at_offset(input_buffer), "true", 4) == 0)) + { + item->type = cJSON_True; + item->valueint = 1; + input_buffer->offset += 4; + return true; + } + /* string */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '\"')) + { + return parse_string(item, input_buffer); + } + /* number */ + if (can_access_at_index(input_buffer, 0) && ((buffer_at_offset(input_buffer)[0] == '-') || ((buffer_at_offset(input_buffer)[0] >= '0') && (buffer_at_offset(input_buffer)[0] <= '9')))) + { + return parse_number(item, input_buffer); + } + /* array */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '[')) + { + return parse_array(item, input_buffer); + } + /* object */ + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '{')) + { + return parse_object(item, input_buffer); + } + + return false; +} + +/* Render a value to text. */ +static cJSON_bool print_value(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output = NULL; + + if ((item == NULL) || (output_buffer == NULL)) + { + return false; + } + + switch ((item->type) & 0xFF) + { + case cJSON_NULL: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "null"); + return true; + + case cJSON_False: + output = ensure(output_buffer, 6); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "false"); + return true; + + case cJSON_True: + output = ensure(output_buffer, 5); + if (output == NULL) + { + return false; + } + strcpy((char*)output, "true"); + return true; + + case cJSON_Number: + return print_number(item, output_buffer); + + case cJSON_Raw: + { + size_t raw_length = 0; + if (item->valuestring == NULL) + { + return false; + } + + raw_length = strlen(item->valuestring) + sizeof(""); + output = ensure(output_buffer, raw_length); + if (output == NULL) + { + return false; + } + memcpy(output, item->valuestring, raw_length); + return true; + } + + case cJSON_String: + return print_string(item, output_buffer); + + case cJSON_Array: + return print_array(item, output_buffer); + + case cJSON_Object: + return print_object(item, output_buffer); + + default: + return false; + } +} + +/* Build an array from input text. */ +static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* head of the linked list */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (buffer_at_offset(input_buffer)[0] != '[') + { + /* not an array */ + goto fail; + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ']')) + { + /* empty array */ + goto success; + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse next value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || buffer_at_offset(input_buffer)[0] != ']') + { + goto fail; /* expected end of array */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Array; + item->child = head; + + input_buffer->offset++; + + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an array to text */ +static cJSON_bool print_array(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_element = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output array. */ + /* opening square bracket */ + output_pointer = ensure(output_buffer, 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer = '['; + output_buffer->offset++; + output_buffer->depth++; + + while (current_element != NULL) + { + if (!print_value(current_element, output_buffer)) + { + return false; + } + update_offset(output_buffer); + if (current_element->next) + { + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ','; + if(output_buffer->format) + { + *output_pointer++ = ' '; + } + *output_pointer = '\0'; + output_buffer->offset += length; + } + current_element = current_element->next; + } + + output_pointer = ensure(output_buffer, 2); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ']'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Build an object from the text. */ +static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_buffer) +{ + cJSON *head = NULL; /* linked list head */ + cJSON *current_item = NULL; + + if (input_buffer->depth >= CJSON_NESTING_LIMIT) + { + return false; /* to deeply nested */ + } + input_buffer->depth++; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '{')) + { + goto fail; /* not an object */ + } + + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == '}')) + { + goto success; /* empty object */ + } + + /* check if we skipped to the end of the buffer */ + if (cannot_access_at_index(input_buffer, 0)) + { + input_buffer->offset--; + goto fail; + } + + /* step back to character in front of the first element */ + input_buffer->offset--; + /* loop through the comma separated array elements */ + do + { + /* allocate next item */ + cJSON *new_item = cJSON_New_Item(&(input_buffer->hooks)); + if (new_item == NULL) + { + goto fail; /* allocation failure */ + } + + /* attach next item to list */ + if (head == NULL) + { + /* start the linked list */ + current_item = head = new_item; + } + else + { + /* add to the end and advance */ + current_item->next = new_item; + new_item->prev = current_item; + current_item = new_item; + } + + /* parse the name of the child */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_string(current_item, input_buffer)) + { + goto fail; /* failed to parse name */ + } + buffer_skip_whitespace(input_buffer); + + /* swap valuestring and string, because we parsed the name */ + current_item->string = current_item->valuestring; + current_item->valuestring = NULL; + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != ':')) + { + goto fail; /* invalid object */ + } + + /* parse the value */ + input_buffer->offset++; + buffer_skip_whitespace(input_buffer); + if (!parse_value(current_item, input_buffer)) + { + goto fail; /* failed to parse value */ + } + buffer_skip_whitespace(input_buffer); + } + while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ',')); + + if (cannot_access_at_index(input_buffer, 0) || (buffer_at_offset(input_buffer)[0] != '}')) + { + goto fail; /* expected end of object */ + } + +success: + input_buffer->depth--; + + if (head != NULL) { + head->prev = current_item; + } + + item->type = cJSON_Object; + item->child = head; + + input_buffer->offset++; + return true; + +fail: + if (head != NULL) + { + cJSON_Delete(head); + } + + return false; +} + +/* Render an object to text. */ +static cJSON_bool print_object(const cJSON * const item, printbuffer * const output_buffer) +{ + unsigned char *output_pointer = NULL; + size_t length = 0; + cJSON *current_item = item->child; + + if (output_buffer == NULL) + { + return false; + } + + /* Compose the output: */ + length = (size_t) (output_buffer->format ? 2 : 1); /* fmt: {\n */ + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + + *output_pointer++ = '{'; + output_buffer->depth++; + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + output_buffer->offset += length; + + while (current_item) + { + if (output_buffer->format) + { + size_t i; + output_pointer = ensure(output_buffer, output_buffer->depth); + if (output_pointer == NULL) + { + return false; + } + for (i = 0; i < output_buffer->depth; i++) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += output_buffer->depth; + } + + /* print key */ + if (!print_string_ptr((unsigned char*)current_item->string, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + length = (size_t) (output_buffer->format ? 2 : 1); + output_pointer = ensure(output_buffer, length); + if (output_pointer == NULL) + { + return false; + } + *output_pointer++ = ':'; + if (output_buffer->format) + { + *output_pointer++ = '\t'; + } + output_buffer->offset += length; + + /* print value */ + if (!print_value(current_item, output_buffer)) + { + return false; + } + update_offset(output_buffer); + + /* print comma if not last */ + length = ((size_t)(output_buffer->format ? 1 : 0) + (size_t)(current_item->next ? 1 : 0)); + output_pointer = ensure(output_buffer, length + 1); + if (output_pointer == NULL) + { + return false; + } + if (current_item->next) + { + *output_pointer++ = ','; + } + + if (output_buffer->format) + { + *output_pointer++ = '\n'; + } + *output_pointer = '\0'; + output_buffer->offset += length; + + current_item = current_item->next; + } + + output_pointer = ensure(output_buffer, output_buffer->format ? (output_buffer->depth + 1) : 2); + if (output_pointer == NULL) + { + return false; + } + if (output_buffer->format) + { + size_t i; + for (i = 0; i < (output_buffer->depth - 1); i++) + { + *output_pointer++ = '\t'; + } + } + *output_pointer++ = '}'; + *output_pointer = '\0'; + output_buffer->depth--; + + return true; +} + +/* Get Array size/item / object item. */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) +{ + cJSON *child = NULL; + size_t size = 0; + + if (array == NULL) + { + return 0; + } + + child = array->child; + + while(child != NULL) + { + size++; + child = child->next; + } + + /* FIXME: Can overflow here. Cannot be fixed without breaking the API */ + + return (int)size; +} + +static cJSON* get_array_item(const cJSON *array, size_t index) +{ + cJSON *current_child = NULL; + + if (array == NULL) + { + return NULL; + } + + current_child = array->child; + while ((current_child != NULL) && (index > 0)) + { + index--; + current_child = current_child->next; + } + + return current_child; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) +{ + if (index < 0) + { + return NULL; + } + + return get_array_item(array, (size_t)index); +} + +static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) +{ + cJSON *current_element = NULL; + + if ((object == NULL) || (name == NULL)) + { + return NULL; + } + + current_element = object->child; + if (case_sensitive) + { + while ((current_element != NULL) && (current_element->string != NULL) && (strcmp(name, current_element->string) != 0)) + { + current_element = current_element->next; + } + } + else + { + while ((current_element != NULL) && (case_insensitive_strcmp((const unsigned char*)name, (const unsigned char*)(current_element->string)) != 0)) + { + current_element = current_element->next; + } + } + + if ((current_element == NULL) || (current_element->string == NULL)) { + return NULL; + } + + return current_element; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, false); +} + +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string) +{ + return get_object_item(object, string, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string) +{ + return cJSON_GetObjectItem(object, string) ? 1 : 0; +} + +/* Utility for array list handling. */ +static void suffix_object(cJSON *prev, cJSON *item) +{ + prev->next = item; + item->prev = prev; +} + +/* Utility for handling references. */ +static cJSON *create_reference(const cJSON *item, const internal_hooks * const hooks) +{ + cJSON *reference = NULL; + if (item == NULL) + { + return NULL; + } + + reference = cJSON_New_Item(hooks); + if (reference == NULL) + { + return NULL; + } + + memcpy(reference, item, sizeof(cJSON)); + reference->string = NULL; + reference->type |= cJSON_IsReference; + reference->next = reference->prev = NULL; + return reference; +} + +static cJSON_bool add_item_to_array(cJSON *array, cJSON *item) +{ + cJSON *child = NULL; + + if ((item == NULL) || (array == NULL) || (array == item)) + { + return false; + } + + child = array->child; + /* + * To find the last item in array quickly, we use prev in array + */ + if (child == NULL) + { + /* list is empty, start new one */ + array->child = item; + item->prev = item; + item->next = NULL; + } + else + { + /* append to the end */ + if (child->prev) + { + suffix_object(child->prev, item); + array->child->prev = item; + } + } + + return true; +} + +/* Add item to array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) +{ + return add_item_to_array(array, item); +} + +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic push +#endif +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wcast-qual" +#endif +/* helper function to cast away const */ +static void* cast_away_const(const void* string) +{ + return (void*)string; +} +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) + #pragma GCC diagnostic pop +#endif + + +static cJSON_bool add_item_to_object(cJSON * const object, const char * const string, cJSON * const item, const internal_hooks * const hooks, const cJSON_bool constant_key) +{ + char *new_key = NULL; + int new_type = cJSON_Invalid; + + if ((object == NULL) || (string == NULL) || (item == NULL) || (object == item)) + { + return false; + } + + if (constant_key) + { + new_key = (char*)cast_away_const(string); + new_type = item->type | cJSON_StringIsConst; + } + else + { + new_key = (char*)cJSON_strdup((const unsigned char*)string, hooks); + if (new_key == NULL) + { + return false; + } + + new_type = item->type & ~cJSON_StringIsConst; + } + + if (!(item->type & cJSON_StringIsConst) && (item->string != NULL)) + { + hooks->deallocate(item->string); + } + + item->string = new_key; + item->type = new_type; + + return add_item_to_array(object, item); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, false); +} + +/* Add an item to an object with constant string as key */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item) +{ + return add_item_to_object(object, string, item, &global_hooks, true); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) +{ + if (array == NULL) + { + return false; + } + + return add_item_to_array(array, create_reference(item, &global_hooks)); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item) +{ + if ((object == NULL) || (string == NULL)) + { + return false; + } + + return add_item_to_object(object, string, create_reference(item, &global_hooks), &global_hooks, false); +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name) +{ + cJSON *null = cJSON_CreateNull(); + if (add_item_to_object(object, name, null, &global_hooks, false)) + { + return null; + } + + cJSON_Delete(null); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name) +{ + cJSON *true_item = cJSON_CreateTrue(); + if (add_item_to_object(object, name, true_item, &global_hooks, false)) + { + return true_item; + } + + cJSON_Delete(true_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name) +{ + cJSON *false_item = cJSON_CreateFalse(); + if (add_item_to_object(object, name, false_item, &global_hooks, false)) + { + return false_item; + } + + cJSON_Delete(false_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean) +{ + cJSON *bool_item = cJSON_CreateBool(boolean); + if (add_item_to_object(object, name, bool_item, &global_hooks, false)) + { + return bool_item; + } + + cJSON_Delete(bool_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number) +{ + cJSON *number_item = cJSON_CreateNumber(number); + if (add_item_to_object(object, name, number_item, &global_hooks, false)) + { + return number_item; + } + + cJSON_Delete(number_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) +{ + cJSON *string_item = cJSON_CreateString(string); + if (add_item_to_object(object, name, string_item, &global_hooks, false)) + { + return string_item; + } + + cJSON_Delete(string_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw) +{ + cJSON *raw_item = cJSON_CreateRaw(raw); + if (add_item_to_object(object, name, raw_item, &global_hooks, false)) + { + return raw_item; + } + + cJSON_Delete(raw_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name) +{ + cJSON *object_item = cJSON_CreateObject(); + if (add_item_to_object(object, name, object_item, &global_hooks, false)) + { + return object_item; + } + + cJSON_Delete(object_item); + return NULL; +} + +CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name) +{ + cJSON *array = cJSON_CreateArray(); + if (add_item_to_object(object, name, array, &global_hooks, false)) + { + return array; + } + + cJSON_Delete(array); + return NULL; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) +{ + if ((parent == NULL) || (item == NULL)) + { + return NULL; + } + + if (item != parent->child) + { + /* not the first element */ + item->prev->next = item->next; + } + if (item->next != NULL) + { + /* not the last element */ + item->next->prev = item->prev; + } + + if (item == parent->child) + { + /* first element */ + parent->child = item->next; + } + else if (item->next == NULL) + { + /* last element */ + parent->child->prev = item->prev; + } + + /* make sure the detached item doesn't point anywhere anymore */ + item->prev = NULL; + item->next = NULL; + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) +{ + if (which < 0) + { + return NULL; + } + + return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) +{ + cJSON_Delete(cJSON_DetachItemFromArray(array, which)); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItem(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObject(object, string)); +} + +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); +} + +/* Replace array/object items with new ones. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) +{ + cJSON *after_inserted = NULL; + + if (which < 0) + { + return false; + } + + after_inserted = get_array_item(array, (size_t)which); + if (after_inserted == NULL) + { + return add_item_to_array(array, newitem); + } + + newitem->next = after_inserted; + newitem->prev = after_inserted->prev; + after_inserted->prev = newitem; + if (after_inserted == array->child) + { + array->child = newitem; + } + else + { + newitem->prev->next = newitem; + } + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) +{ + if ((parent == NULL) || (parent->child == NULL) || (replacement == NULL) || (item == NULL)) + { + return false; + } + + if (replacement == item) + { + return true; + } + + replacement->next = item->next; + replacement->prev = item->prev; + + if (replacement->next != NULL) + { + replacement->next->prev = replacement; + } + if (parent->child == item) + { + if (parent->child->prev == parent->child) + { + replacement->prev = replacement; + } + parent->child = replacement; + } + else + { /* + * To find the last item in array quickly, we use prev in array. + * We can't modify the last item's next pointer where this item was the parent's child + */ + if (replacement->prev != NULL) + { + replacement->prev->next = replacement; + } + if (replacement->next == NULL) + { + parent->child->prev = replacement; + } + } + + item->next = NULL; + item->prev = NULL; + cJSON_Delete(item); + + return true; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) +{ + if (which < 0) + { + return false; + } + + return cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); +} + +static cJSON_bool replace_item_in_object(cJSON *object, const char *string, cJSON *replacement, cJSON_bool case_sensitive) +{ + if ((replacement == NULL) || (string == NULL)) + { + return false; + } + + /* replace the name in the replacement */ + if (!(replacement->type & cJSON_StringIsConst) && (replacement->string != NULL)) + { + cJSON_free(replacement->string); + } + replacement->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if (replacement->string == NULL) + { + return false; + } + + replacement->type &= ~cJSON_StringIsConst; + + return cJSON_ReplaceItemViaPointer(object, get_object_item(object, string, case_sensitive), replacement); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, false); +} + +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) +{ + return replace_item_in_object(object, string, newitem, true); +} + +/* Create basic types: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_NULL; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_True; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = boolean ? cJSON_True : cJSON_False; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Number; + item->valuedouble = num; + + /* use saturation in case of overflow */ + if (num >= INT_MAX) + { + item->valueint = INT_MAX; + } + else if (num <= (double)INT_MIN) + { + item->valueint = INT_MIN; + } + else + { + item->valueint = (int)num; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_String; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) + { + item->type = cJSON_String | cJSON_IsReference; + item->valuestring = (char*)cast_away_const(string); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Object | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child) { + cJSON *item = cJSON_New_Item(&global_hooks); + if (item != NULL) { + item->type = cJSON_Array | cJSON_IsReference; + item->child = (cJSON*)cast_away_const(child); + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Raw; + item->valuestring = (char*)cJSON_strdup((const unsigned char*)raw, &global_hooks); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type=cJSON_Array; + } + + return item; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if (item) + { + item->type = cJSON_Object; + } + + return item; +} + +/* Create Arrays: */ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if (!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber((double)numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (numbers == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for(i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateNumber(numbers[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p, n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count) +{ + size_t i = 0; + cJSON *n = NULL; + cJSON *p = NULL; + cJSON *a = NULL; + + if ((count < 0) || (strings == NULL)) + { + return NULL; + } + + a = cJSON_CreateArray(); + + for (i = 0; a && (i < (size_t)count); i++) + { + n = cJSON_CreateString(strings[i]); + if(!n) + { + cJSON_Delete(a); + return NULL; + } + if(!i) + { + a->child = n; + } + else + { + suffix_object(p,n); + } + p = n; + } + + if (a && a->child) { + a->child->prev = n; + } + + return a; +} + +/* Duplication */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse) +{ + cJSON *newitem = NULL; + cJSON *child = NULL; + cJSON *next = NULL; + cJSON *newchild = NULL; + + /* Bail on bad ptr */ + if (!item) + { + goto fail; + } + /* Create new item */ + newitem = cJSON_New_Item(&global_hooks); + if (!newitem) + { + goto fail; + } + /* Copy over all vars */ + newitem->type = item->type & (~cJSON_IsReference); + newitem->valueint = item->valueint; + newitem->valuedouble = item->valuedouble; + if (item->valuestring) + { + newitem->valuestring = (char*)cJSON_strdup((unsigned char*)item->valuestring, &global_hooks); + if (!newitem->valuestring) + { + goto fail; + } + } + if (item->string) + { + newitem->string = (item->type&cJSON_StringIsConst) ? item->string : (char*)cJSON_strdup((unsigned char*)item->string, &global_hooks); + if (!newitem->string) + { + goto fail; + } + } + /* If non-recursive, then we're done! */ + if (!recurse) + { + return newitem; + } + /* Walk the ->next chain for the child. */ + child = item->child; + while (child != NULL) + { + newchild = cJSON_Duplicate(child, true); /* Duplicate (with recurse) each item in the ->next chain */ + if (!newchild) + { + goto fail; + } + if (next != NULL) + { + /* If newitem->child already set, then crosswire ->prev and ->next and move on */ + next->next = newchild; + newchild->prev = next; + next = newchild; + } + else + { + /* Set newitem->child and move to it */ + newitem->child = newchild; + next = newchild; + } + child = child->next; + } + if (newitem && newitem->child) + { + newitem->child->prev = newchild; + } + + return newitem; + +fail: + if (newitem != NULL) + { + cJSON_Delete(newitem); + } + + return NULL; +} + +static void skip_oneline_comment(char **input) +{ + *input += static_strlen("//"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if ((*input)[0] == '\n') { + *input += static_strlen("\n"); + return; + } + } +} + +static void skip_multiline_comment(char **input) +{ + *input += static_strlen("/*"); + + for (; (*input)[0] != '\0'; ++(*input)) + { + if (((*input)[0] == '*') && ((*input)[1] == '/')) + { + *input += static_strlen("*/"); + return; + } + } +} + +static void minify_string(char **input, char **output) { + (*output)[0] = (*input)[0]; + *input += static_strlen("\""); + *output += static_strlen("\""); + + + for (; (*input)[0] != '\0'; (void)++(*input), ++(*output)) { + (*output)[0] = (*input)[0]; + + if ((*input)[0] == '\"') { + (*output)[0] = '\"'; + *input += static_strlen("\""); + *output += static_strlen("\""); + return; + } else if (((*input)[0] == '\\') && ((*input)[1] == '\"')) { + (*output)[1] = (*input)[1]; + *input += static_strlen("\""); + *output += static_strlen("\""); + } + } +} + +CJSON_PUBLIC(void) cJSON_Minify(char *json) +{ + char *into = json; + + if (json == NULL) + { + return; + } + + while (json[0] != '\0') + { + switch (json[0]) + { + case ' ': + case '\t': + case '\r': + case '\n': + json++; + break; + + case '/': + if (json[1] == '/') + { + skip_oneline_comment(&json); + } + else if (json[1] == '*') + { + skip_multiline_comment(&json); + } else { + json++; + } + break; + + case '\"': + minify_string(&json, (char**)&into); + break; + + default: + into[0] = json[0]; + json++; + into++; + } + } + + /* and null-terminate. */ + *into = '\0'; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Invalid; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_False; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xff) == cJSON_True; +} + + +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & (cJSON_True | cJSON_False)) != 0; +} +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_NULL; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Number; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_String; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Array; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Object; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Raw; +} + +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive) +{ + if ((a == NULL) || (b == NULL) || ((a->type & 0xFF) != (b->type & 0xFF))) + { + return false; + } + + /* check if type is valid */ + switch (a->type & 0xFF) + { + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + case cJSON_Number: + case cJSON_String: + case cJSON_Raw: + case cJSON_Array: + case cJSON_Object: + break; + + default: + return false; + } + + /* identical objects are equal */ + if (a == b) + { + return true; + } + + switch (a->type & 0xFF) + { + /* in these cases and equal type is enough */ + case cJSON_False: + case cJSON_True: + case cJSON_NULL: + return true; + + case cJSON_Number: + if (compare_double(a->valuedouble, b->valuedouble)) + { + return true; + } + return false; + + case cJSON_String: + case cJSON_Raw: + if ((a->valuestring == NULL) || (b->valuestring == NULL)) + { + return false; + } + if (strcmp(a->valuestring, b->valuestring) == 0) + { + return true; + } + + return false; + + case cJSON_Array: + { + cJSON *a_element = a->child; + cJSON *b_element = b->child; + + for (; (a_element != NULL) && (b_element != NULL);) + { + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + + a_element = a_element->next; + b_element = b_element->next; + } + + /* one of the arrays is longer than the other */ + if (a_element != b_element) { + return false; + } + + return true; + } + + case cJSON_Object: + { + cJSON *a_element = NULL; + cJSON *b_element = NULL; + cJSON_ArrayForEach(a_element, a) + { + /* TODO This has O(n^2) runtime, which is horrible! */ + b_element = get_object_item(b, a_element->string, case_sensitive); + if (b_element == NULL) + { + return false; + } + + if (!cJSON_Compare(a_element, b_element, case_sensitive)) + { + return false; + } + } + + /* doing this twice, once on a and b to prevent true comparison if a subset of b + * TODO: Do this the proper way, this is just a fix for now */ + cJSON_ArrayForEach(b_element, b) + { + a_element = get_object_item(a, b_element->string, case_sensitive); + if (a_element == NULL) + { + return false; + } + + if (!cJSON_Compare(b_element, a_element, case_sensitive)) + { + return false; + } + } + + return true; + } + + default: + return false; + } +} + +CJSON_PUBLIC(void *) cJSON_malloc(size_t size) +{ + return global_hooks.allocate(size); +} + +CJSON_PUBLIC(void) cJSON_free(void *object) +{ + global_hooks.deallocate(object); +} diff --git a/testRoot/jni/utils/cJSON.h b/testRoot/jni/utils/cJSON.h new file mode 100644 index 00000000..2628d763 --- /dev/null +++ b/testRoot/jni/utils/cJSON.h @@ -0,0 +1,300 @@ +/* + Copyright (c) 2009-2017 Dave Gamble and cJSON contributors + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef cJSON__h +#define cJSON__h + +#ifdef __cplusplus +extern "C" +{ +#endif + +#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) +#define __WINDOWS__ +#endif + +#ifdef __WINDOWS__ + +/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 3 define options: + +CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols +CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default) +CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol + +For *nix builds that support visibility attribute, you can define similar behavior by + +setting default visibility to hidden by adding +-fvisibility=hidden (for gcc) +or +-xldscope=hidden (for sun cc) +to CFLAGS + +then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does + +*/ + +#define CJSON_CDECL __cdecl +#define CJSON_STDCALL __stdcall + +/* export symbols by default, this is necessary for copy pasting the C and header file */ +#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_EXPORT_SYMBOLS +#endif + +#if defined(CJSON_HIDE_SYMBOLS) +#define CJSON_PUBLIC(type) type CJSON_STDCALL +#elif defined(CJSON_EXPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllexport) type CJSON_STDCALL +#elif defined(CJSON_IMPORT_SYMBOLS) +#define CJSON_PUBLIC(type) __declspec(dllimport) type CJSON_STDCALL +#endif +#else /* !__WINDOWS__ */ +#define CJSON_CDECL +#define CJSON_STDCALL + +#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY) +#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type +#else +#define CJSON_PUBLIC(type) type +#endif +#endif + +/* project version */ +#define CJSON_VERSION_MAJOR 1 +#define CJSON_VERSION_MINOR 7 +#define CJSON_VERSION_PATCH 16 + +#include + +/* cJSON Types: */ +#define cJSON_Invalid (0) +#define cJSON_False (1 << 0) +#define cJSON_True (1 << 1) +#define cJSON_NULL (1 << 2) +#define cJSON_Number (1 << 3) +#define cJSON_String (1 << 4) +#define cJSON_Array (1 << 5) +#define cJSON_Object (1 << 6) +#define cJSON_Raw (1 << 7) /* raw json */ + +#define cJSON_IsReference 256 +#define cJSON_StringIsConst 512 + +/* The cJSON structure: */ +typedef struct cJSON +{ + /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */ + struct cJSON *next; + struct cJSON *prev; + /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */ + struct cJSON *child; + + /* The type of the item, as above. */ + int type; + + /* The item's string, if type==cJSON_String and type == cJSON_Raw */ + char *valuestring; + /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */ + int valueint; + /* The item's number, if type==cJSON_Number */ + double valuedouble; + + /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ + char *string; +} cJSON; + +typedef struct cJSON_Hooks +{ + /* malloc/free are CDECL on Windows regardless of the default calling convention of the compiler, so ensure the hooks allow passing those functions directly. */ + void *(CJSON_CDECL *malloc_fn)(size_t sz); + void (CJSON_CDECL *free_fn)(void *ptr); +} cJSON_Hooks; + +typedef int cJSON_bool; + +/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them. + * This is to prevent stack overflows. */ +#ifndef CJSON_NESTING_LIMIT +#define CJSON_NESTING_LIMIT 1000 +#endif + +/* returns the version of cJSON as a string */ +CJSON_PUBLIC(const char*) cJSON_Version(void); + +/* Supply malloc, realloc and free functions to cJSON */ +CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks); + +/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */ +/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */ +CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value, size_t buffer_length); +/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */ +/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */ +CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); +CJSON_PUBLIC(cJSON *) cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated); + +/* Render a cJSON entity to text for transfer/storage. */ +CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); +/* Render a cJSON entity to text for transfer/storage without any formatting. */ +CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ +CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt); +/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */ +/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */ +CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format); +/* Delete a cJSON entity and all subentities. */ +CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); + +/* Returns the number of items in an array (or object). */ +CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); +/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */ +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); +/* Get item "string" from object. Case insensitive. */ +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); +CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string); +/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ +CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void); + +/* Check item type and return its value */ +CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item); +CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item); + +/* These functions check the type of an item */ +CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item); + +/* These calls create a cJSON item of the appropriate type. */ +CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); +CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); +CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); +/* raw json */ +CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); +CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); +CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); + +/* Create a string where valuestring references a string so + * it will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); +/* Create an object/array that only references it's elements so + * they will not be freed by cJSON_Delete */ +CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child); +CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); + +/* These utilities create an Array of count items. + * The parameter count cannot be greater than the number of elements in the number array, otherwise array access will be out of bounds.*/ +CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count); +CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); + +/* Append item to the specified array/object. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item); +/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object. + * WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before + * writing to `item->string` */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); +/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */ +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); +CJSON_PUBLIC(cJSON_bool) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); + +/* Remove/Detach items from Arrays/Objects. */ +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); + +/* Update array items. */ +CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); + +/* Duplicate a cJSON item */ +CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); +/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will + * need to be released. With recurse!=0, it will duplicate any children connected to the item. + * The item->next and ->prev pointers are always zero on return from Duplicate. */ +/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal. + * case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */ +CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive); + +/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings. + * The input pointer json cannot point to a read-only address area, such as a string constant, + * but should point to a readable and writable address area. */ +CJSON_PUBLIC(void) cJSON_Minify(char *json); + +/* Helper functions for creating and adding items to an object at the same time. + * They return the added item or NULL on failure. */ +CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean); +CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number); +CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string); +CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw); +CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name); +CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); + +/* When assigning an integer value, it needs to be propagated to valuedouble too. */ +#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) +/* helper for the cJSON_SetNumberValue macro */ +CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); +#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) +/* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */ +CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring); + +/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/ +#define cJSON_SetBoolValue(object, boolValue) ( \ + (object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \ + (object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \ + cJSON_Invalid\ +) + +/* Macro for iterating over an array or object */ +#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) + +/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */ +CJSON_PUBLIC(void *) cJSON_malloc(size_t size); +CJSON_PUBLIC(void) cJSON_free(void *object); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/testRoot/jni/utils/jsonUtils.h b/testRoot/jni/utils/jsonUtils.h new file mode 100644 index 00000000..96a9bebb --- /dev/null +++ b/testRoot/jni/utils/jsonUtils.h @@ -0,0 +1,34 @@ +#ifndef JSON_ENCODE_UTILS_H_ +#define JSON_ENCODE_UTILS_H_ +#include +#include +#include +#include "cJSON.h" + +static std::string CreateJsonBody(const std::vector>& keyValuePairs) { + cJSON *root = cJSON_CreateObject(); + if(!root) { + return {}; + } + for (const auto& pair : keyValuePairs) { + cJSON_AddItemToObject(root, std::get<0>(pair).c_str(), cJSON_CreateString(std::get<1>(pair).c_str())); + } + + char *json = cJSON_Print(root); + std::string jsonStr(json); + free(json); + cJSON_Delete(root); + return jsonStr; +} + +static std::string GetMiddleJsonString(std::string_view text) { + std::string jsonString; + int jsonStart = text.find("{"); + int jsonEnd = text.find_last_of("}"); + if(jsonStart != std::string::npos && jsonEnd != std::string::npos) { + jsonString = text.substr(jsonStart, jsonEnd - jsonStart + 1); + } + return jsonString; +} + +#endif \ No newline at end of file diff --git a/testRoot/jni/utils/stringUtils.h b/testRoot/jni/utils/stringUtils.h new file mode 100644 index 00000000..37289f8d --- /dev/null +++ b/testRoot/jni/utils/stringUtils.h @@ -0,0 +1,13 @@ +#ifndef STRING_UTILS_H_ +#define STRING_UTILS_H_ +#include +#include + +static void replaceAllOccurrences(std::string& str, const std::string& toSearch, const std::string& replaceWith) { + size_t pos = str.find(toSearch); + while(pos != std::string::npos) { + str.replace(pos, toSearch.length(), replaceWith); + pos = str.find(toSearch, pos + replaceWith.length()); + } +} +#endif \ No newline at end of file diff --git "a/testRoot/jni/\347\274\226\350\257\221\346\265\201\347\250\213.txt" "b/testRoot/jni/\347\274\226\350\257\221\346\265\201\347\250\213.txt" new file mode 100644 index 00000000..f8be961d --- /dev/null +++ "b/testRoot/jni/\347\274\226\350\257\221\346\265\201\347\250\213.txt" @@ -0,0 +1,5 @@ +1.首次编译Android.mk只可得su与lib_root_server.so文件, +2.双击运行generate_source_file_lib_root_server_data.bat +3.双击运行generate_source_file_lib_su_env_data.bat +4.双击运行generate_source_file_su_exec_data.bat +5.再次编译Android.mk可得testRoot文件 \ No newline at end of file diff --git a/testRoot/main.cpp b/testRoot/main.cpp deleted file mode 100644 index ca905fee..00000000 --- a/testRoot/main.cpp +++ /dev/null @@ -1,147 +0,0 @@ -#include -#include - -#include "super_root.h" -#include "adb_inject.h" -#define ROOT_KEY 0x7F6766F8 - -void show_capability_info() -{ - struct __user_cap_header_struct cap_header_data; - cap_user_header_t cap_header = &cap_header_data; - - struct __user_cap_data_struct cap_data_data; - cap_user_data_t cap_data = &cap_data_data; - - cap_header->pid = getpid(); - cap_header->version = _LINUX_CAPABILITY_VERSION_3; //_1、_2、_3 - - if (capget(cap_header, cap_data) < 0) { - perror("FAILED capget()"); - exit(1); - } - - printf("Cap data 0x%x, 0x%x, 0x%x\n", cap_data->effective, cap_data->permitted, cap_data->inheritable); - printf("now getuid()=%d,geteuid()=%d,getgid()=%d,getegid()=%d\n", getuid(), geteuid(), getgid(), getegid()); - - FILE * fp = popen("getenforce", "r"); - if (fp) - { - char cmd[512] = { 0 }; - fread(cmd, 1, sizeof(cmd), fp); - pclose(fp); - - printf("SELinux status: %s\n", cmd); - } -} -void test_root() -{ - show_capability_info(); - - printf("get_root ret:%d\n", get_root(ROOT_KEY)); - - show_capability_info(); - - //system("id"); - //system("/data/local/tmp/getmyinfo"); - //system("insmod /sdcard/rwProcMem37.ko ; echo $?"); - //system("cat /proc/1/maps"); - //system("ls /proc"); - //system("screencap -p /sdcard/temp.png"); - return; -} - -void test_disable_selinux() -{ - int ret = disable_selinux(ROOT_KEY); - printf("disable_selinux ret:%d\n", ret); - printf("done.\n"); - return; -} - -void test_enable_selinux() -{ - int ret = enable_selinux(ROOT_KEY); - printf("enable_selinux ret:%d\n", ret); - printf("done.\n"); - return; -} - - -void test_run_cmd(char * cmd, bool bKeepAdbRoot = false) { - printf("inject_cmd_remote_process(%s)\n", cmd); - char szResult[0x1000] = { 0 }; - ssize_t ret = safe_inject_adb_process_run_cmd_wrapper(ROOT_KEY, cmd, bKeepAdbRoot, szResult, sizeof(szResult)); - printf("inject_cmd_remote_process ret val:%zd\n", ret); - printf("inject_cmd_remote_process result:%s\n", szResult); -} - -int main(int argc, char *argv[]) -{ - printf( - "======================================================\n" - "本工具名称: Linux ARM64 完美隐藏ROOT演示\n" - "本工具功能列表:\n" - "\t1.显示自身权限信息\n" - "\t2.获取ROOT权限\n" - "\t3.绕过SELinux\n" - "\t4.还原SELinux\n" - "\t5.执行ROOT权限级别的Shell命令\n" - "\t6.赋予ADB最高级别权限\n" - "\t新一代root,跟面具完全不同思路,摆脱面具被检测的弱点,完美隐藏root功能,挑战全网root检测手段,兼容安卓APP直接JNI调用,稳定、流畅、不闪退。\n" - "======================================================\n" - ); - - - ++argv; - --argc; - - - int cmdc; - char *cmdv[6]; - - while (argc) { - // Clean up - cmdc = 0; - memset(cmdv, 0, sizeof(cmdv)); - - // Split the commands - for (char *tok = strtok(argv[0], " "); tok; tok = strtok(nullptr, " ")) - { - cmdv[cmdc++] = tok; - if (cmdc == 0) - { - continue; - } - } - - - if (strcmp(cmdv[0], "show") == 0) { - show_capability_info(); - } - else if (strcmp(cmdv[0], "root") == 0) { - test_root(); - } - else if (strcmp(cmdv[0], "disable") == 0) { - test_disable_selinux(); - } - else if (strcmp(cmdv[0], "enable") == 0) { - test_enable_selinux(); - } - else if (strcmp(cmdv[0], "cmd") == 0) { - test_run_cmd("id"); - //test_run_cmd("id > /sdcard/run.txt"); - //test_run_cmd("insmod rwProcMem37.ko > /sdcard/run.txt"); - } - else if (strcmp(cmdv[0], "adb") == 0) { - test_run_cmd("id", true); - } - else { - return 1; - } - - --argc; - ++argv; - } - return 0; -} \ No newline at end of file diff --git a/testRoot/testRoot.vcxproj b/testRoot/testRoot.vcxproj deleted file mode 100644 index 87beb8d5..00000000 --- a/testRoot/testRoot.vcxproj +++ /dev/null @@ -1,94 +0,0 @@ - - - - - Debug - ARM - - - Release - ARM - - - Debug - ARM64 - - - Release - ARM64 - - - Debug - x86 - - - Release - x86 - - - Debug - x64 - - - Release - x64 - - - - {2a287d40-5742-4389-a623-50f6bcea2a0f} - Linux - testRoot - 15.0 - Linux - 1.0 - Generic - {D51BCBC9-82E9-4017-911E-C93873C4EA2B} - - - - true - - - false - - - true - - - false - - - true - - - false - - - false - - - true - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/testRoot/testRoot.vcxproj.filters b/testRoot/testRoot.vcxproj.filters deleted file mode 100644 index 52b27543..00000000 --- a/testRoot/testRoot.vcxproj.filters +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - {4f0b16e1-e05c-44ce-8377-35b973d209f5} - - - - - jni - - - jni - - - - - - - - \ No newline at end of file diff --git a/testRoot/testRoot.vcxproj.user b/testRoot/testRoot.vcxproj.user deleted file mode 100644 index 6e2aec7a..00000000 --- a/testRoot/testRoot.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file