-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHimitsuCrack.c
More file actions
194 lines (157 loc) · 5.54 KB
/
Copy pathHimitsuCrack.c
File metadata and controls
194 lines (157 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/types.h>
static const char *SIGNATURES[] = {
"#!/bin/bash",
"#!/bin/sh",
"#!/usr/bin/env sh",
"#!/usr/bin/env bash",
"#! /bin/bash",
"#! /bin/sh",
"#! /usr/bin/env",
"if [",
"if\n[",
"then\n",
"echo \"",
"echo '"
};
#define NUM_SIGS (sizeof(SIGNATURES) / sizeof(SIGNATURES[0]))
int attempt_extraction(pid_t pid) {
char maps_path[64], mem_path[64];
snprintf(maps_path, sizeof(maps_path), "/proc/%d/maps", pid);
snprintf(mem_path, sizeof(mem_path), "/proc/%d/mem", pid);
FILE *maps_file = fopen(maps_path, "r");
int mem_fd = open(mem_path, O_RDONLY);
if (!maps_file || mem_fd < 0) {
if (maps_file) fclose(maps_file);
if (mem_fd >= 0) close(mem_fd);
return -1;
}
char line[512];
int found = 0;
while (fgets(line, sizeof(line), maps_file)) {
unsigned long start, end, inode = 0;
char perms[5] = {0};
char path[256] = "";
int parsed = sscanf(line, "%lx-%lx %4s %*x %*s %lu %255s",
&start, &end, perms, &inode, path);
if (parsed < 4 || perms[0] != 'r') continue;
if (inode != 0 && strstr(path, "memfd:") == NULL && strstr(path, "(deleted)") == NULL)
continue;
size_t size = end - start;
if (size > 1024 * 1024 * 256) continue;
char *buffer = malloc(size);
if (!buffer) continue;
ssize_t bytes_read = pread(mem_fd, buffer, size, start);
if (bytes_read > 0) {
void *match = NULL;
const char *matched_sig = NULL;
for (size_t i = 0; i < NUM_SIGS; i++) {
match = memmem(buffer, bytes_read, SIGNATURES[i], strlen(SIGNATURES[i]));
if (match) {
matched_sig = SIGNATURES[i];
break;
}
}
if (match) {
size_t offset = (char *)match - buffer;
size_t last_len = 0;
int stable_count = 0;
printf("[+] Signature match (\"%s\") in dynamic region 0x%lx\n", matched_sig, start);
printf("[*] Unpacking detected. Waiting for memory to stabilize...\n");
while (stable_count < 5) {
usleep(2000);
ssize_t fresh_read = pread(mem_fd, buffer, size, start);
if (fresh_read <= 0) break;
char *script_start = buffer + offset;
size_t max_len = fresh_read - offset;
size_t current_len = 0;
while (current_len < max_len && script_start[current_len] != '\0')
current_len++;
if (current_len != last_len) {
stable_count = 0;
last_len = current_len;
} else if (current_len > 0) {
stable_count++;
}
}
char *final_script_start = buffer + offset;
size_t final_len = last_len;
char dump_filename[128];
snprintf(dump_filename, sizeof(dump_filename), "extracted_%d.sh", pid);
FILE *dump_file = fopen(dump_filename, "wb");
if (dump_file) {
fwrite(final_script_start, 1, final_len, dump_file);
fclose(dump_file);
printf(" -> Extracted %zu bytes to %s\n", final_len, dump_filename);
found = 1;
} else {
perror("[-] Failed to write dump file");
}
}
}
free(buffer);
if (found) break;
}
close(mem_fd);
fclose(maps_file);
return found;
}
long long current_time_ms() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <path_to_binary> [args...]\n", argv[0]);
return 1;
}
pid_t pid = fork();
if (pid < 0) {
perror("[-] Fork failed");
return 1;
}
if (pid == 0) {
execvp(argv[1], &argv[1]);
perror("[-] Exec failed");
exit(1);
} else {
printf("[*] Spawned child process PID: %d\n", pid);
printf("[*] Polling memory for signatures...\n");
long long start_ms = current_time_ms();
long long timeout_ms = 2000;
struct timespec sleep_step = {0, 1000000L};
int extracted = 0;
while (current_time_ms() - start_ms < timeout_ms) {
int status;
if (waitpid(pid, &status, WNOHANG) == pid) {
printf("[!] Target process exited before payload was found.\n");
break;
}
int result = attempt_extraction(pid);
if (result == 1) {
extracted = 1;
break;
} else if (result == -1) {
break;
}
nanosleep(&sleep_step, NULL);
}
if (extracted)
printf("[*] Extraction successful. Terminating target PID %d...\n", pid);
else
printf("[-] Timeout or process exit reached. Terminating target PID %d...\n", pid);
kill(pid, SIGKILL);
waitpid(pid, NULL, 0);
printf("[*] Done.\n");
}
return 0;
}