pthread_create
現在、このプログラムで 、pthread_join
、pthread_exit
、およびを使用してマルチスレッドを使用しようとしていますpthread_self
。次に、コードcrypt_r
の代わりに使用するつもりです。crypt
最大 8 スレッドまでしか使用できませんが、2 スレッドから始める方法さえわかりません。を宣言する 1 行しかありませんpthread_t t1,t2,t3,t4,t5,t6,t7,t8
。
これらの計画は、それらをに入れることですpthread_create
が、これらの値を初期化する以外に、ここからどこに行くべきかわかりません。
pthread_create の入力が次のようになることpthread_create(t1, NULL, ... , ...)
はわかっていますが、3 番目の入力を作成する方法や 4 番目の入力が何であるかはわかりません。次に、コマンド ライン引数で指定されたスレッドの数に基づいて、各スレッドがチェックする文字の範囲を分割する必要があります。これまでのところ、1 つのスレッドのみで動作するように設計してきましたがcrypt_r
、マルチスレッドに移行することを計画しています...
どうすればこれを機能させることができるのか、本当に混乱しています..可能であれば。
ある種の void 関数がpthread_create
.. の 3 番目のエントリであることは知っていますが、その関数は私の 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;
}