1212#include " xdl.h"
1313#include " nlohmann/json.hpp"
1414
15+ #define BUFFER_SIZE 1024
16+
1517using zygisk::Api;
1618using zygisk::AppSpecializeArgs;
1719using 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 {
148151private:
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
198209static 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
240262REGISTER_ZYGISK_MODULE (MyModule)
0 commit comments