Skip to content

Commit a4d54d1

Browse files
authored
Merge pull request Pennyw0rth#468 from 0xb11a1/raw-ntds-copy
Add raw-ntds-copy module
2 parents 7328c08 + 37de885 commit a4d54d1

4 files changed

Lines changed: 655 additions & 6 deletions

File tree

nxc/connection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import datetime
2+
import os
13
import random
24
import sys
35
import contextlib
@@ -15,6 +17,7 @@
1517
from nxc.loaders.moduleloader import ModuleLoader
1618
from nxc.logger import nxc_logger, NXCAdapter
1719
from nxc.context import Context
20+
from nxc.paths import NXC_PATH
1821
from nxc.protocols.ldap.laps import laps_search
1922
from nxc.helpers.pfx import pfx_auth
2023

@@ -156,6 +159,11 @@ def __init__(self, args, db, target):
156159
self.local_ip = None
157160
self.dns_server = self.args.dns_server
158161

162+
# Construct the output file template using os.path.join for OS compatibility
163+
base_log_dir = os.path.join(os.path.expanduser(NXC_PATH), "logs")
164+
filename_pattern = f"{self.hostname}_{self.host}_{datetime.now().strftime('%Y-%m-%d_%H%M%S')}".replace(":", "-")
165+
self.output_file_template = os.path.join(base_log_dir, "{output_folder}", filename_pattern)
166+
159167
# DNS resolution
160168
dns_result = self.resolver(target)
161169
if dns_result:
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
Add-Type -TypeDefinition @"
2+
using System;
3+
using System.Runtime.InteropServices;
4+
using Microsoft.Win32.SafeHandles;
5+
6+
public class CNativeMethods
7+
{
8+
public const uint GENERIC_READ = 0x80000000;
9+
public const uint OPEN_EXISTING = 3;
10+
public const uint FILE_SHARE_READ = 0x00000001;
11+
public const uint FILE_SHARE_WRITE = 0x00000002;
12+
public const uint FILE_SHARE_DELETE = 0x00000004;
13+
14+
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
15+
public static extern SafeFileHandle CreateFile(
16+
string lpFileName,
17+
uint dwDesiredAccess,
18+
uint dwShareMode,
19+
IntPtr lpSecurityAttributes,
20+
uint dwCreationDisposition,
21+
uint dwFlagsAndAttributes,
22+
IntPtr hTemplateFile
23+
);
24+
25+
[DllImport("kernel32.dll", SetLastError = true)]
26+
public static extern bool ReadFile(
27+
SafeFileHandle hFile,
28+
byte[] lpBuffer,
29+
uint nNumberOfBytesToRead,
30+
out uint lpNumberOfBytesRead,
31+
IntPtr lpOverlapped
32+
);
33+
34+
[DllImport("kernel32.dll", SetLastError = true)]
35+
public static extern bool SetFilePointerEx(
36+
SafeFileHandle hFile,
37+
long lDistanceToMove,
38+
out long lpNewFilePointer,
39+
uint dwMoveMethod
40+
);
41+
}
42+
43+
public enum EMoveMethod : uint
44+
{
45+
Begin = 0,
46+
Current = 1,
47+
End = 2
48+
}
49+
"@
50+
Function read_disk{
51+
$offset = [long]$args[0]
52+
$size = [int]$args[1]
53+
try {
54+
$handle = [CNativeMethods]::CreateFile("\\.\PHYSICALDRIVE0",
55+
[CNativeMethods]::GENERIC_READ,
56+
[CNativeMethods]::FILE_SHARE_READ -bor [CNativeMethods]::FILE_SHARE_WRITE -bor [CNativeMethods]::FILE_SHARE_DELETE,
57+
[IntPtr]::Zero, [CNativeMethods]::OPEN_EXISTING, 0, [IntPtr]::Zero)
58+
59+
if ($handle.IsInvalid) {
60+
throw "Failed to create file handle"
61+
}
62+
63+
$moveToHigh = 0
64+
$success = [CNativeMethods]::SetFilePointerEx($handle, $offset, [ref]$moveToHigh, [EMoveMethod]::Begin)
65+
if (-not $success) {
66+
throw "Failed to set file pointer"
67+
}
68+
69+
$buffer = New-Object byte[] $size
70+
$bytesRead = 0
71+
$success = [CNativeMethods]::ReadFile($handle, $buffer, $size, [ref]$bytesRead, [IntPtr]::Zero)
72+
73+
if (-not $success) {
74+
throw "Failed to read file"
75+
}
76+
77+
$memoryStream = New-Object System.IO.MemoryStream
78+
$gzipStream = New-Object System.IO.Compression.GzipStream($memoryStream, [System.IO.Compression.CompressionMode]::Compress)
79+
$gzipStream.Write($buffer, 0, $buffer.Length)
80+
$gzipStream.Close()
81+
82+
$compressedBytes = $memoryStream.ToArray()
83+
$compressedBase64 = [Convert]::ToBase64String($compressedBytes)
84+
85+
Write-Output $compressedBase64
86+
} catch {
87+
Write-Error "An error occurred: $_"
88+
}
89+
90+
finally {
91+
if ($handle -and !$handle.IsInvalid) {
92+
$handle.Close()
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)