Skip to content

Commit

Permalink
Simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
zbalkan committed Feb 1, 2024
1 parent af63ef7 commit 72c8b70
Showing 1 changed file with 33 additions and 100 deletions.
133 changes: 33 additions & 100 deletions source/dumpntds/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,57 +20,6 @@ internal static class Program
WriteIndented = true
};

/// <summary>
/// The columns from the "ntds" ESENT database, just the columns from the
/// "datatable" table that are needed by the "ntdsxtract" script to parse
/// the user details including password hashes
/// </summary>
private static readonly List<string> userColumns = ["DNT_col",
"PDNT_col",
"time_col",
"Ancestors_col",
"ATTb590606",
"ATTm3",
"ATTm589825",
"ATTk589826",
"ATTl131074",
"ATTl131075",
"ATTq131091",
"ATTq131192",
"OBJ_col",
"ATTi131120",
"ATTb590605",
"ATTr589970",
"ATTm590045",
"ATTm590480",
"ATTj590126",
"ATTj589832",
"ATTq589876",
"ATTq591520",
"ATTq589983",
"ATTq589920",
"ATTq589873",
"ATTj589993",
"ATTj589836",
"ATTj589922",
"ATTk589914",
"ATTk589879",
"ATTk589918",
"ATTk589984",
"ATTk591734",
"ATTk36",
"ATTk589949",
"ATTj589993",
"ATTm590443",
"ATTm590187",
"ATTm590188",
"ATTm591788",
"ATTk591823",
"ATTk591822",
"ATTk591789",
"ATTi590943",
"ATTk590689"];

/// <summary>
/// Application entry point
/// </summary>
Expand Down Expand Up @@ -127,18 +76,7 @@ private static void ExportJson(Session session, JET_DBID dbid)
private static List<IDictionary<string, object>> ExtractDatatableAsList(Session session, JET_DBID dbid)
{
var datatableValues = new List<IDictionary<string, object>>();

// Extract and cache the columns from the "datatable" table. Note
// that we are only interested in the columns needed for "ntdsextract"
var propertyNames = new List<ColumnInfo>();
foreach (var column in Api.GetTableColumns(session, dbid, "datatable"))
{
if (!userColumns.Contains(column.Name))
{
continue;
}
propertyNames.Add(column);
}
var columns = new List<ColumnInfo>(Api.GetTableColumns(session, dbid, "datatable"));

using (var table = new Table(session, dbid, "datatable", OpenTableGrbit.ReadOnly))
{
Expand All @@ -149,25 +87,25 @@ private static List<IDictionary<string, object>> ExtractDatatableAsList(Session
while (Api.TryMoveNext(session, table))
{
var obj = new Dictionary<string, object>();
foreach (var property in propertyNames)
foreach (var column in columns)
{
formattedData = GetFormattedValue(session, table, property);
formattedData = GetFormattedValue(session, table, column);
// The first row has a null "PDNT_col" value which causes issues with the "ntdsxtract" scripts.
// esedbexport seems to have some other value, esedbexport parses the data, rather than using the API
if (property.Name == "PDNT_col")
if (column.Name == "PDNT_col")
{
if (formattedData.Length == 0)
{
obj.Add(property.Name, "0");
obj.Add(column.Name, "0");
continue;
}
}

var propertyValue = formattedData.Replace("\0", string.Empty);
var cellValue = formattedData.Replace("\0", string.Empty);
// Ignore emptry or null values
if (!string.IsNullOrEmpty(propertyValue))
if (!string.IsNullOrEmpty(cellValue))
{
obj.Add(property.Name, propertyValue);
obj.Add(column.Name, cellValue);
}
}

Expand All @@ -183,29 +121,25 @@ private static List<IDictionary<string, object>> ExtractDatatableAsList(Session
private static List<IDictionary<string, object>> ExtractLinkTableAsList(Session session, JET_DBID dbid)
{
var linktableValues = new List<IDictionary<string, object>>();
var columns = new List<ColumnInfo>(Api.GetTableColumns(session, dbid, "link_table"));

using (var table = new Table(session, dbid, "link_table", OpenTableGrbit.ReadOnly))
{
// Extract and cache the columns from the "link_table" table
var propertyNames = new List<ColumnInfo>(Api.GetTableColumns(session, dbid, "link_table"));

Api.JetSetTableSequential(session, table, SetTableSequentialGrbit.None);
Api.MoveBeforeFirst(session, table);

var temp = new List<dynamic>();

var formattedData = string.Empty;
while (Api.TryMoveNext(session, table))
{
var obj = new Dictionary<string, object>();
foreach (var property in propertyNames)
foreach (var column in columns)
{
formattedData = GetFormattedValue(session, table, property);
var propertyValue = formattedData.Replace("\0", string.Empty);
formattedData = GetFormattedValue(session, table, column);
var cellValue = formattedData.Replace("\0", string.Empty);
// Ignore emptry or null values
if (!string.IsNullOrEmpty(propertyValue))
if (!string.IsNullOrEmpty(cellValue))
{
obj.Add(property.Name, propertyValue);
obj.Add(column.Name, cellValue);
}
}

Expand All @@ -226,27 +160,19 @@ private static void ExportCsv(Session session, JET_DBID dbid)

private static void ExportDataTable(Session session, JET_DBID dbid)
{
// Extract and cache the columns from the "datatable" table. Note
// that we are only interested in the columns needed for "ntdsextract"
var columns = new List<ColumnInfo>();
foreach (var column in Api.GetTableColumns(session, dbid, "datatable"))
{
if (!userColumns.Contains(column.Name))
{
continue;
}
columns.Add(column);
}

using var file = new StreamWriter("datatable.csv");
using var table = new Table(session, dbid, "datatable", OpenTableGrbit.ReadOnly);

var columns = new List<ColumnInfo>(Api.GetTableColumns(session, dbid, "datatable"));

// Write out the column headers
var index = 0;
foreach (var property in userColumns)
foreach (var column in columns)
{
index++;
file.Write(property);
if (index != userColumns.Count)
file.Write(column.Name);
if (index != columns.Count)
{
file.Write("\t");
}
Expand Down Expand Up @@ -282,18 +208,18 @@ private static void ExportDataTable(Session session, JET_DBID dbid)

// Now write out each columns data
index = 0;
foreach (var property in userColumns)
foreach (var column in columns)
{
index++;
if (obj.TryGetValue(property, out var val))
if (obj.TryGetValue(column.Name, out var val))
{
file.Write(val);
}
else
{
file.Write(string.Empty);
}
if (index != userColumns.Count)
if (index != columns.Count)
{
file.Write("\t");
}
Expand All @@ -308,6 +234,7 @@ private static void ExportLinkTable(Session session, JET_DBID dbid)
{
using var file = new StreamWriter("linktable.csv");
using var table = new Table(session, dbid, "link_table", OpenTableGrbit.ReadOnly);

// Extract and cache the columns from the "link_table" table
var columns = new List<ColumnInfo>(Api.GetTableColumns(session, dbid, "link_table"));

Expand All @@ -327,8 +254,6 @@ private static void ExportLinkTable(Session session, JET_DBID dbid)
Api.JetSetTableSequential(session, table, SetTableSequentialGrbit.None);
Api.MoveBeforeFirst(session, table);

var temp = new List<dynamic>();

var currentRow = 0;
var formattedData = string.Empty;
while (Api.TryMoveNext(session, table))
Expand All @@ -347,7 +272,15 @@ private static void ExportLinkTable(Session session, JET_DBID dbid)
foreach (var column in columns)
{
index++;
file.Write(obj[column.Name]);
if (obj.TryGetValue(column.Name, out var val))
{
file.Write(val);
}
else
{
file.Write(string.Empty);
}

if (index != columns.Count)
{
file.Write("\t");
Expand Down

0 comments on commit 72c8b70

Please sign in to comment.