Skip to content

Commit 6693c7b

Browse files
author
hackcatml
committed
Replace the cp command with fread and fwrite, and add a signal handler for the zygisk-gadget tool.
1 parent ee091fd commit 6693c7b

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

module.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ ext {
55
toolName = "zygisk-gadget"
66
moduleAuthor = "hackcatml"
77
moduleDescription = "A Zygisk Module That Injects Frida-Gadget"
8-
moduleVersion = "v1.2.0"
8+
moduleVersion = "v1.2.1"
99
moduleVersionCode = 1
1010
}

module/src/main/cpp/main.cpp

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "xdl.h"
1313
#include "nlohmann/json.hpp"
1414

15+
#define BUFFER_SIZE 1024
16+
1517
using zygisk::Api;
1618
using zygisk::AppSpecializeArgs;
1719
using zygisk::ServerSpecializeArgs;
@@ -120,6 +122,7 @@ class MyModule : public zygisk::ModuleBase {
120122
if (strcmp(package_name, target_package_name.c_str()) == 0) {
121123
LOGD("Enable gadget injection %s", package_name);
122124
_enable_gadget_injection = true;
125+
write(fd, &_enable_gadget_injection, sizeof(_enable_gadget_injection));
123126

124127
_target_package_name = strdup(target_package_name.c_str());
125128

@@ -148,7 +151,7 @@ class MyModule : public zygisk::ModuleBase {
148151
private:
149152
Api* _api{};
150153
JNIEnv* _env{};
151-
bool _enable_gadget_injection{};
154+
bool _enable_gadget_injection = false;
152155
char* _target_package_name{};
153156
uint _delay{};
154157
char* _frida_gadget_name{};
@@ -168,31 +171,39 @@ json get_json(const std::string& path) {
168171
}
169172
}
170173

171-
static void executeCommand(const char* gadget_path, const char* package_name, const char* format) {
172-
char* command;
173-
int res = asprintf(&command, format, gadget_path, package_name);
174-
if (res == -1) {
175-
LOGD("Failed to build command string");
176-
return;
174+
static void copy_file(const char *source_path, const char *dest_path) {
175+
FILE *source_file, *dest_file;
176+
char buffer[BUFFER_SIZE];
177+
size_t bytes_read;
178+
179+
source_file = fopen(source_path, "rb");
180+
if (source_file == nullptr) {
181+
LOGD("Error opening source file");
182+
exit(EXIT_FAILURE);
177183
}
178-
LOGD("Command: %s", command);
179184

180-
std::array<char, 128> buffer{};
181-
std::string result;
182-
FILE* pipe = popen(command, "r");
183-
if (!pipe) {
184-
LOGD("Failed to run command");
185-
free(command);
186-
return;
185+
dest_file = fopen(dest_path, "wb");
186+
if (dest_file == nullptr) {
187+
LOGD("Error opening destination file");
188+
fclose(source_file);
189+
exit(EXIT_FAILURE);
190+
}
191+
192+
while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, source_file)) > 0) {
193+
if (fwrite(buffer, 1, bytes_read, dest_file) != bytes_read) {
194+
LOGD("Error writing to destination file");
195+
fclose(source_file);
196+
fclose(dest_file);
197+
exit(EXIT_FAILURE);
198+
}
187199
}
188200

189-
while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
190-
result += buffer.data();
201+
if (ferror(source_file)) {
202+
LOGD("Error reading from source file");
191203
}
192-
// LOGD("result: %s", result.c_str());
193204

194-
pclose(pipe);
195-
free(command);
205+
fclose(source_file);
206+
fclose(dest_file);
196207
}
197208

198209
static void companion_handler(int i) {
@@ -207,6 +218,13 @@ static void companion_handler(int i) {
207218
bool frida_config_mode = j["package"]["mode"]["config"];
208219

209220
writeString(i, target_package_name);
221+
222+
bool enable_gadget_injection;
223+
read(i, &enable_gadget_injection, sizeof(enable_gadget_injection));
224+
if (!enable_gadget_injection) {
225+
return;
226+
}
227+
210228
write(i, &delay, sizeof(delay));
211229

212230
#ifdef __arm__
@@ -223,18 +241,22 @@ static void companion_handler(int i) {
223241
writeString(i, frida_gadget_name);
224242
std::string frida_gadget_path = module_dir + "/" + frida_gadget_name;
225243

226-
std::string format = "cp %s /data/data/%s/";
227-
244+
std::string copy_src;
245+
std::string copy_dst;
228246
if (frida_config_mode) {
229247
std::regex frida_config_pattern(".*-gadget\\.config$");
230248
std::string frida_config_name = find_matching_file(module_dir, frida_config_pattern);
231249
std::string frida_config_path = module_dir + "/" + frida_config_name;
232250

233251
std::string new_frida_config_name = frida_gadget_name.substr(0, frida_gadget_name.find_last_of('.')) + ".config.so";
234-
executeCommand(frida_config_path.c_str(), target_package_name.c_str(), (format + new_frida_config_name).c_str());
252+
copy_src = frida_config_path;
253+
copy_dst = "/data/data/" + target_package_name + "/" + new_frida_config_name;
254+
copy_file(copy_src.c_str(), copy_dst.c_str());
235255
}
236256

237-
executeCommand(frida_gadget_path.c_str(), target_package_name.c_str(), format.c_str());
257+
copy_src = frida_gadget_path;
258+
copy_dst = "/data/data/" + target_package_name + "/" + frida_gadget_name;
259+
copy_file(copy_src.c_str(), copy_dst.c_str());
238260
}
239261

240262
REGISTER_ZYGISK_MODULE(MyModule)

module/src/main/cpp/tool/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <fstream>
55
#include <thread>
66
#include <regex>
7+
#include <csignal>
78

89
#include "logcat.h"
910
#include "nlohmann/json.hpp"
@@ -102,6 +103,17 @@ std::string find_matching_file(const fs::path& directory, const std::regex& patt
102103
return ""; // Return an empty string if no match is found
103104
}
104105

106+
// Function to handle signals like Ctrl + C (SIGINT)
107+
void signalHandler(int signal) {
108+
json j = get_json(config_file_path);
109+
std::vector<std::string> key_path;
110+
key_path = {"package", "name"};
111+
update_json(j, key_path, "com.hackcatml.test");
112+
write_json(j, config_file_path);
113+
114+
exit(signal);
115+
}
116+
105117
int main(int argc, char* argv[]) {
106118
uint uid = getuid();
107119
if (uid != 0) {
@@ -169,6 +181,9 @@ int main(int argc, char* argv[]) {
169181
std::thread t(write_json, j, config_file_path);
170182
t.detach();
171183

184+
// Register signal handler for SIGINT (Ctrl + C)
185+
std::signal(SIGINT, signalHandler);
186+
172187
logcat();
173188

174189
return 0;

0 commit comments

Comments
 (0)