Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NTLMv1 flag on GPO #101

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CommonLib/OutputTypes/GPO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
{
public class GPO : OutputBase
{
public bool NTLMv1Enabled { get; set; }
}
}
88 changes: 88 additions & 0 deletions src/CommonLib/Processors/GPOLmCompatibilityLevelProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.DirectoryServices.Protocols;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SharpHoundCommonLib.LDAPQueries;

namespace SharpHoundCommonLib.Processors
{
public class GPOLmCompatibilityLevelProcessor
{
private static readonly Regex NTLMv1Regex = new Regex(@"\\LmCompatibilityLevel *= *\d+ *, *(\d)", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);

private readonly ILogger _log;

private readonly ILDAPUtils _utils;

public GPOLmCompatibilityLevelProcessor(ILDAPUtils utils, ILogger log = null)
{
_utils = utils;
_log = log ?? Logging.LogProvider.CreateLogger("GPOLmCompatProc");
}

public Task<Boolean> ReadGPOLmCompatibilityLevel(ISearchResultEntry entry)
{
var dn = entry.DistinguishedName;
return ReadGPOLmCompatibilityLevel(dn);
}

public async Task<Boolean> ReadGPOLmCompatibilityLevel(string gpDn)
{
var opts = new LDAPQueryOptions
{
Filter = new LDAPFilter().AddAllObjects().GetFilter(),
Scope = SearchScope.Base,
Properties = CommonProperties.GPCFileSysPath,
AdsPath = gpDn
};
var filePath = _utils.QueryLDAP(opts).FirstOrDefault()?
.GetProperty(LDAPProperties.GPCFileSYSPath);
if (filePath == null)
{
_log.LogWarning("Unable to process {} for NTLMv1 flag", gpDn);
return false;
}

return await ProcessGPOTemplateFile(filePath);
}

/// <summary>
/// Parses a GPO GptTmpl.inf file and grep lmcompatibilitylevel value
/// </summary>
/// <param name="basePath"></param>
/// <returns>
/// lmcompatibilitylevel < 3
/// </returns>
internal async Task<Boolean> ProcessGPOTemplateFile(string basePath)
{
var templatePath = Path.Combine(basePath, "MACHINE", "Microsoft", "Windows NT", "SecEdit", "GptTmpl.inf");

if (!File.Exists(templatePath))
return false;

FileStream fs;
try
{
fs = new FileStream(templatePath, FileMode.Open, FileAccess.Read);
}
catch
{
return false;
}

using var reader = new StreamReader(fs);
var content = await reader.ReadToEndAsync();
var ntlmv1Match = NTLMv1Regex.Match(content);

if (!ntlmv1Match.Success)
return false;

//We've got a match! Lets figure out whats going on
var ntlmv1Text = int.Parse(ntlmv1Match.Groups[1].Value);
return ntlmv1Text < 3;
}
}
}
Loading