【问题标题】:How do I compare and decrypt md5 password hashes in C? [closed]如何在 C 中比较和解密 md5 密码哈希? [关闭]
【发布时间】:2014-07-12 02:17:14
【问题描述】:

该程序用于比较密码哈希。我说的是Reading (filename),但后来我收到segmentation fault (core dumped) 错误。我相信我的 main 或 readfile 函数有问题。 fscanf 是否在这里引起了问题? main 中 for 循环中的中间参数是什么,我相信它是行数,对吗?我已经为更好的方向提供了 cmets。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crypt.h"

int tryguess(char *hash, char *guess)
{
    // Extract the salt from the hash
    char *salt;
    memcpy(salt, &hash[3], 8);
    salt[8] = '\0'; 
    // Hash the guess using the salt
    char *hashGuess = md5crypt(guess, salt);
    // Compare the two hashes
    if (strcmp(hashGuess, hash) == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

// Given a hash and a dictionary of guesses,
// try all guesses and return the matching guess.
char *crack(char *hash, char *dict[])
{
    int i = 0;
    while (dict[i])
    {
        if (tryguess(hash, dict[i])) return dict[i];
        i++;
    }
    return NULL;
}

// Read in a file.
// The first line of the file is the number of lines in the file.
// Returns an array of strings, with the last element being a NULL
// indicating the end of the array.
char **read_file(char *fname)
{
    char **dict;

    printf("Reading %s\n", fname);

    FILE *d = fopen(fname, "r");

    if (! d) return NULL;
    // Get the number of lines in the file
    char *size;
    fscanf(d, "%s[^\n]", size);
    int filesize = atoi(size); 

    // Allocate memory for the array of strings (character pointers)
    dict[0] = malloc(100 * sizeof(char *));

    // Read in the rest of the file, allocting memory for each string
    // as we go.
    int count = 0;
    int index = 0;
    while (count < filesize)
    {
        for (int i = 0; dict[i] != NULL; i++)
        {
            fscanf(d, "%s[^\n]\n", dict[i]);
            if (dict[i+1] != NULL)
            {
                dict[i+1] = malloc(1000);
            }
            count++;
            index++;
        }
    }


    // NULL termination. Last entry in the array should be NULL.
    dict[index] = NULL;

    printf("Done\n");
    fclose(d);
    return dict;
  }

int main(int argc, char *argv[])
{
    if (argc < 2) 
    {
        printf("Usage: %s hash_file dict_file\n", argv[0]);
        exit(1);
    }

    char **dictionary = read_file(argv[2]);
    char **hashes = read_file(argv[1]);

    // For each hash, try every entry in the dictionary.
    // Print the matching dictionary entry.
    for (int i = 0; i < (# of lines); i++)
    {
    char *hash = hashes[i];
    char *result = crack(hash, dictionary);
    printf("%s", result);
    }   
}

【问题讨论】:

  • md5 是一个哈希值。它不能被“解密”。你要求用汉堡包做一头牛。 md5是绞肉机。
  • 如果要解密,请查看彩虹表,因为MD5无法解密。
  • 变了,好些了吗?
  • @MarcB 是的,但是当然,如​​果您想复制特定的汉堡包,则可以繁殖和研磨许多不同的奶牛,直到找到与目标汉堡包相同的汉堡包为止。这是 OP 考虑代码的意图,所以您唯一的一点是术语之一。
  • 你无法解密它,你能做的是找到返回相同MD5哈希码的字符串(蛮力)。以下是一些有用的信息:Are there two known strings which have the same MD5 hash value?

标签: c encryption hash cryptography md5


【解决方案1】:

我看到的一个问题是(这可能导致分段错误):

// Extract the salt from the hash
char *salt;
memcpy(salt, &hash[3], 8);
salt[8] = '\0'; 

你不能给salt写任何东西,因为它只是指针, 没有进行内存分配。 你可以在堆栈上声明它,如果你知道它的最大大小,例如,char salt[16];。 用法也类似:memcpy(salt, &amp;hash[3], 8);

【讨论】:

  • 更改为 salt[9],但不幸的是仍然出现同样的错误
【解决方案2】:

分段错误(核心转储)是您在以下情况下遇到的错误:

通过寻址不存在/分配的内存。

尝试从非法内存位置读取将导致此错误。即

  1. 如果您打开一个文件,它会失败并且返回的文件指针为 NULL,您尝试从该文件指针中读取。这会给你一个分段错误。

【讨论】:

  • 我让它读取第一个文件,正如你在 main 中看到的,带有字典,但第二个返回一个段错误,即使文件存在
【解决方案3】:
    dict[i] = string;
    dict = malloc(1000);

这两条线在什么世界里一起有意义?你设置了一个指针(指向一个堆栈分配的字符串!)然后你忽略了你之前的缓冲区dict,转而使用一个新的缓冲区。这些指针错误需要修复!

【讨论】:

  • 我可以做 dict[i+1] = malloc(1000) 吗?
  • 仅此更改是不够的。请注意,在设置 dict[i] = string; 时,您完全忽略了 dict[i] 的当前值。停止堆栈分配的字符串,正确分配字典条目并将值直接放在字典中。还有,你怎么知道 1000 字节就够了?
  • 好的,我按照您所说的方式编辑了帖子。虽然仍然是段错误=/
猜你喜欢
  • 1970-01-01
  • 2013-05-14
  • 2023-03-29
  • 2021-04-10
  • 2016-10-29
  • 2019-11-11
  • 2022-01-24
  • 2011-06-05
  • 1970-01-01
相关资源
最近更新 更多