Simple and rude so-plus decryption implementation

Source: Internet
Author: User

Reprinted from http://bbs.pediy.com/showthread.php?t=191649

have been to the. so file when loading decryption does not understand, do not understand how it works and implementation ideas. Recently turned to look at all kinds of information, there are some ideas. See the Forum does not have similar posts, so to a post, also as a study note. Limited to the level, this dish did not find some specific implementation of Android platform ideas, these methods are to learn from other platforms to realize the idea and the YY, there will certainly be many omissions and errors, but also please Daniel, thank you!

Simple and rude so-plus decryption implementation
I. Overview
Using the dynamic link library to achieve the core of Android application, can be a certain degree of anti-reverse. Because of the presence of artifacts such as IDA, the core component needs to be encrypted. Dynamic link library encryption, in my opinion, there are two ways to implement: 1. have source code; 2, no source code. Unencrypted encryption, like the shell of the window platform and the shell of the. dex file, need to parse the file, insert the decryption code where appropriate, and fix some parameters. If you have the source code, you can construct the decryption codes and let the decryption process complete when. So is loaded. (Of course, after the application has loaded the. so file, in-memory. So data has been decrypted and can be directly dump analysis.) At the same time, there are some ways to fight the dump, and this is not going to unfold.
The following is only discussed in the form of source code, and some feasible methods of implementation are analyzed. Mainly includes analysis of ELF headers (not the meaning of each field); Encryption and decryption implementations based on specific sections and specific functions (no complex cryptographic algorithms are discussed).

Analysis of elf head for dynamic link library
There is a lot of information on the web about the elf file format, and it is well written and detailed. I do not repeat here, not very familiar with the friend, suggest first look. The following, I mainly from the link view and load view to analyze the elf head of the various fields, hoping to provide readers with some Elf file header correction ideas.
Here, I'll go back to the various fields of ELF headers:
typedef struct {
unsigned char e_ident[ei_nident]; /* File identification. */
Elf32_half E_type; /* File type. */
Elf32_half E_machine; /* Machine architecture. */
Elf32_word e_version; /* ELF format version. */
Elf32_addr E_entry; /* Entry point. */
Elf32_off E_phoff; /* Program header file offset. */
Elf32_off E_shoff; /* section header file offset. */
Elf32_word E_flags; /* architecture-specific flags. */
Elf32_half e_ehsize; /* Size of ELF header in bytes. */
Elf32_half e_phentsize; /* Size of program header entry. */
Elf32_half E_phnum; /* Number of program header entries. */
Elf32_half e_shentsize; /* Size of section header entry. */
Elf32_half E_shnum; /* Number of section header entries. */
Elf32_half E_shstrndx; /* Section name Strings section. */
} ELF32_EHDR;
E_ident, E_type, E_machine, E_version, E_flags, and e_ehsize fields are fixed; the E_entry entry address is related to the file type. E_phoff, E_phentsize, and E_phnum are related to the load view; E_shoff, E_shentsize, E_shnum, and E_shstrndx are related to linked views. Currently E_ehsize = 52 bytes, E_shentsize = 40 bytes, E_phentsize = 32 bytes.
Here's a look at the arrangement of the two views:

Directly, you can get some information: The program header is located behind the elf header, and the section header is located at the end of the elf file. That can be launched:
E_phoff = sizeof (e_ehsize);
Entire elf File size = e_shoff + e_shnum * sizeof (e_shentsize) + 1
The value of the E_shstrndx field is associated with strip. Before strip:. Shstrtab is not the last section. Then e_shstrndx = E_shnum–1–2;

After strip, the two sections of the. Symtab and. Strtab at the end of a dynamic link library are removed. Then E_shstrndx = e_shnum–1.

The. So files generated under \libs\ armeabi\ using the NDK are strip and packaged into the APK. A. So file that has not been strip can be found under \obj\local\armeabi\. Here, we can fix the. So file mentioned in the http://bbs.pediy.com/showthread.php?t=188793 post. If E_shoff and E_shnum are all changed to any value, it is more troublesome to fix them.
It feels like E_shoff, E_shnum, and so on section related information arbitrary modification, the use of. So files has no effect. Indeed, there are at least two areas to substantiate:
1. So files in-memory mapping

It is certain that the friend who understands the elf load (execution) View is clearly aware that. so files are mapped to memory in segment units. The section of the red area in the figure is memory that is not mapped and, of course, is not found in the segment.
2. Android Linker source code
There is an important structure soinfo in the Linker.h source code, some fields are listed below:
struct soinfo{
const Char Name[soinfo_name_len]; So full name
ELF32_PHDR *phdr; Address of program Header
int phnum; Number of segment
unsigned *dynamic; Point to. Dynamic, same as in section and segment
The following 4 members are related to a. hash table
unsigned nbucket;
unsigned nchain;
unsigned *bucket;
unsigned *chain;

unsigned *preinit_array;
unsigned preinit_array_count;
These two members can only appear in an executable file

Point to the initialization code, which precedes the line of the main function, which is called by linker at load time, and can be seen in linker.c: __linker_init, Link_image, call_constructors, call _array
unsigned *init_array;
unsigned init_array_count;
void (*init_func) (void);
Similar to Init_array, except that it executes after main ends
unsigned *fini_array;
unsigned fini_array_count;
void (*fini_func) (void);
}
In addition, there are many places in linker.c that can be corroborated. The essence or linker is the so file based on the load view resolution.
Based on the above conclusions, we will analyze the fields of elf heads.
1) The E_ident[ei_nident] field contains magic number, byte order, Word length, and version, followed by padding 0. For Android linker, check the magic number by Verify_elf_object function to determine if the. so file. Well, we can write data to the location, at least to the next 0 fill position. Unfortunately, I tested it under Fedora 14 and was not able to write data to 0 populated locations, and the linker reported non-0 padding errors.
2) for Android linker, E_type, E_machine, E_version and E_flags fields do not care, can be modified to other data (analysis only, no measurement)
3) for the dynamic link library, E_entry entry address is meaningless, because when the program is loaded, the set jump address is the address of the dynamic connector, this field can be filled as data.
4) So when loading, there is no relationship to the linked view, that is, E_shoff, E_shentsize, E_shnum, and e_shstrndx these fields can be arbitrarily modified. After being modified, using tools such as Readelf and Ida to open, will report a variety of errors, I believe the reader has seen.
5) Since the so load is closely related to the load view, natural E_phoff, E_phentsize, and E_phnum fields are not movable.

According to the above conclusions, do a unrecognizable, various tools to open the error so file is very easy, the reader can try, here is not an example, you will see in the following content.

Third, the implementation of encryption and decryption based on specific section
It is mentioned here that the addition and decryption based on section means that the specific section of the so file is encrypted, so the file is decrypted when it is loaded. An example is given below.
Suppose you have an Shelldemo application that calls a native method to return a string for the UI to display. In the native method, the GetString method is called to return a string for the native method to return. I need to encrypt the GetString method. Here, the GetString method is stored in the. MyText (Specify __ATTRIBUTE__ ((section (". MyText"));), that is, the. MyText needs to be encrypted.
Encryption process:
1) Read section offset Shoff, Shnum, and shstrtab from so file headers
2) Read the string in the Shstrtab and store it in the STR space
3) Read the section header from the Shoff position and store it in the SHDR
4) indexed in STR string by Shdr-Sh_name, string comparison with. MyText, if not matched, continue reading
5) Read and save the. MyText content through the Sh_size field Shdr, Sh_offset and SHDR.
6) For ease of understanding, do not use complex cryptographic algorithms. Here, only the content of all the contents of the reverse, that is *content = ~ (*content);
7) write content back to so file
8) In order to verify that the section field can be arbitrarily modified in the second verse, here, Shdr-a addr is written to the ELF header E_shoff, sh_size and addr are written in E_entry, ehdr.e_en try = (length << +) + nsize. Of course, this also simplifies the decryption process, there is a benefit is: if the so file header correction back, the program can not be run.

When decrypting, it is necessary to ensure that the decryption function is called when the so is loaded, and that function is declared as: Init_getstring __attribute__ ((constructor)). (can also be implemented using the C + + constructor, which is also implemented by attribute)
Decryption process:
1) dynamic linker calls init_getstring via Call_array
2) init_getstring First call the Getlibaddr method to get the start address of so file in memory
3) Read the first 52 bytes, that is, the elf head. obtained by E_shoff. MyText Memory load address, ehdr.e_entry get. MyText Size and memory block
4) Modify the Read and write permissions of the memory block where the. MyText resides
5) The [E_shoff, E_shoff + size] Memory area data is decrypted, that is, take the inverse operation: *content = ~ (*content);
6) Modify read and Write permissions back to memory area
(Here is the decryption of the data in the code snippet, which requires write permission.) If the data segment data is decrypted, you do not need to change the permissions to directly manipulate it)

Use readelf to view encrypted so files:

The operation result is very simple, the source code see annex

Note: Not all sections can be fully added, and some data cannot be encrypted. For example, directly to the. Text encryption, will be related to the CRT code is also encrypted, only selective encryption. The following describes how to implement

Iv. implementation of encryption and decryption based on specific functions
The above encryption and decryption method is simple and rude. Implemented in this way, if the ELF header section is restored, it is easy to find so many more sections. So, the data part of the existing section in Elf is encrypted, can achieve a certain hidden effect.
Similar to the example in the previous section, named Shelldemo2, just native returns the string directly to the UI. What needs to be done is to encrypt the java_com_example_shelldemo2_mainactivity_getstring function. Both encryption and decryption are implemented based on the Mount View. It is important to note that if the cryptographic function is declared with static, then the function will not appear in the. Dynsym and cannot be decrypted in the load view through the function name. Of course, you can also use the Trickery method, similar to the previous section, the address and length information written to the so header implementation. Java_com_example_shelldemo2_mainactivity_getstring need to be called, then it must be found in. Dynsym.
Encryption process:
1) Read the file header to get E_phoff, e_phentsize, and e_phnum information
2) find the dynamic by using the P_type field in ELF32_PHDR. As can be seen, in fact, dynamic is the. Dynamic section. Get the starting position and length of the file from the P_offset and P_filesz fields

3) traverse. Dynamic to find the offset in the. Dynsym,. dynstr,. Hash section file, and the size of the. Dynstr. In my test environment, the ELF.H definition in Fedora 14 and Windows7 Cygwin x64. The D_tag of the HASH is: Dt_gnu_hash, while the Android source code is: Dt_hash.
4) Calculate the hash value according to the function name
5) According to the hash value, find the subscript hash% nbuckets bucket; reads the elf32_sym symbol of the corresponding index in the. Dynsym from the value in the bucket; st_ from the symbol Name so find the corresponding string in. dynstr compared to the function name. If not, find the next elf32_sym symbol according to Chain[hash% nbuckets] until it is found or chain terminated. The narrative here is somewhat complex, directly on the code.
for (i = bucket[funhash% nbucket]; I! = 0; i = chain[i]) {
if (strcmp (Dynstr + (funsym + i)->st_name, funcName) = = 0) {
Flag = 0;
Break
}
}
6) Once you find the elf32_sym symbol for the function, you can find the position and size of the function based on the St_value and St_size fields.
7) The next steps are the same as in the previous section, and we will not repeat the following

The decryption process is a cryptographic inverse process, roughly the same, with only a few subtle differences, as follows:
1) Find the start address of the so file in memory
2) also through so file head to find PhDr, from PhDr find pt_dynamic, need to take p_vaddr and P_filesz field, not p_offset, here need attention.
3) Follow-up operation on the encryption similar, do not repeat. The decryption of memory area data also requires attention to read and write permission issues.

Post-encryption effects:

The result is the same as the previous section and is not posted.

V. References
http://blog.csdn.net/forlong401/article/details/12060605
"Elf file Format"
ANDROID linker Source: Bionic\linker
Android libc Source: bionic\libc\bionic
Google: Elf link view and load view related information
------------------------------------------------------------------------
Based on the above method, I wrote a crackme.apk keygen program for everyone to play. Enter the checksum for the username and regcodes,8 bits of the 3~10 bit, the character range: A~z, A~z, 0~9. If the checksum is passed, the prompt: congratulation! You crack it!.
Attachment:
Shelldemo.zip.
Shelldemo2.zip.
crackme.apk.

Last PDF, this is too messy.
Simple and rude so plus decryption implementation. pdf* reprint please specify from the Snow forum @pediy.com

Simple and rude so-plus decryption implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.