【问题标题】:printing results of base64_decode gives unexpected outputbase64_decode 的打印结果给出了意外的输出
【发布时间】:2013-12-20 04:15:03
【问题描述】:

对于一个课程,我得到了一个 base64 编码的加盐 sha-256 哈希密码文件。

文件格式为:

用户名:base64 编码的 sha256 密码:salt

我最初的想法是对哈希进行 base64 解码,这样我就剩下:

用户名:salted 哈希密码:salt

然后通过 JTR 或 hashcat 运行它来破解密码。

我的问题出在base64解码过程中。

我的代码如下:

public static byte[] decode(String string) {
    try {
        return new BASE64Decoder().decodeBuffer(string);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public static void splitLine(String strLine) 
throws Exception {
    StringTokenizer st = new StringTokenizer(strLine, ":");
    if (st.hasMoreTokens())
        userName = st.nextToken();
    if (st.hasMoreTokens())
        password = st.nextToken();
    if (st.hasMoreTokens())
        salt = st.nextToken();
}


public static void main(String[] argv) {
    String line = null;
    String pwdFile = null;
    int count = 0;
    try { 
         pwdFile = argv[0]; 
         BufferedReader br = new BufferedReader(new FileReader(pwdFile));

        line = br.readLine();
        while (line != null) {
            splitLine(line);

/* 替代#1:为哈希生成大量不可打印字符 */ System.out.println(userName+":"+new String(decode(password))+":"+salt);

/* 替代#2:给出散列每个字节的十进制值列表 */ System.out.println(userName+":"+Arrays.toString(decode(password))+":"+salt);

            count++;
            line = br.readLine();
         }
        br.close();
        System.err.println("total lines read: " + count);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
}

使用替代#1,我的输出文件中的行数比输入文件中的多 50,000 行,因此我假设一些解码的字符串包含我也需要修复的换行符。

如何以 hashcat 或 JTR 将识别为加盐 sha256 的格式返回并打印密码的原始哈希值?

【问题讨论】:

    标签: java hash decoding


    【解决方案1】:

    问题:您正在尝试使用 Base64 编码的密码哈希,当它们被解码时,有不可打印的字符

    背景: 当一个值被散列时,所有的字节都根据散列算法改变,得到的字节通常超出可打印字符的范围。 Base64 编码只是一个将所有字节映射为可打印字符的字母表。

    解决方案:使用 Base64 解码返回的字节,而不是尝试将它们变成字符串。在打印这些原始字节或将它们提供给 Hashcat 或 JTR 之前,将它们转换为十六进制表示 (Base16)。简而言之,您需要执行以下操作(恰好使用 Guava 库):

    String hex = BaseEncoding.base16().encode(bytesFromEncodedString);
    

    这是从a longer answer I posted浓缩而来的

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 2016-03-11
      相关资源
      最近更新 更多