Java安全学习笔记(七)-攻击消息摘要保存的口令

来源:互联网 发布:淘宝秒杀抢拍器手机版 编辑:程序博客网 时间:2024/05/22 15:56
通过消息摘要保存口令的学习,认为保存口令较为安全的原因是,攻击者即使通过攻击得到了口令文件,知道了账号的口令摘要,也难以通过该值反推出口令的值,因而无法登录系统。但是当口令的安全系数比较弱时,也就是说口令的位数很少时,攻击者很容易通过字典式攻击由口令的消息摘要反推出原有口令的值。本实例将演示如何用字典式攻击消息摘要保存的口令。
之前我们用MD5生成消息摘要和密文,如果有人获取到消息摘要后,会利用字典式攻击解析消息摘要。
攻击消息摘要保存的口令的技术要点如下:
生成字符串组合
生成字典;
保存字典;
用一个个生成的摘要串去进行尝试匹配攻击。
package core;import java.io.BufferedReader;import java.io.FileOutputStream;import java.io.FileReader;import java.io.PrintWriter;import java.security.MessageDigest;/** * 攻击消息摘要保存的口令 */public class Attack_Mess {public static void main(String[] args) throws Exception {///* 程序开始时间 *///long start = System.currentTimeMillis();//System.out.println("程序开始执行时间为:" + start);/* 获取用户的摘要信息 */String user = "";String password = "";BufferedReader in = new BufferedReader(new FileReader("user_pass.txt"));while ((user = in.readLine()) != null) {password = in.readLine();if (user.equals("msg")) {System.out.println("获取用户的摘要信息为:");System.out.println(password + "\n");break;}}MessageDigest md = MessageDigest.getInstance("MD5");PrintWriter out = new PrintWriter(new FileOutputStream("dictary.txt"));/** 生成2位口令的字典 */for (int i1 = 'a'; i1 <= 'z'; i1++) {for (int i2 = 'a'; i2 <= 'z'; i2++) {char[] ch = { (char) i1, (char) i2 };String passwd = new String(ch);md.update(passwd.getBytes("UTF8"));byte s[] = md.digest();String result = "";for (int i = 0; i < s.length; i++) {result += Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);}out.print(passwd + ",");// 向字典内写入字符out.println(result);// 向字典内写入对应字符的消息摘要,}}in.close();out.close();Dictionary(password);///* 程序结束时间 *///long end = System.currentTimeMillis();//System.out.println("程序结束执行时间为:" + end);//long dis1 = (end - start) / 1000;//System.out.println("程序执行时间/s " + dis1);//long dis2 = (end - start) / 1000;//System.out.println("程序执行时间/min " + dis2);}public static void Dictionary(String pass) throws Exception {// 查看生成的字典是否包含给定的消息摘要String md, str;BufferedReader in = new BufferedReader(new FileReader("dictary.txt"));// 读取生成的字典while ((md = in.readLine()) != null) {str = md.substring(md.lastIndexOf(",") + 1);// 取出消息摘要if (str.equals(pass)) {System.out.println("根据用户的摘要信息而破解的口令为:");System.out.println(md.substring(0, md.lastIndexOf(",")));// 输出口令break;} else {//System.out.println("该字符串" + str + "匹配失败");}}in.close();}}


源程序解读:
(1)本实例的核心是三重for循环生成字典,为了节省时间,只设置了2个字符(小写a~z)的的情况,就这样就花费很多时间,实际生活中密码设的越长越复杂越不容易破解。
(2)程序中的注释都是调试代码,可以取消注释进行代码调试。