C# NTLM 哈希计算器

2024-03-20

我最近开始学习C#。我尝试用这种语言生成 NTLM 哈希,但找不到为我执行此操作的函数。在 python 3.x 中我会导入hashlib并计算它hashlib.new("md4", "Hello, World!".encode("utf-16le")).

我在 C# 中的对象浏览器中进行了搜索,但没有找到任何内容,最接近的是 Windows NTLM 身份验证类。我还搜索了微软的文档并找到了哈希计算器,但仅适用于 sha1 和 md5。

有没有办法在 C# 中计算 NTLM 哈希?你能告诉我一个如何做的例子吗?我更喜欢一个简短的方法来保持简单。

Thanks.


您可以使用 Reflection 创建对现有加密提供程序的扩展,以调用 MD4 的 CNG(.Net 可能应该这样做,或者使之变得更容易):

namespace System.Security.Cryptography {
    [System.Runtime.InteropServices.ComVisible(true)]
    public abstract class MD4 : HashAlgorithm {
        static MD4() {
            CryptoConfig.AddAlgorithm(typeof(MD4CryptoServiceProvider), "System.Security.Cryptography.MD4");
        }

        protected MD4() {
            HashSizeValue = 128;
        }

        new static public MD4 Create() {
            return Create("System.Security.Cryptography.MD4");
        }

        new static public MD4 Create(string algName) {
            return (MD4)CryptoConfig.CreateFromName(algName);
        }
    }

    [System.Runtime.InteropServices.ComVisible(true)]
    public sealed class MD4CryptoServiceProvider : MD4 {
        internal static class Utils {
            internal static Type UtilsType = Type.GetType("System.Security.Cryptography.Utils");

            public static T InvokeInternalMethodOfType<T>(object o, object pType, string methodName, params object[] args) {
                var internalType = (pType is string internalTypeName) ? Type.GetType(internalTypeName) : (Type)pType;
                var internalMethods = internalType.GetMethods(BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | (o == null ? BindingFlags.Static : 0));
                var internalMethod = internalMethods.Where(m => m.Name == methodName && m.GetParameters().Length == args.Length).Single();
                return (T)internalMethod?.Invoke(o, args);
            }

            public static T GetInternalPropertyValueOfInternalType<T>(object o, object pType, string propertyName) {
                var internalType = (pType is string internalTypeName) ? Type.GetType(internalTypeName) : (Type)pType;
                var internalProperty = internalType.GetProperty(propertyName, BindingFlags.NonPublic | (o == null ? BindingFlags.Static : 0));
                return (T)internalProperty.GetValue(o);
            }

            internal static SafeHandle CreateHash(int algid) {
                return InvokeInternalMethodOfType<SafeHandle>(null, UtilsType, "CreateHash", GetInternalPropertyValueOfInternalType<object>(null, UtilsType, "StaticProvHandle"), algid);
            }

            internal static void HashData(SafeHandle h, byte[] data, int ibStart, int cbSize) {
                InvokeInternalMethodOfType<object>(null, UtilsType, "HashData", h, data, ibStart, cbSize);
            }

            internal static byte[] EndHash(SafeHandle h) {
                return InvokeInternalMethodOfType<byte[]>(null, UtilsType, "EndHash", h);
            }
        }

        internal const int ALG_CLASS_HASH = (4 << 13);
        internal const int ALG_TYPE_ANY = (0);
        internal const int ALG_SID_MD4 = 2;
        internal const int CALG_MD4 = (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_MD4);

        [System.Security.SecurityCritical]
        private SafeHandle _safeHashHandle = null;

        [System.Security.SecuritySafeCritical]
        public MD4CryptoServiceProvider() {
            if (CryptoConfig.AllowOnlyFipsAlgorithms)
                throw new InvalidOperationException("Cryptography_NonCompliantFIPSAlgorithm");
            Contract.EndContractBlock();
            // cheat with Reflection
            _safeHashHandle = Utils.CreateHash(CALG_MD4);
        }

        protected override void Dispose(bool disposing) {
            if (_safeHashHandle != null && !_safeHashHandle.IsClosed)
                _safeHashHandle.Dispose();
            base.Dispose(disposing);
        }

        public override void Initialize() {
            if (_safeHashHandle != null && !_safeHashHandle.IsClosed)
                _safeHashHandle.Dispose();

            _safeHashHandle = Utils.CreateHash(CALG_MD4);
        }

        protected override void HashCore(byte[] rgb, int ibStart, int cbSize) {
            Utils.HashData(_safeHashHandle, rgb, ibStart, cbSize);
        }

        protected override byte[] HashFinal() {
            return Utils.EndHash(_safeHashHandle);
        }
    }
}

完成此操作后,几个帮助器扩展将让您轻松使用它(我修改了它以创建一个单例,这样它就不必在每次使用它时都进行反射/创建的工作):

static class Ext {
    public static HashAlgorithm MD4Singleton;

    static Ext() { 
        MD4Singleton = System.Security.Cryptography.MD4.Create();   
    }

    public static byte[] MD4(this string s) { 
        return MD4Singleton.ComputeHash(System.Text.Encoding.Unicode.GetBytes(s));
    }

    public static string AsHexString(this byte[] bytes) { 
        return String.Join("", bytes.Select(h => h.ToString("X2")));
    }
}

现在您只需对一些示例数据调用扩展方法:

void Main() {
    var input = "testing";

    var hash = input.MD4();
    var hashStr = hash.AsHexString();
    Console.WriteLine(hashStr);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# NTLM 哈希计算器 的相关文章

随机推荐

  • IHttpActionResult 与异步任务

    我见过的大多数 Web API 2 0 方法都会返回IHttpActionResult 它被定义为 定义异步创建 System Net Http HttpResponseMessage 的命令 的接口 我对方法返回时发生的情况有点困惑asy
  • Jenkins 管道、bash 和管道

    我有一个输出字符串 我想对其运行 tr 和 jq 命令 管道是这样有意义的 IP sh script echo spawnServer jq 0 tr d returnStdout true 不幸的是 詹金斯管道讨厌管道 所以我得到的是 t
  • 我如何检查移动数据或 wifi 是否打开或关闭。 ios 快速

    在我的应用程序中 我正在检查移动数据是否关闭 是否会显示弹出窗口 例如检查您的数据连接 为此我写了这段代码 import Foundation import SystemConfiguration public class Reachabi
  • 在 Cypress 测试中尝试使用 Auth0 登录时出错

    我们的应用程序只能由经过身份验证的用户访问 并且我们使用Auth0 https auth0 com 用于身份验证 我们已经开始编写 Cypress 测试 并且在每次测试之前尝试使用 Auth0 JavaScript 客户端登录 第一个测试总
  • F#:带有类型定义的引用?

    我正在使用引号 但看不到类型定义的表达式模式 真的没有吗 还是我错过了什么 lt type MyType name string member x Name name gt 给出 引号文字中出现意外的关键字 type 你不能 你只能引用代码
  • Python Spark Dataframe 到 Elasticsearch

    我不知道如何使用 Spark 中的 python 将数据帧写入 Elasticsearch 我按照以下步骤操作here https db blog web cern ch blog prasanth kothuri 2016 05 inte
  • 如何在eclipse软件中获得Redo

    我需要找回我的程序 如何获取Redoeclipse pls 的键盘快捷键任何人都可以帮助我 You can use below code to implement Redo public class TextAreaDemoB extend
  • 如何对 numpy 数组进行舍入?

    我有一个 numpy 数组 如下所示 data np array 1 60130719e 01 9 93827160e 01 3 63108206e 04 我想将每个元素四舍五入到小数点后两位 我怎样才能这样做呢 Numpy 提供了两种相同
  • 计算一年内信用卡每月最低还款额的代码

    请我尝试找出我的推理有什么问题 从而找出我的结果 我正在学习一门在线课程 我需要计算 12 个月内消除信用卡债务所需的最低金额 我得到了年利率 债务金额 余额 的值以及每月还款额应增加的值 10 的倍数 根据我的推理 我生成的代码应该在几个
  • 如何查找Active Directory的全局编录?

    我想在 Active Directory 环境中搜索用户GC DC xxx DC yyy DC zzz格式 但是 如何以编程方式在任意 Active Directory 环境中查找全局编录 每个域名总是对应一个全局目录吗 我可以尝试其他替代
  • 将图像旋转 X 度 C# wpf [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 这已经困扰我很多年了 我只想要一个简单的方法来将图像旋转 X 度 这是针对炮塔防御游戏 其中炮塔需要向某个方向射击 我想要这样的东西 pub
  • Linux 上 C 语言中字符数组和指针的分段错误

    所以我有以下程序 int main char one computer char two another two 1 b one 1 b return 0 它在 one 1 b 行上出现段错误 这是有道理的 因为指针 one 指向的内存必须
  • 如果在图表渲染完成之前调用 FSharpChart.SaveAs (),则保存空白图像

    在 F Interactive 中运行时 我希望以下代码创建一个简单的饼图并将其保存到磁盘 let pie FSharpChart Pie Apples 1 Oranges 2 Bananas 3 FSharpChart SaveAs te
  • 使用Powershell将Excel中的文本转换为数字

    我有一个 Excel 文件 其中三列设置为 数字 但是 当我打开该文件时 我会看到以下内容 我在这里找到了一个有用的链接 堆栈溢出链接 https stackoverflow com questions 68941811 attemptin
  • 对于 AMD 模块,什么时候(或为什么)可以在 Define() 中使用 require() ?

    我对AMD模块 例如使用RequireJs或curl js 的理解是 require 用于异步加载不同的模块 加载后执行回调 fn 要定义模块 您将有单独的脚本使用define 但我见过一些模块使用require 在它们的函数定义中 例如
  • 计算 pandas 系列的*滚动*最大回撤

    编写一个计算时间序列最大回撤的函数非常容易 写下来需要一点点思考O n 时间而不是O n 2 时间 但情况并没有那么糟糕 这将起作用 import numpy as np import pandas as pd import matplot
  • 如何构建一个同时暴露rest和soap服务的Spring Boot jar

    我一直在考虑为开发人员社区构建一个测试 jar 以便公开 API 的下一个版本的预览 让存根返回具有精确格式的预期响应等 我们确实有 REST 和 SOAP API 我想构建 REST 服务不会有任何问题 因为网络上充斥着示例 令人惊讶的是
  • 方面未在 Spring 中执行

    我正在编写一个几乎完全受登录保护的网站 我正在使用 Spring Security 不过 有些页面不受保护 主页 登录页面 注册页面 忘记密码页面 我想要实现的是 如果用户在访问这些非安全页面时未登录 正常显示它们 如果用户已经登录 则重定
  • Firefox41 中的 GWT 重复编码 URL 中的令牌

    包含地点 活动等的 Gwt 应用程序 有没有其他人遇到过 Firefox 41 0 在 URL 标记中重复编码 看起来就像调用 PlaceController goto 时一样 循环直到崩溃 令牌以管道开头 search advanced
  • C# NTLM 哈希计算器

    我最近开始学习C 我尝试用这种语言生成 NTLM 哈希 但找不到为我执行此操作的函数 在 python 3 x 中我会导入hashlib并计算它hashlib new md4 Hello World encode utf 16le 我在 C