c - 在 C 中多线程密码检查器

标签 c multithreading pthreads pthread-join

目前正在尝试使用 pthread_createpthread_joinpthread_exitpthread_self 让此程序使用多线程。然后我打算在我的代码中使用 crypt_r 代替 crypt

最多只能有 8 个线程,但我什至不知道如何开始使用两个线程。我只有一行声明 pthread_t t1,t2,t3,t4,t5,t6,t7,t8

这些的计划是将它们放入 pthread_create 但除了初始化这些值之外,我不知道从这里去哪里。

我知道 pthread_create 的输入类似于 pthread_create(t1, NULL, ... , ...) 但我不知道如何进行第三个输入或第四个输入是什么输入甚至是。 然后我必须确保根据命令行 arg 指定的线程数拆分每个线程正在检查的字母范围。到目前为止,我将其设计为仅在一个线程上工作,并计划将其移动到具有多线程的 crypt_r...

我真的很困惑如何才能完成这项工作。如果可能的话。

我知道某种 void 函数是 pthread_create 的第三个入口.. 但该函数必须是我的 passwordChecker 吗?或者什么?

/*
crack.exe
*/
/*  g++ -o crack crack.c -lcrypt -lpthread */
//#define _GNU_SOURCE
#include <crypt.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <math.h>

void *passwordLooper(int ks, char target[9], char s[10]);
void *threadFunction(void *threads);

int main(int argc, char *argv[]){           /* usage = crack threads keysize target */
    int i = 0;
    /*  arg[0] = crack, arg[1] = #of threads arg[2] = size of password, arg[3] = hashed password being cracked */

    if (argc !=  4) {
        fprintf(stderr, "Too few/many arguements give.\n");
        fprintf(stderr, "Proper usage: ./crack threads keysize target\n");
        exit(0);
    }

    int threads = *argv[1]-'0';         // threads is now equal to the second command line argument number
    int keysize = *argv[2]-'0';         // keysize is now equal to the third command line argument number
    char target[9]; 
    strcpy(target, argv[3]);
    char salt[10];

    while ( i < 2 ){            //Takes first two characters of the hashed password and assigns them to the salt variable
        salt[i] = target[i];
        i++;
    }


    printf("threads = %d\n", threads);      /*used for testing */
    printf("keysize = %d\n", keysize);
    printf("target = %s\n", target);        
    printf("salt = %s\n", salt);        


    if (threads < 1 || threads > 8){
        fprintf(stderr, "0 < threads <= 8\n");
        exit(0);
    }                                               /*Checks to be sure that threads and keysize are*/
    if (keysize < 1 || keysize > 8){                                                /*of the correct size   */
        fprintf(stderr, "0 < keysize <= 8\n");
        exit(0);
    }

    pthread_t t1,t2,t3,t4,t5,t6,t7,t8;

    if ( threads = 1 ){
        pthread_create(&t1, NULL, *threadFunction, threads);
    }


    char unSalted[30];
    int j = 0;
    for (i = 2; target[i] != '\0'; i++){        /*generates variable from target that does not include salt*/
        unSalted[j] = target[i];
        j++;
    }
    printf("unSalted = %s\n", unSalted); //unSalted is the variable target without the first two characters (the salt)

    char password[9] = {0};
    passwordLooper(keysize, target, salt);


}




/*_____________________________________________________________________________________________________________*/
/*_____________________________________________________________________________________________________________*/
void *passwordLooper(int ks, char target[9], char s[10]){
    char password[9] = {0};
    struct crypt_data cd;   
    cd.initialized = 0;
    int result;

    for (;;){
        int level = 0; 
        while (level < ks && strcmp( crypt(password, s), target ) != 0) {
            if (password[level] == 0){
                password[level] = 'a';
                break;
            }
            if (password[level] >= 'a' && password[level] < 'z'){
                password[level]++;
                break;
            }
            if (password[level] == 'z'){
                password[level] = 'a';
                level++;
            }
        }

        char *cryptPW = crypt(password, s);
        result = strcmp(cryptPW, target);

        if (result == 0){               //if result is zero, cryptPW and target are the same
            printf("result = %d\n", result);
            printf ("Password found: %s\n", password);
            printf("Hashed version of password is %s\n", cryptPW);
            break;  
        }
        if (level >= ks){           //if level ends up bigger than the keysize, break, no longer checking for passwords
            printf("Password not found\n");
            break;
        }



    }
    return 0;
}

使用 malloc 结构

/*
crack.exe
By: Zach Corse
*/
/*  g++ -o crack crack.c -lcrypt -lpthread */
//#define _GNU_SOURCE
#include <crypt.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <math.h>
#include <malloc.h>


void *passwordLooper(void *passwordData);
//void *threadFunction(void *threads);
typedef struct{
    int keysize;
    char *target;
    char *salt;
}passwordData;

int main(int argc, char *argv[]){           /* usage = crack threads keysize target */
    int i = 0;
    /*  arg[0] = crack, arg[1] = #of threads arg[2] = size of password, arg[3] = hashed password being cracked */

    if (argc !=  4) {
        fprintf(stderr, "Too few/many arguements give.\n");
        fprintf(stderr, "Proper usage: ./crack threads keysize target\n");
        exit(0);
    }

    int threads = *argv[1]-'0';         // threads is now equal to the second command line argument number
    int keysize = *argv[2]-'0';         // keysize is now equal to the third command line argument number
    char target[9]; 
    strcpy(target, argv[3]);
    char salt[10];

    while ( i < 2 ){            //Takes first two characters of the hashed password and assigns them to the salt variable
        salt[i] = target[i];
        i++;
    }


    printf("threads = %d\n", threads);      /*used for testing */
    printf("keysize = %d\n", keysize);
    printf("target = %s\n", target);        
    printf("salt = %s\n", salt);        


    if (threads < 1 || threads > 8){
        fprintf(stderr, "0 < threads <= 8\n");
        exit(0);
    }                                               /*Checks to be sure that threads and keysize are*/
    if (keysize < 1 || keysize > 8){                                                /*of the correct size   */
        fprintf(stderr, "0 < keysize <= 8\n");
        exit(0);
    }

    pthread_t t1,t2,t3,t4,t5,t6,t7,t8;

    struct crypt_data data;
    data.initialized = 0;
    //~ passwordData.keysize = keysize;
    //~ passwordData.target = target;
    //~ passwordData.salt = salt;

    passwordData *pwd = (passwordData *) malloc(sizeof(pwd));
    pwd->keysize = keysize;
    pwd->target = target;
    pwd->salt = salt;


    //~ if ( threads = 1 ){
        //~ pthread_create(&t1, NULL, *threadFunction, threads);
    //~ }


    char unSalted[30];
    int j = 0;
    for (i = 2; target[i] != '\0'; i++){        /*generates variable from target that does not include salt*/
        unSalted[j] = target[i];
        j++;
    }
    printf("unSalted = %s\n", unSalted); //unSalted is the variable target without the first two characters (the salt)

    char password[9] = {0};
    passwordLooper(pwd);


}




/*_____________________________________________________________________________________________________________*/
/*_____________________________________________________________________________________________________________*/
void *passwordLooper(passwordData pwd){
    char password[9] = {0};
    int result;

    int ks = pwd.keysize;
    char *target = pwd.target;
    char *s = pwd.salt;

    for (;;){
        int level = 0; 
        while (level < ks && strcmp( crypt(password, s), target ) != 0) {
            if (password[level] == 0){
                password[level] = 'a';
                break;
            }
            if (password[level] >= 'a' && password[level] < 'z'){
                password[level]++;
                break;
            }
            if (password[level] == 'z'){
                password[level] = 'a';
                level++;
            }
        }

        char *cryptPW = crypt(password, s);
        result = strcmp(cryptPW, target);

        if (result == 0){               //if result is zero, cryptPW and target are the same
            printf("result = %d\n", result);
            printf ("Password found: %s\n", password);
            printf("Hashed version of password is %s\n", cryptPW);
            break;  
        }
        if (level >= ks){           //if level ends up bigger than the keysize, break, no longer checking for passwords
            printf("Password not found\n");
            break;
        }
    }
    return 0;
}

最佳答案

好吧,这是 pthread_create 的原型(prototype):

整数 pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg);

它表明第三个参数是一个指向函数的指针,该函数返回一个指向 void 的指针,并将一个指向 void 的指针作为其唯一参数。我看到你有一个原型(prototype):

void *threadFunction(void *threads);

哪个符合那些规范,但实际上并没有写出来? pthread_create 中的第 4 个参数只是一个指向 void 的指针,它基本上可以包含您想要的任何内容。

我猜你想要线程化的函数实际上是 passwordLooper,它有 3 个参数。但是 pthread_create 指定了一个只需要 1 个参数的函数。你必须找到一个解决方法,这样你就只能接受一个参数你的 passwordLooper 函数仍然传递你关心的所有 3 个变量。有几种方法可以做到这一点:

  1. 全局变量(糟糕,不是线程安全的,不要真的这样做)
  2. malloc() 一些内存,memcpy() 你的参数到其中(或指向它们的指针),然后将新 malloc() 的内存传递给 pthread_create()。在你调用的函数中,你必须将 3 个变量从 void * 中解析出来(hacky,但理论上可行)
  3. 定义一个包含您的 3 个参数的结构,分配该结构的副本,将您的变量复制到新分配的结构中,并将其作为您的第 4 个参数传递。这极大地简化了返回值的解析,并且是完成将多个变量作为 pthread_create 的第四个参数传递的一般方法

关于c - 在 C 中多线程密码检查器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073436/

相关文章:

java - 运动 : What is the best/safe way to shutdown a worker?

c - 从 pthread 退出会释放 malloced 内存吗?

c - 如何修复对膨胀/收缩函数的 undefined reference ?

C 函数之间的清晰性

java - 从服务返回同步消息,然后进行异步处理 - 担心挂起线程吗?

c++ - pthread_exit(NULL);不工作

c++ - 使用 printf() 改变多线程结果?

c - 在C中多次包含静态库

c argv[计数]+2

java - AWS Lambda Java 多线程