using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using NewLife;
namespace NewLife.XCoder.Tools.Md5;
/// <summary>MD5 反查结果</summary>
/// <param name="Plain">命中的明文;未命中为 null</param>
/// <param name="Source">来源:local / online / none</param>
public sealed record Md5LookupResult(String? Plain, String Source)
{
/// <summary>是否命中</summary>
public Boolean Found => !Plain.IsNullOrEmpty();
}
/// <summary>MD5 反查:先查内置常见口令表,再查在线库,最后才引导用户去浏览器</summary>
/// <remarks>
/// 内置表覆盖最常见的弱口令,命中即出结果,不产生网络请求;
/// 在线查询只做一次 HTTP GET 并解析页面文本,失败不抛异常(离线也能用本地表)。
/// </remarks>
public static class Md5Lookup
{
/// <summary>常见弱口令字典(命中率最高的前若干条)</summary>
public static IReadOnlyList<String> CommonPasswords { get; } =
[
"123456", "123456789", "12345678", "111111", "123123", "000000", "888888", "666666",
"password", "password1", "passw0rd", "admin", "admin123", "root", "guest", "test",
"qwerty", "qwerty123", "abc123", "1qaz2wsx", "iloveyou", "woaini", "woaini1314",
"zhangsan", "lisi", "wangwu", "1234567890", "asdfgh", "zxcvbnm", "monkey", "dragon",
];
/// <summary>在线反查地址模板</summary>
public const String OnlineUrl = "https://md5.gromweb.com/?md5=";
/// <summary>计算字符串的 32 位小写 MD5</summary>
/// <param name="text">明文</param>
/// <returns>32 位十六进制摘要</returns>
public static String Hash(String text)
{
var data = MD5.HashData(Encoding.UTF8.GetBytes(text ?? ""));
return Convert.ToHexString(data).ToLowerInvariant();
}
/// <summary>在本地常见口令表中查找</summary>
/// <param name="hash">32 位 MD5</param>
/// <returns>命中返回明文,否则 null</returns>
public static String? LookupLocal(String? hash)
{
if (hash.IsNullOrWhiteSpace()) return null;
var target = hash!.Trim().ToLowerInvariant();
if (target.Length != 32) return null;
foreach (var item in CommonPasswords)
{
if (Hash(item) == target) return item;
}
return null;
}
/// <summary>从在线页面文本中提取明文</summary>
/// <param name="html">页面内容</param>
/// <returns>明文;未命中返回 null</returns>
public static String? ParseOnline(String? html)
{
if (html.IsNullOrWhiteSpace()) return null;
// 新版页面:<a class="String" href="/?string=password">password</a>
var match = System.Text.RegularExpressions.Regex.Match(html!, "class=\"String\"[^>]*>(?<v>[^<]+)</a>", RegexOptions.IgnoreCase);
if (match.Success)
{
var value = DecodeText(match.Groups["v"].Value);
if (value.Length > 0) return value;
}
// 兼容旧版页面:<em class="long-content string">明文</em>
match = System.Text.RegularExpressions.Regex.Match(html!, "class=\"long-content string\"[^>]*>(?<v>[^<]+)</", RegexOptions.IgnoreCase);
if (match.Success)
{
var value = DecodeText(match.Groups["v"].Value);
if (value.Length > 0) return value;
}
return null;
}
private static String DecodeText(String? text)
{
if (text.IsNullOrWhiteSpace()) return "";
return System.Net.WebUtility.HtmlDecode(text!.Trim());
}
/// <summary>查询:先本地,再在线</summary>
/// <param name="hash">32 位 MD5</param>
/// <param name="timeoutSeconds">在线查询超时秒数</param>
/// <param name="token">取消</param>
/// <returns>查询结果</returns>
public static async Task<Md5LookupResult> LookupAsync(String? hash, Int32 timeoutSeconds = 8, CancellationToken token = default)
{
if (hash.IsNullOrWhiteSpace()) return new Md5LookupResult(null, "none");
var local = LookupLocal(hash);
if (local != null) return new Md5LookupResult(local, "local");
try
{
using var client = new HttpClient { Timeout = TimeSpan.FromSeconds(timeoutSeconds) };
// 站点会按 User-Agent 抦截空 UA 请求
if (!client.DefaultRequestHeaders.Contains("User-Agent"))
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (compatible; XCoder)");
var html = await client.GetStringAsync(OnlineUrl + Uri.EscapeDataString(hash!.Trim()), token).ConfigureAwait(false);
var online = ParseOnline(html);
return new Md5LookupResult(online, online == null ? "none" : "online");
}
catch (Exception)
{
// 离线或接口异常时退回「未命中」,由界面提示去浏览器
return new Md5LookupResult(null, "none");
}
}
}
|