|
| 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