Skip to content

Commit 5d0d290

Browse files
authored
refactor: refactor CLI (alibaba#23)
* refactor: refactor CLI and release v105 --------- Signed-off-by: bppps <bpppsaka@gmail.com>
1 parent fad7c78 commit 5d0d290

24 files changed

Lines changed: 909 additions & 360 deletions

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ flight_profiler_agent.${SHARED_LIB_SUFFIX}:
3030
@mv build/libfrida-gum.* build/lib/
3131
@echo "compiling flight_profiler_agent.${SHARED_LIB_SUFFIX}"
3232
@${CC} ${CFLAGS} ${LDFLAGS} -I${PY_HEADER_PATH} -Ibuild/include -Icsrc \
33-
csrc/code_inject.cpp csrc/frida_profiler.cpp \
33+
csrc/profiler_attach.cpp csrc/frida_profiler.cpp \
3434
csrc/time_util.cpp csrc/symbol_util.cpp csrc/python_util.cpp \
3535
csrc/py_gil_intercept.cpp csrc/py_gil_stat.cpp csrc/stack/py_stack.cpp \
3636
-o build/lib/flight_profiler_agent.${SHARED_LIB_SUFFIX} -Lbuild/lib -lfrida-gum -ldl
3737
@if [ "$(IS_DARWIN)" != "Darwin" ]; then \
38-
$(CC) $(INJECT_CFLAGS) -Icsrc/inject/ -o build/lib/inject csrc/inject/ProcessTracer.cpp csrc/inject/ProcessUtils.cpp csrc/inject/LibraryInjector.cpp csrc/inject/inject.cpp -ldl;\
39-
cp build/lib/inject flight_profiler/lib/inject;\
38+
$(CC) $(INJECT_CFLAGS) -Icsrc/attach/ -o build/lib/attach csrc/attach/ProcessTracer.cpp csrc/attach/ProcessUtils.cpp csrc/attach/AttachAgent.cpp csrc/attach/attach.cpp -ldl;\
39+
cp build/lib/attach flight_profiler/lib/attach;\
4040
fi
4141
@cp build/lib/flight_profiler_agent.${SHARED_LIB_SUFFIX} flight_profiler/lib/flight_profiler_agent.${SHARED_LIB_SUFFIX}
4242

Lines changed: 116 additions & 126 deletions
Large diffs are not rendered by default.

csrc/attach/AttachAgent.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef ATTACH_AGENT_H
2+
#define ATTACH_AGENT_H
3+
4+
#include "ExitCode.h"
5+
#include "ProcessTracer.h"
6+
#include "ProcessUtils.h"
7+
#include <memory>
8+
#include <string>
9+
#include <sys/user.h>
10+
#include <vector>
11+
12+
/**
13+
* @brief Manages the attaching of profiler agent into target processes
14+
*
15+
* This class implements a comprehensive solution for attaching profiler agent
16+
* into target processes using advanced ptrace-based techniques. It handles
17+
* all aspects of the attach process including preparation, execution, and
18+
* verification.
19+
*/
20+
class AttachAgent {
21+
public:
22+
/**
23+
* @brief Constructs an AttachAgent instance
24+
* @param target_process_id PID of the process to attach the agent into
25+
* @param shared_library_file_path File path of the shared library to load
26+
* @param debug_mode Enable debug logging
27+
*/
28+
AttachAgent(pid_t target_process_id,
29+
const std::string &shared_library_file_path,
30+
bool debug_mode = false);
31+
32+
/**
33+
* @brief Cleans up resources used by the AttachAgent
34+
*/
35+
~AttachAgent();
36+
37+
/**
38+
* @brief Performs the complete agent attach process
39+
* @return ExitCode indicating success or failure
40+
*/
41+
ExitCode performAttach();
42+
43+
private:
44+
// Core instance attributes
45+
pid_t target_process_id_;
46+
std::string library_file_path_;
47+
ProcessTracer process_tracer_;
48+
49+
// Attach workflow methods
50+
ExitCode initializeAttachEnvironment(long &code_attach_address,
51+
REG_TYPE *original_registers,
52+
REG_TYPE *working_registers);
53+
ExitCode orchestrateAttachSequence(long attach_address,
54+
long malloc_function_address,
55+
long free_function_address,
56+
long dlopen_function_address,
57+
int library_path_string_length,
58+
REG_TYPE *initial_registers);
59+
ExitCode confirmAttachSuccess(long attach_memory_location,
60+
const std::vector<char> &backup_memory_data,
61+
size_t shellcode_byte_size,
62+
REG_TYPE *original_register_state);
63+
64+
// Shellcode generation methods
65+
std::vector<char> createShellcodePayload(size_t &payload_size,
66+
intptr_t &return_instruction_offset);
67+
68+
// Path manipulation utilities
69+
void getParentDirectoryPath(std::string &file_path);
70+
};
71+
72+
#endif // ATTACH_AGENT_H
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,28 +272,27 @@ bool ProcessTracer::verifySignalStatus() {
272272
}
273273

274274
/**
275-
* @brief Restore the process state after a failed injection attempt
275+
* @brief Restore the process state after a failed attach attempt
276276
*
277277
* This function performs the three required steps to restore the process state:
278-
* 1. Write the original memory data back to the injection address
278+
* 1. Write the original memory data back to the attach address
279279
* 2. Restore the original register state
280280
* 3. Detach from the process
281281
*
282-
* @param injection_address Address where the shellcode was injected
283-
* @param backup_data Pointer to the original data at the injection address
282+
* @param attach_address Address where the shellcode was loaded
283+
* @param backup_data Pointer to the original data at the attach address
284284
* @param data_length Length of the backup data
285285
* @param registers Pointer to the original register state
286286
* @return true if restoration was successful, false otherwise
287287
*/
288-
bool ProcessTracer::recoverInjection(long injection_address,
289-
const void *backup_data,
290-
size_t data_length, REG_TYPE *registers) {
291-
// Step 1: Write the original memory data back to the injection address
292-
if (!writeMemory(injection_address, backup_data, data_length)) {
288+
bool ProcessTracer::recoverAttach(long attach_address, const void *backup_data,
289+
size_t data_length, REG_TYPE *registers) {
290+
// Step 1: Write the original memory data back to the attach address
291+
if (!writeMemory(attach_address, backup_data, data_length)) {
293292
if (debug_mode_) {
294293
std::cerr << "[ERROR] PyFlightProfiler: Failed to recover original "
295294
"memory data at address 0x"
296-
<< std::hex << injection_address << std::dec << std::endl;
295+
<< std::hex << attach_address << std::dec << std::endl;
297296
}
298297
return false;
299298
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ class ProcessTracer {
112112

113113
// Failure recovery
114114
/**
115-
* @brief Restore the process state after a failed injection attempt
116-
* @param injection_address Address where the shellcode was injected
117-
* @param backup_data Pointer to the original data at the injection address
115+
* @brief Restore the process state after a failed attach attempt
116+
* @param attach_address Address where the shellcode was loaded
117+
* @param backup_data Pointer to the original data at the attach address
118118
* @param data_length Length of the backup data
119119
* @param registers Pointer to the original register state
120120
* @return true if restoration was successful, false otherwise
121121
*/
122-
bool recoverInjection(long injection_address, const void *backup_data,
123-
size_t data_length, REG_TYPE *registers);
122+
bool recoverAttach(long attach_address, const void *backup_data,
123+
size_t data_length, REG_TYPE *registers);
124124

125125
// Accessor for debug mode
126126
/**
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "LibraryInjector.h"
1+
#include "AttachAgent.h"
22
#include "ProcessUtils.h"
33
#include <cstdio>
44
#include <cstdlib>
@@ -26,22 +26,22 @@ void extractParentDirectoryFromPath(std::string &file_system_path) {
2626
}
2727

2828
/**
29-
* @brief Main entry point for the library injection utility
29+
* @brief Main entry point for the profiler attach utility
3030
*
31-
* This program injects the flight_profiler_agent.so library into a target
32-
* process using advanced ptrace-based injection techniques.
31+
* This program attaches the flight_profiler_agent.so library into a target
32+
* process using advanced ptrace-based techniques.
3333
*
34-
* Usage: ./inject <process_identifier>
34+
* Usage: ./attach <process_identifier>
3535
*
3636
* @param argument_count Number of command line arguments
3737
* @param argument_values Array of command line arguments
38-
* @return 0 on successful injection, 1 on failure
38+
* @return 0 on successful attach, 1 on failure
3939
*/
4040
int main(int argument_count, char **argument_values) {
4141
// Validate command line arguments
4242
if (argument_count < 2) {
43-
std::cout << "Invalid inject command without target process identifier "
44-
"provided, USAGE: ./inject process_id!"
43+
std::cout << "Invalid attach command without target process identifier "
44+
"provided, USAGE: ./attach process_id!"
4545
<< std::endl;
4646
return 1;
4747
}
@@ -86,9 +86,8 @@ int main(int argument_count, char **argument_values) {
8686
std::string library_file_path(library_file_path_cstring);
8787
free(library_file_path_cstring);
8888

89-
// Create and execute the injector
90-
LibraryInjector library_injector(target_process_id, library_file_path,
91-
debug_mode);
89+
// Create and execute the attach agent
90+
AttachAgent attach_agent(target_process_id, library_file_path, debug_mode);
9291

93-
return static_cast<int>(library_injector.performInjection());
92+
return static_cast<int>(attach_agent.performAttach());
9493
}

csrc/code_inject.h

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)