Skip to content

Commit c242f6c

Browse files
driver loader nonsense removed
1 parent 1f0f46e commit c242f6c

File tree

5 files changed

+71
-237
lines changed

5 files changed

+71
-237
lines changed

include/ntw/concepts.hpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2020 Justas Masiulis
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <type_traits>
20+
#include <cstddef>
21+
#include <ranges>
22+
23+
namespace ntw {
24+
25+
template<class T>
26+
concept Byte =
27+
(sizeof(T) == 1 && std::is_integral_v<T>) || std::is_same_v<T, std::byte>;
28+
29+
template<class R>
30+
concept ByteRange =
31+
std::ranges::contiguous_range<R>&& Byte<std::ranges::range_value_t>;
32+
33+
} // namespace ntw

include/ntw/sys/driver.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2020 Justas Masiulis
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
#include "../result.hpp"
19+
#include "../unicode_string.hpp"
20+
#include "../io/registry_key.hpp"
21+
22+
namespace ntw::sys {
23+
24+
NTW_INLINE status load_driver(unicode_string service_name);
25+
26+
NTW_INLINE status unload_driver(unicode_string service_name);
27+
28+
} // namespace ntw::sys
29+
30+
#include "impl/driver_loader.inl"

include/ntw/sys/driver_loader.hpp

-86
This file was deleted.

include/ntw/sys/impl/driver_loader.inl

+5-138
Original file line numberDiff line numberDiff line change
@@ -15,151 +15,18 @@
1515
*/
1616

1717
#pragma once
18-
#include "../driver_loader.hpp"
19-
#include <algorithm>
18+
#include "../driver.hpp"
2019

2120
namespace ntw::sys {
2221

23-
NTW_INLINE driver::~driver() noexcept { static_cast<void>(unload()); }
24-
25-
NTW_INLINE driver::driver(driver&& other) noexcept
26-
: _service_path(_service_path_buffer, other._service_path.size())
27-
{
28-
auto& sp = other._service_path.get();
29-
sp.Buffer = nullptr;
30-
for(auto i = 0; i < (sp.Length >> 1); ++i)
31-
_service_path_buffer[i] = other._service_path_buffer[i];
32-
}
33-
34-
NTW_INLINE driver& driver::operator=(driver&& other) noexcept
22+
status load_driver(unicode_string service_name)
3523
{
36-
unload();
37-
_service_path.get().Length = other._service_path.get().Length;
38-
_service_path.get().MaximumLength = other._service_path.get().MaximumLength;
39-
for(auto i = 0; i < (other._service_path.get().Length >> 1); ++i)
40-
_service_path_buffer[i] = other._service_path_buffer[i];
41-
return *this;
24+
return NTW_SYSCALL(NtLoadDriver)(&service_name.get());
4225
}
4326

44-
NTW_INLINE status driver::_build_service_path(unicode_string file_path) noexcept
27+
status unload_driver(unicode_string service_name)
4528
{
46-
// load into service path buffer
47-
// L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\"
48-
// obfuscation + speed
49-
auto uptr = reinterpret_cast<std::uint64_t*>(_service_path_buffer);
50-
uptr[0] = 28992356398268508;
51-
uptr[1] = 32088645568757865;
52-
uptr[2] = 27303403459444857;
53-
uptr[3] = 30962698416554083;
54-
uptr[4] = 34058828670304357;
55-
uptr[5] = 30681206260760691;
56-
uptr[6] = 32088649860579420;
57-
uptr[7] = 32651569751457906;
58-
uptr[8] = 32651569752113219;
59-
uptr[9] = 23362886930727026;
60-
uptr[10] = 23362818211577957;
61-
uptr[11] = 29555379368231013;
62-
uptr[12] = 25896191785238627;
63-
64-
// find first separator
65-
const auto first = std::find(file_path.rbegin(), file_path.rend(), L'\\');
66-
if(first == file_path.rend())
67-
STATUS_OBJECT_PATH_SYNTAX_BAD;
68-
69-
// find file extension
70-
const auto last = std::find(file_path.rbegin(), first, L'.');
71-
72-
// check size
73-
const std::size_t size = (first - last) - 1;
74-
if(size > 256 - 52)
75-
return STATUS_NAME_TOO_LONG;
76-
77-
// copy file name without extension
78-
std::copy_n(first.base(), size, _service_path_buffer + 52);
79-
80-
_service_path = { _service_path_buffer, static_cast<std::uint16_t>(size + 52) };
81-
return STATUS_SUCCESS;
29+
return NTW_SYSCALL(NtUnloadDriver)(&service_name.get());
8230
}
8331

84-
NTW_INLINE status driver::_init_service_fields(unicode_string path,
85-
const io::unique_reg_key& reg,
86-
start start_type,
87-
error_control error_control_type,
88-
type driver_type) noexcept
89-
{
90-
alignas(8) wchar_t buffer[16];
91-
auto uibuffer = reinterpret_cast<std::uint64_t*>(buffer);
92-
93-
// ImagePath
94-
uibuffer[0] = 28992339220168777;
95-
uibuffer[1] = 32651513915506789;
96-
uibuffer[2] = 104;
97-
98-
auto status =
99-
reg.set({ buffer, 9 }, REG_EXPAND_SZ, path.begin(), path.byte_size() + 2);
100-
if(!status.success())
101-
return status;
102-
103-
// Type
104-
uibuffer[0] = 28429453692043348;
105-
106-
status = reg.set({ buffer, 4 }, static_cast<ulong_t>(driver_type));
107-
if(!status.success())
108-
return status;
109-
110-
// ErrorControl
111-
uibuffer[0] = 31244212048625733;
112-
uibuffer[1] = 30962724183933042;
113-
uibuffer[2] = 30399774233591924;
114-
115-
status = reg.set({ buffer, 12 }, static_cast<ulong_t>(error_control_type));
116-
if(!status.success())
117-
return status;
118-
119-
uibuffer[0] = 32088563964444755;
120-
uibuffer[1] = 116;
121-
122-
return reg.set({ buffer, 5 }, static_cast<ulong_t>(start_type));
123-
}
124-
125-
NTW_INLINE result<driver> driver::load(unicode_string path,
126-
start start_type,
127-
error_control error_control_type,
128-
type driver_type) noexcept
129-
{
130-
driver d;
131-
auto status = d._build_service_path(path);
132-
if(!status.success())
133-
return status;
134-
135-
io::reg_create_options options;
136-
if(start_type == start::manual_with_cleanup) {
137-
start_type = start::manual;
138-
options.non_preserved();
139-
}
140-
141-
const auto reg = io::unique_reg_key::create(
142-
d._service_path, io::reg_access{}.write(), options);
143-
if(!reg)
144-
return { reg.status() };
145-
146-
_init_service_fields(path, *reg, start_type, error_control_type, driver_type);
147-
148-
status = NTW_SYSCALL(NtLoadDriver)(&d._service_path.get());
149-
return { status, std::move(d) };
150-
}
151-
152-
NTW_INLINE status driver::unload() noexcept
153-
{
154-
if(_service_path.begin()) {
155-
const auto s = NTW_SYSCALL(NtUnloadDriver)(&_service_path.get());
156-
if(NT_SUCCESS(s))
157-
_service_path.get().Buffer = nullptr;
158-
return s;
159-
}
160-
return STATUS_NOT_FOUND;
161-
}
162-
163-
NTW_INLINE void driver::release() noexcept { _service_path.get().Buffer = nullptr; }
164-
16532
} // namespace ntw::sys

test/test_driver.cpp

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <ntw/sys/driver_loader.hpp>
1+
#include <ntw/sys/driver.hpp>
22
#include <ntw/ob/token.hpp>
33
#define CATCH_CONFIG_MAIN
44
#define WIN32_NO_STATUS
@@ -11,16 +11,6 @@ TEST_CASE("driver loader")
1111
using namespace ntw::ob;
1212
using namespace ntw::sys;
1313

14-
const auto token = token::open(process_ref{}, token_access{}.adjust_privileges());
15-
REQUIRE(token);
16-
17-
REQUIRE(token->replace_privilege(privilege::load_driver().enable()).success());
18-
19-
const auto driver = driver::load(L"\\??\\C:\\Windows\\System32\\drivers\\MEME.sys",
20-
driver::start::manual_with_cleanup,
21-
driver::error_control::normal,
22-
driver::type::device_driver);
23-
24-
INFO(std::hex << driver.status().get());
25-
REQUIRE(driver);
14+
ntw::sys::load_driver({});
15+
ntw::sys::unload_driver({});
2616
}

0 commit comments

Comments
 (0)