提问人:simanta 提问时间:11/2/2023 最后编辑:halfersimanta 更新时间:11/7/2023 访问量:51
需要根据用户在命令提示符 argv[1] 中输入的线程数来划分密码破解系统
Need to divide the password cracking system according to the amount of threads that is entered by the user in the command prompt argv[1]
问:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <crypt.h>
#include <unistd.h>
#include <pthread.h>
int count = 0; // A counter used to track the number of combinations explored so far
int num_threads = 4; // Default number of threads
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/**
Required by lack of standard function in C.
*/
void substr(char *dest, char *src, int start, int length) {
memcpy(dest, src + start, length);
*(dest + length) = '\0';
}
/**
This function can crack the kind of password explained above. All combinations
that are tried are displayed, and when the password is found, # is put at the
start of the line.
*/
void crack(char *salt_and_encrypted) {
int x, y, z; // Loop counters
char salt[7]; // String used in hashing the password
char plain[7]; // The combination of letters currently being checked
char *enc; // Pointer to the encrypted password
substr(salt, salt_and_encrypted, 0, 6);
for (x = 'A'; x <= 'Z'; x++) {
for (y = 'A'; y <= 'Z'; y++) {
for (z = 0; z <= 99; z++) {
sprintf(plain, "%c%c%02d", x, y, z);
enc = (char *)crypt(plain, salt);
count++;
if (strcmp(salt_and_encrypted, enc) == 0) {
pthread_mutex_lock(&mutex);
printf("#%-8d%s %s\n", count, plain, enc);
pthread_mutex_unlock(&mutex);
return; // Password found, exit the function
}
}
}
}
}
void* worker(void* arg) {
char *salt_and_encrypted = (char *)arg;
crack(salt_and_encrypted);
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <num_threads>\n", argv[0]);
return 1;
}
num_threads = atoi(argv[1);
char encrypted_password[] = "......."; // Replace with your encrypted password
pthread_t threads[num_threads];
int i;
for (i = 0; i < num_threads; i++) {
pthread_create(&threads[i], NULL, worker, encrypted_password);
}
for (i = 0; i < num_threads; i++) {
pthread_join(threads[i], NULL);
}
printf("%d solutions explored\n", count);
return 0;
}
这是我创建的代码。我需要将加密的密码放在encrypted_password变量中。加密密码从“$6$AS$”开始,因为这是在加密密码开头添加的盐。我需要根据在argv[1]上传递的参数进行多线程处理。
我已经尝试了代码的许多变体。无论我怎么尝试,我都得不到正确的答案。上面的代码是我最接近它的代码。该程序用于破解常规 C 中的密码,同时使用多线程语法。我的任务是使用多线程将工作负载拆分到多个线程上并查找密码。找到密码时,程序也需要停止。我确实尝试使用返回 0 关键字让程序停止,但它不起作用。
下面是我自己在执行代码之前制作的骨架代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <crypt.h>
#include <unistd.h>
int count=0; // A counter used to track the number of combinations explored so far
/**
Required by lack of standard function in C.
*/
void substr(char *dest, char *src, int start, int length){
memcpy(dest, src + start, length);
*(dest + length) = '\0';
}
/**
This function can crack the kind of password explained above. All combinations
that are tried are displayed and when the password is found, #, is put at the
start of the line. Note that one of the most time consuming operations that
it performs is the output of intermediate results, so performance experiments
for this kind of program should not include this. i.e. comment out the printfs.
*/
void crack(char *salt_and_encrypted){
int x, y, z; // Loop counters
char salt[7]; // String used in hashing the password. Need space for \0 // incase you have modified the salt value, then should modifiy the number accordingly
char plain[7]; // The combination of letters currently being checked // Please modifiy the number when you enlarge the encrypted password.
char *enc; // Pointer to the encrypted password
substr(salt, salt_and_encrypted, 0, 6);
for(x='A'; x<='Z'; x++){
for(y='A'; y<='Z'; y++){
for(z=0; z<=99; z++){
sprintf(plain, "%c%c%02d", x, y, z);
enc = (char *) crypt(plain, salt);
count++;
if(strcmp(salt_and_encrypted, enc) == 0){
printf("#%-8d%s %s\n", count, plain, enc);
//return; //uncomment this line if you want to speed-up the running time, program will find you the cracked password only without exploring all possibilites
}else{
printf("%-8d%s %s\n", count, plain, enc); //when the password is not found
}
}
}
}
}
int main(int argc, char *argv[]){
crack("$6$AS$07bMLxcKqVbTklYXuFw7alIrQnPfh6.oDeQIZHaud6iaAZj9IgbkPytVlU2nx.H10EGS49uOIoV3m3ZNZF87s0"); //Copy and Paste your ecrypted password here.
printf("%d solutions explored\n", count);
return 0;
}
我期待代码遍历所有加密密码,并找到我放入程序中的加密密码的解密密码。我将加密密码放在 encrypted_password 变量中。我需要代码根据用户希望使用的线程数量进行多线程处理。
为了解决这个问题,我接下来可以尝试什么?
答: 暂无答案
评论
crypt
函数已经返回了一个 ,因此显式转换(强制转换)几乎毫无用处。for (x = 'A'; x <= 'Z'; x++)
char *
char *
crypt
函数不是可重入的,也不是线程安全的。您应该改用。crypt_r