Skip to content

Commit

Permalink
add replace header restrict feature
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneyang authored and eugeneyang committed Aug 31, 2016
1 parent 2a6d78c commit 62b56fe
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ build/
!default.perspectivev3
xcuserdata
*.xccheckout
*.xcscmblueprint
*.moved-aside
DerivedData
*.hmap
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Usage: restore-symbol -o <output-file> [-j <json-symbol-file>] <mach-o-file>
-o <output-file> New mach-o-file path
--disable-oc-detect Disable auto detect and add oc method into symbol table,
only add symbol in json file
--replace-restrict New mach-o-file will replace the LC_SEGMENT(__RESTRICT,__restrict)
with LC_SEGMENT(__restrict,__restrict) to close dylib inject protection
-j <json-symbol-file> Json file containing extra symbol info, the key is "name","address"
like this:
Expand Down
13 changes: 11 additions & 2 deletions source/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#define RS_OPT_DISABLE_OC_DETECT 1
#define RS_OPT_VERSION 2
#define RS_OPT_REPLACE_RESTRICT 3



Expand All @@ -40,6 +41,8 @@ void print_usage(void)
" -o <output-file> New mach-o-file path\n"
" --disable-oc-detect Disable auto detect and add oc method into symbol table,\n"
" only add symbol in json file\n"
" --replace-restrict New mach-o-file will replace the LC_SEGMENT(__RESTRICT,__restrict)\n"
" with LC_SEGMENT(__restrict,__restrict) to close dylib inject protection\n"
" -j <json-symbol-file> Json file containing extra symbol info, the key is \"name\",\"address\"\n like this:\n \n"
" [\n {\n \"name\": \"main\", \n \"address\": \"0xXXXXXX\"\n }, \n {\n \"name\": \"-[XXXX XXXXX]\", \n \"address\": \"0xXXXXXX\"\n },\n .... \n ]\n"

Expand All @@ -50,14 +53,15 @@ void print_usage(void)



void restore_symbol(NSString * inpath, NSString * output, NSString *jsonPath, bool oc_detect_enable);
void restore_symbol(NSString * inpath, NSString *outpath, NSString *jsonPath, bool oc_detect_enable, bool replace_restrict);

int main(int argc, char * argv[]) {




bool oc_detect_enable = true;
bool replace_restrict = false;
NSString *inpath = nil;
NSString * outpath = nil;
NSString *jsonPath = nil;
Expand All @@ -71,6 +75,8 @@ int main(int argc, char * argv[]) {
{ "output", required_argument, NULL, 'o' },
{ "json", required_argument, NULL, 'j' },
{ "version", no_argument, NULL, RS_OPT_VERSION },
{ "replace-restrict", no_argument, NULL, RS_OPT_REPLACE_RESTRICT },

{ NULL, 0, NULL, 0 },

};
Expand Down Expand Up @@ -100,6 +106,9 @@ int main(int argc, char * argv[]) {
oc_detect_enable = false;
break;

case RS_OPT_REPLACE_RESTRICT:
replace_restrict = true;
break;
default:
break;
}
Expand All @@ -115,6 +124,6 @@ int main(int argc, char * argv[]) {
}


restore_symbol(inpath, outpath, jsonPath, oc_detect_enable);
restore_symbol(inpath, outpath, jsonPath, oc_detect_enable, replace_restrict);

}
22 changes: 17 additions & 5 deletions source/restore-symbol.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@



void restore_symbol(NSString * inpath, NSString *outpath, NSString *jsonPath, bool oc_detect_enable){
void restore_symbol(NSString * inpath, NSString *outpath, NSString *jsonPath, bool oc_detect_enable, bool replace_restrict){



Expand All @@ -49,7 +49,7 @@ void restore_symbol(NSString * inpath, NSString *outpath, NSString *jsonPath, bo
}



if ([outpath length] == 0) {
fprintf(stderr, "Error: No output file path!\n");
exit(1);
Expand All @@ -76,13 +76,13 @@ void restore_symbol(NSString * inpath, NSString *outpath, NSString *jsonPath, bo
CDMachOFile * machOFile = (CDMachOFile *)ofile;
const bool Is32Bit = ! machOFile.uses64BitABI;


RSSymbolCollector *collector = [RSSymbolCollector new];
collector.machOFile = machOFile;

if (oc_detect_enable) {
fprintf(stderr, "Scan OC method in mach-o-file.\n");

CDClassDump *classDump = [[CDClassDump alloc] init];
CDArch targetArch;
if ([machOFile bestMatchForLocalArch:&targetArch] == NO) {
Expand All @@ -106,7 +106,7 @@ void restore_symbol(NSString * inpath, NSString *outpath, NSString *jsonPath, bo
[classDump recursivelyVisit:visitor];

}

fprintf(stderr, "Scan OC method finish.\n");
}

Expand Down Expand Up @@ -146,6 +146,18 @@ void restore_symbol(NSString * inpath, NSString *outpath, NSString *jsonPath, bo
uint32 origin_symbol_table_offset = machOFile.symbolTable.symoff;
uint32 origin_symbol_table_num = machOFile.symbolTable.nsyms;


if (replace_restrict){
CDLCSegment * restrict_seg = [machOFile segmentWithName:@"__RESTRICT"];

struct segment_command *restrict_seg_cmd = (struct segment_command *)((char *)outData.mutableBytes + restrict_seg.commandOffset);
struct section *restrict_section = (struct section *)((char *)outData.mutableBytes + restrict_seg.commandOffset + (Is32Bit? sizeof(struct segment_command) : sizeof(struct segment_command_64)));


strncpy(restrict_seg_cmd -> segname, "__restrict", 16);
strncpy(restrict_section -> segname, "__restrict", 16);
}

//LC_CODE_SIGNATURE need align 16 byte, so add padding at end of string table.
uint32 string_table_padding = 0;
{
Expand Down

0 comments on commit 62b56fe

Please sign in to comment.