我試圖在函數openFile中打開this file並將其傳遞給函數commonPasswd,但在嘗試編譯時收到警告。警告:'fp'在此函數中未初始化使用[-Wuninitialized]
warning: ‘fp’ is used uninitialized in this function [-Wuninitialized]
if (openFile (fp)) {
^
如果我運行它,我得到一個分段錯誤(核心轉儲)。檢查你可以做./program 34JBXiZ7tYKF。
#define _XOPEN_SOURCE
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
static const char alphabet[] = "abc";
bool checkPass (const char* key, const char* hash, const char* salt)
{
if (!strcmp (crypt (key, salt), hash))
return true;
return false;
}
bool openFile (FILE* fp)
{
if ((fp = fopen ("./commonPasswd.txt", "r")) == NULL)
return false;
return true;
}
bool commonPasswd (const char* hash, const char* salt)
{
char* buf = calloc (20, sizeof (char));
FILE* fp;
if (openFile (fp)) {
while (fgets (buf, 20, fp) != NULL) {
if (checkPass (buf, hash, salt))
return true;
free (buf);
}
}
return false;
}
bool DES (char* argv[])
{
char salt[2];
strncpy(salt, argv[1], 2 * sizeof (char));
if (commonPasswd (argv[1], salt))
return true;
return false;
}
int main (int argc, char* argv[])
{
if (argc != 2) {
printf ("Usage: ./crack passwd\n");
return 1;
}
if (DES (argv))
return 0;
return 2;
}
'crypt'函數是否知道'salt'完全是2個字符長?因爲'salt'不一定包含'\ 0'值,所以如果它在'crypt'內部被處理爲一個字符串,這可能導致seg錯誤。 –
是的,它知道鹽的長度是2個字符,或者它也可以接受像'$ 1 $ anySizeSALT $'這樣的參數用於md5加密。 – Arturo