当前位置:网站首页>Hook principle
Hook principle
2022-06-24 02:32:00 【conanma】
HOOK summary
HOOK Chinese translated into “ hook ” or “ hook ”. stay iOS Reverse means Change the program running process A technique . adopt HOOK Technology allows other people's programs to execute their own code . This technique is often used in reverse engineering . So in the process of learning , We should focus on understanding its principle , This can effectively protect malicious code .
iOS in HOOK Several ways of Technology
Method Swizzle: It is mainly used forOC Method, utilize OC OfRuntime characteristic, The dynamic changeSEL( Method number ) and IMP( Method realization )Correspondence of , achieve OC The purpose of the method call process change , This technology is prior to 11 - Code injection Used .fishhook:Facebook One of theDynamically modify links MachO fileTools for . utilize MachO File loading principle , By modifying theLazy loading and non lazy loadingPointers to two tables , Reach the right C Function HOOK Purpose , Official addressCydia Substrate: It was originally called Mobile Substrate, Its main function is toOC Method 、C Function and function addressConduct HOOK operation . Of course, it's not just about iOS And designed , Android can also be used , Official address
Method Swizzle
utilize OC Of Runtime characteristic , The dynamic change SEL( Method number ) and IMP( Method realization ) Correspondence of , achieve OC The purpose of the method call process change . It is mainly used for OC Method .
stay OC in ,SEL and IMP The relationship between , It is like the relationship between the title and page number in the table of contents of a book .
SEL It's the method number , It can be compared to the title in the table of contents . IMP Is the address of the method implementation , It can be compared to the page number in the table of contents .
Method Swizzle Technology is through runtime Provided function modification SEL and IMP Correspondence of
Cydia Substrate
Cydia Substrate Mainly by 3 Part of it is made up of :
- MobileHooker
- MobileLoader
- safe mode
MobileHooker
It defines a series of Macros and functions , Bottom call objc Of runtime and fishhook To replace the function of the system or target application .
It has two important functions
MSHookMessageEx: Mainly forObjective-CMethod
void MSHookMessageEx(Class class, SEL selector, IMP replacement, IMP result)
MSHookFunction: Mainly forC and C++ function,Logos The grammatical %hook This is to encapsulate the following functions
void MSHookFunction(void function, void* replacement, void** p_original)
MobileLoader
MobileLoader Used to load third party dylib Into the running application . Startup time MobileLoader The third-party dynamic library of the specified directory will be loaded according to the rules , The third-party dynamic library is the cracking program we wrote .
safe mode
The essence of cracking programs is dylib, Parasitic in the process of others . Once the system process goes wrong , Could cause the whole process to crash , It will cause iOS paralysis . therefore Cydia Substrate Introduced safe mode , In safe mode, all are based on Cydia Substratede Three parties of dylib Will be banned , Easy to check and repair .
fishhook
The key function
// The function used to rebind the symbol table , Use it in exchange for FISHHOOK_VISIBILITY int rebind_symbols(struct rebinding rebindings[], size_t rebindings_nel);
Parameter description :
rebindings: DepositrebindingAn array of structs , Multiple functions can be exchanged at the same time .rebindingThe structure is as follows :
struct rebinding {
const char *name; // stay HOOK Name of function
void *replacement; // New function address
void **replaced; // Original function address
};rebindings_nel:rebindingsLength of array .
HOOK The flow of system functions
Principle of external function call
When App start-up when ,dyld Read The main program MachO file , Will load Shared cache System libraries in , Of the system functions used in the program Real address Replace MachO Medium Placeholder address .
about MachO Code snippet in (__TEXT) Come on , It is read-only Of . At run time , You cannot directly modify the real address of an external function . In order to solve the above situation , Apple uses PIC technology ( Location independent code ), When calling an external function , stay Compile phase , Will be in MachO Readable and writable data segment of , Defining symbols , Occupy 8 byte , Address used to store external functions . But what is temporarily stored in the compilation phase is Placeholder address . stay Runtime ,dyld Bind symbols Real function address . For code snippets , There is no change .
therefore , External call function , Not direct address access , Instead, you find the address through symbols . This one OC in SEL And IMP The corresponding relationship of is very similar . Such mechanism , Allows developers to dynamically HOOK External call function , stay OC Dynamic change in the market SEL And IMP Correspondence of , For external calling functions , What changes dynamically is the correspondence between symbols and addresses , The above operations are called : Symbol table rebinding .
NSLog Calling process
1、 When app starts , determine ASLR, adopt lldb Medium image list You can view the current ASLR
among 0x0000000100d54000 Contains the program __PAGEZERO(0x100000000), therefore ,ASLR by :0x100d54000 - 0x100000000 = 0xd54000
2、 stay NSLog Stop before passing the breakpoint , Through assembly, you can see , The address at the current breakpoint is :0x100d5a2a8 NSLog The sign is in Mach-O The offset in is :0x100d5a2a8 - 0x100d54000 = 0x62A8
3、 View and execute 0x100d5a2a8 What's in the address
- Print content directly by address
- Mach-O View content
- Use dis -s Print
0x62A8 The content of is located in __TEXT paragraph Of __stubs in ,__stubs be called Symbolic pile , Its essence is a piece of code , Used to jump to the lazy loaded symbol table , Find the value of the corresponding symbol . here 0x62A8 Namely NSLog Pile of , The code content is as follows :
(lldb) dis -s 0x100d5a2a8
testDyld`NSLog:
0x100d5a2a8 <+0>: nop
0x100d5a2ac <+4>: ldr x16, #0x5d54 ; (void *)0x0000000100d5a3d4
0x100d5a2b0 <+8>: br x16How to jump to the lazy loading symbol table through the pile ? Let's start with the address . 【1】 The address where the current program is running is :0x100d5a2ac 【2】 take 0x100d5a2ac+0x5d54 Offset , obtain 0x0000000100d60000 【3】 For the sake of clarity 0x0000000100d60000 This value corresponds to Mach-O Which offset value , Need to put 0x0000000100d60000 - ASLR - Memory virtual address , namely 0x0000000100d60000 - 0x0000000000d54000 - 0x0000000100000000 = 0xC000 【4】 see Mach-O in 0xC000 The contents of the offset , That is, lazy loading in the symbol table NSLog Symbol pointer
【5】 Take out the lazy load symbol table ,NSLog The pointer corresponding to the symbol :0x00000001000063D4, In memory ,0x00000001000063D4 + ASLR = 0x00000001000063D4 + 0x0000000000d54000 = 0x0000000100d5a3d4
【6】 Jump to 0x0000000100d5a3d4 Execute at address .0x0000000100d5a3d4 - ASLR - Virtual address = 0x0000000100d5a3d4 - 0x0000000000d54000 - 0x0000000100000000 = 0x63D4, stay MachO in , find offset 0x63D4, Point to __TEXT,__stubs_helper The code in , see Mach-O in 0x63D4 The contents of the offset
【7】 from Mach-O Can be seen in , Use b Instructions Jump to offset 0x63BC It's about . Reuse br Instructions Jump to offset 0x8008 It's about .
【8】 see Mach-O in 0x8008 The contents of the offset .
This is pointing to dyld_stub_binder function , This function is used for Symbol binding Of .
【9】dyld_stub_binder It is also an external function , How to find its address ? from Mach-O in , You can see dyld_stub_binder The offset address of the function is 0x8008, But its value is all 0, Description in Mach-O No value in , and dyld_stub_binder The real address of the function is dyld When loading the main program , Will bind Non lazy load symbols and weak reference symbols , therefore dyld_stub_binder The value of a function , When the program starts, it is dyld Direct binding .
【10】 thus , When an external symbol is called for the first time , Already completed NSLog The symbol binding of , From the shared cache NSLog Take out the actual address of , And fill in 0x100d5a2a8 Compilation Division . After filling, see the following figure :
【11】 When called the second time NSLog when , here , The symbol table stores NSLog The real address of , So you can jump directly to NSLog At the function of .
summary
HOOKDefinition : A technique for changing the flow of program execution .- HOOK Three common ways
Method Swizzle, utilize OC Runtime features , modifySEL and IMPCorrespondence of .fishHook,HOOK Common ways of external symbols , Modification changeSymbols and addressesCorrespondence of .Cydia Substrate, The main function is to targetOC Method 、C Function and function addressConduct HOOK operation
- External symbols call the process , With NSLog For example
- 【 First step 】 adopt
Code segment __TEXTin__stubs( pile )inaddrDetermine the location of the pile . - 【 The second step 】 take
Memory address of the pile+ASLRobtainContents of piles, Pile content is essentially a piece of code , Used to jump to the lazy load symbol table , Find the function implementation address of the corresponding symbol . - 【 The third step 】 When
For the first timeWhen calling an external symbol , The function address corresponding to the symbol is a placeholder address , Point to__stubs_helperThe code in , By callingdyld_stub_binderfunction , performSymbol binding. - 【 Step four 】 When
Againload NSLog when , Since the symbol binding has been completed , therefore , The function address corresponding to the symbol is no longer a placeholder address , It isNSLog The real address of the function.
- 【 First step 】 adopt
边栏推荐
- The blue screen will be displayed at a remote location, and the error code kmode will be reported_ EXCEPTION_ NOT_ Handled, the DMP file has the keyword cdd dll
- Official spoilers! Figure 1 understand Tencent security @2021 Tencent digital ecology Conference
- Is it illegal to use someone else's trademark to register a domain name? What should we pay attention to when registering a domain name?
- Do you still understand the deadlock handling methods in MySQL performance testing and tuning?
- The dealer management and control platform in the leather industry simplifies the purchase approval process and easily controls agents
- The easydss on demand file upload interface calls postman to report an error. Failed to upload the file?
- Tencent cloud won the first place in the cloud natural language understanding classification task
- NFT metauniverse and the relationship between Games Golden Finance
- Using the database middleware MYCAT to realize read-write separation (dual master and dual slave)
- [supply chain • case] Tianneng group: the key to understanding the leading battery manufacturer to achieve the first profit fault
猜你喜欢

163 mailbox login portal display, enterprise mailbox computer version login portal

How to fill in and register e-mail, and open mass mailing software for free

Leetcode969: pancake sorting (medium, dynamic programming)

Introduction to development model + test model

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations

2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set

Advanced BOM tool intelligent packaging function
Cloudpods golang practice
随机推荐
What information should be provided for enterprise trademark registration? Is it difficult to register a trademark?
How do I fix the iPhone green screen problem? Try these solutions
How to access easynvr management platform through web pages without data?
How to build an enterprise website? Is it difficult?
What is raid? 2000 words can explain RAID 0, 1, 5 and 10 thoroughly, and collect!
Official spoilers! Figure 1 understand Tencent security @2021 Tencent digital ecology Conference
Tke single node risk avoidance
How to register a trademark? How to improve the passing rate of trademark registration?
Advanced BOM tool intelligent packaging function
Simple use of notification
Comparison of four dynamic routes: Rip, OSPF, Isis, BGP
Evaluation index of machine learning model
Layout use case
Tencent peace of mind ecological alliance was officially established as a linkage partner. Open technology helps "digital agriculture"
IPhone sending SMS implementation
How to enable IPv6 network access for personal broadband
Afnetworking server client
What is the domain name trademark? What are the registration conditions for domain names and trademarks?
What is the large bandwidth of IDC machine room?
Go language starts again, go modules' past life, present life and basic use