forked from MagnetForensics/Hibr2Bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisk.cpp
180 lines (138 loc) · 4.56 KB
/
Disk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*++
Copyright (c) 2007 - 2017, Matthieu Suiche
Copyright (c) 2012 - 2014, MoonSols Limited
Copyright (c) 2015 - 2017, Comae Technologies FZE
Module Name:
Disk.cpp
Abstract:
This module contains the internal structure definitions and APIs used by
the Hibr2Bin.
Author:
Matthieu Suiche (m) 1-April-2016
Revision History:
--*/
#include "precomp.h"
BOOLEAN
WriteMemoryBlocksToDisk(
_In_ MemoryBlock *Base,
_In_ PPROGRAM_ARGUMENTS Vars
)
{
ULONG64 FileOffset = 0;
ULONG CacheOffset = 0;
PUCHAR Cache;
BOOLEAN Result = FALSE;
ULONG i = 0;
BYTE *Hash = NULL;
//
// Console
//
HANDLE Handle;
COORD Initial;
BOOLEAN NoMoreEntries = FALSE;
if (Vars->OutFileName) Base->GetContext()->CreateOutputFile(Vars->OutFileName);
else return FALSE;
//
// Create a one Mb Cache.
//
Cache = new byte[WRITE_CACHE_SIZE];
memset(Cache, 0, WRITE_CACHE_SIZE);
wprintf(L"\n");
Handle = GetStdHandle(STD_OUTPUT_HANDLE);
GetCursorPosition(Handle, &Initial);
if (!CryptInitSha256()) {
Red(L" Error: SHA256 initialization failed with error = %d\n", GetLastError());
}
MemoryNode *Current = Base->GetMemoryNodes()->GetFirstChild();
CompressedMemoryBlock *Block = NULL;
ULONG XpressPageIndex = 0;
while (Current)
{
ULONG BytesToWrite;
while (CacheOffset < WRITE_CACHE_SIZE)
{
if (Block == NULL)
{
ULONG64 Offset = Current->GetKeyObject()->Compressed.XpressHeader;
Block = new CompressedMemoryBlock(Base->GetContext(), Offset, TRUE);
//if (Current->GetKeyObject()->NoHeader)
Block->SetCompressionSize(Current->GetKeyObject()->CompressedSize);
Block->SetCompressionType(Current->GetKeyObject()->IsCompressed);
XpressPageIndex = 0;
}
while ((FileOffset < Current->GetKeyObject()->Range.Minimum) &&
(CacheOffset < WRITE_CACHE_SIZE))
{
memset(Cache + CacheOffset, 0, PAGE_SIZE);
FileOffset += PAGE_SIZE;
CacheOffset += PAGE_SIZE;
}
if ((CacheOffset < WRITE_CACHE_SIZE))
{
if (Current->GetKeyObject()->PageCount)
{
assert((Current->GetKeyObject()->Compressed.XpressIndex + XpressPageIndex) <= 0x10);
RtlCopyMemory(Cache + CacheOffset,
(PUCHAR)Block->GetDecompressedData() + (Current->GetKeyObject()->Compressed.XpressIndex + XpressPageIndex) * PAGE_SIZE,
PAGE_SIZE);
FileOffset += PAGE_SIZE;
CacheOffset += PAGE_SIZE;
XpressPageIndex += 1;
}
//
// Next Mdl.
//
if (FileOffset >= Current->GetKeyObject()->Range.Maximum)
{
MemoryNode *Right = Current->GetRightChild();
if (Right == NULL)
{
NoMoreEntries = TRUE;
break;
}
Current = Right;
if (Block)
{
delete Block;
Block = NULL;
}
}
}
}
BytesToWrite = CacheOffset;
SetConsoleCursorPosition(Handle, Initial);
White(L" [0x%llx of 0x%llx] ", FileOffset - BytesToWrite, Current->GetKeyObject()->Range.Maximum);
CryptHashData(Cache, BytesToWrite);
Result = Base->GetContext()->WriteFile(Cache, BytesToWrite);
if (Result == FALSE) goto CleanUp;
CacheOffset = 0;
memset(Cache, 0, WRITE_CACHE_SIZE);
if (NoMoreEntries) break;
}
Hash = CryptGetHash();
Result = TRUE;
CleanUp:
if (Result == TRUE)
{
SetConsoleCursorPosition(Handle, Initial);
Green(L" [0x%llx of 0x%llx] \n", FileOffset, Current->GetKeyObject()->Range.Maximum);
if (Hash != NULL)
{
White(L" SHA256 = ");
for (i = 0; i < CryptGetHashLen(); i += 1) wprintf(L"%02x", Hash[i]);
wprintf(L"\n");
delete[] Hash;
}
}
else
{
SetConsoleCursorPosition(Handle, Initial);
Red(L" [0x%llx of 0x%llx] \n", FileOffset, Current->GetKeyObject()->Range.Maximum);
}
if (Cache)
{
delete[] Cache;
Cache = NULL;
}
return Result;
}