Skip to content

Commit 35aa378

Browse files
author
苏青安
committed
refactor(core): 修改密码尝试函数以接受zip_t指针,优化线程中的ZIP文件打开逻辑
1 parent 8148c1a commit 35aa378

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

bin/zip_bruteforce.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ volatile int found = 0;
1818
char found_password[PASSWORD_LENGTH + 1];
1919
pthread_mutex_t lock;
2020

21-
int try_password(const char* zip_path, const char* password) {
22-
int err = 0;
23-
zip_t* za = zip_open(zip_path, 0, &err);
24-
if (!za) return 0;
21+
int try_password(zip_t* za, const char* password) {
2522
if (zip_set_default_password(za, password) < 0) {
26-
zip_close(za);
2723
return 0;
2824
}
2925
zip_int64_t num_entries = zip_get_num_entries(za, 0);
@@ -46,14 +42,19 @@ int try_password(const char* zip_path, const char* password) {
4642
break;
4743
}
4844
}
49-
zip_close(za);
5045
return success;
5146
}
5247

5348
void* worker(void* arg) {
5449
ThreadData* data = (ThreadData*)arg;
5550
char password[PASSWORD_LENGTH + 1];
5651
password[PASSWORD_LENGTH] = '\0';
52+
int err = 0;
53+
zip_t* za = zip_open(data->zip_path, 0, &err);
54+
if (!za) {
55+
fprintf(stderr, "线程 %d 无法打开ZIP文件\n", data->thread_id);
56+
return NULL;
57+
}
5758
for (int i = data->start; i <= data->end; i++) {
5859
pthread_mutex_lock(&lock);
5960
if (found) {
@@ -62,7 +63,7 @@ void* worker(void* arg) {
6263
}
6364
pthread_mutex_unlock(&lock);
6465
snprintf(password, PASSWORD_LENGTH + 1, "%06d", i);
65-
if (try_password(data->zip_path, password)) {
66+
if (try_password(za, password)) {
6667
pthread_mutex_lock(&lock);
6768
if (!found) {
6869
found = 1;
@@ -72,6 +73,7 @@ void* worker(void* arg) {
7273
break;
7374
}
7475
}
76+
zip_close(za);
7577
return NULL;
7678
}
7779

src/Installer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static function compileCExecutable(): void
1414
$source = __DIR__ . '/../bin/zip_bruteforce.c';
1515
$output = __DIR__ . '/../resources/zip_bruteforce';
1616
if (!file_exists($output)) {
17-
$cmd = "gcc " . escapeshellarg($source) . " -o " . escapeshellarg($output) . " $(pkg-config --cflags --libs libzip) -lpthread 2>&1";
17+
$cmd = "gcc " . escapeshellarg($source) . " -o " . escapeshellarg($output) . " $(pkg-config --cflags --libs libzip) -L/usr/local/lib -lpthread 2>&1";
1818
exec($cmd, $out, $code);
1919
}
2020
}

0 commit comments

Comments
 (0)