Skip to content

Commit c8e20d5

Browse files
fix: mb_kmp::searchOne now ignores inaccessible pages
1 parent 7138d86 commit c8e20d5

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

include/blook/memo.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -270,27 +270,24 @@ class MemoryRange : public Pointer {
270270
return !(*this == other);
271271
}
272272

273-
inline uint8_t operator*() {
273+
inline bool is_readable() const { return try_read().has_value(); }
274+
275+
inline std::optional<uint8_t> try_read() const {
274276
if (cache->buffer
275277
.empty() || /* !(cache->offset ∈ [ptr, ptr+cache->size]) */
276278
cache->offset > ptr.offset() ||
277279
cache->offset + cache->buffer.size() <= ptr.offset()) {
278-
/**
279-
* cache = std::make_shared<CacheBuffer>(
280-
ptr.read(nullptr, bufSize),
281-
ptr.offset()
282-
);
283-
*
284-
*/
285280
cache->buffer.resize(std::min(bufSize, size));
286281
if (!ptr.read(std::span(cache->buffer.data(), cache->buffer.size())))
287-
throw std::runtime_error("Failed to read memory");
282+
return {};
288283
cache->offset = ptr.offset();
289284
}
290285

291286
return cache->buffer[ptr.offset() - cache->offset];
292287
}
293288

289+
inline uint8_t operator*() { return try_read().value(); }
290+
294291
using value_type = uint8_t;
295292
using difference_type = std::ptrdiff_t;
296293
using pointer = uint8_t *;
@@ -316,8 +313,7 @@ ptr.offset()
316313
template <class Scanner = memory_scanner::mb_kmp>
317314
inline std::optional<Pointer>
318315
find_one(const std::vector<uint8_t> pattern) const {
319-
const auto span = std::span<uint8_t>((uint8_t *)_offset, _size);
320-
std::optional<size_t> res = Scanner::searchOne(span, pattern);
316+
std::optional<size_t> res = Scanner::searchOne((uint8_t*)_offset, _size, pattern);
321317
return res.and_then([this](const auto val) {
322318
return std::optional<Pointer>(Pointer(this->proc, this->_offset + val));
323319
});
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
#pragma once
22

3-
#include <vector>
3+
#include <cstdint>
44
#include <optional>
55
#include <span>
6-
#include <cstdint>
6+
#include <vector>
7+
78

89
namespace blook {
910

10-
namespace memory_scanner {
11-
constexpr unsigned char ANYpattern = 0xBC;
11+
namespace memory_scanner {
12+
constexpr unsigned char ANYpattern = 0xBC;
1213

13-
class mb_kmp {
14-
public:
15-
static std::optional<size_t> searchOne(std::span<uint8_t>, const std::vector<uint8_t> &);
16-
};
17-
}
14+
class mb_kmp {
15+
public:
16+
static std::optional<size_t> searchOne(uint8_t *data, size_t size,
17+
const std::vector<uint8_t> &pattern);
18+
};
19+
} // namespace memory_scanner
1820

19-
} // blook
21+
} // namespace blook

src/mb_kmp.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44

55
#include "blook/memory_scanner/mb_kmp.h"
6+
#include "blook/memo.h"
67
#include <vector>
78

89
namespace blook {
@@ -31,17 +32,24 @@ namespace blook {
3132

3233

3334
std::optional<size_t>
34-
memory_scanner::mb_kmp::searchOne(std::span<uint8_t> data, const std::vector<uint8_t> &pattern) {
35-
if (data.size() == 0 || pattern.size() == 0 || pattern.size() > data.size())
35+
memory_scanner::mb_kmp::searchOne(uint8_t* data, size_t size, const std::vector<uint8_t> &pattern) {
36+
if (size == 0 || pattern.size() == 0 || pattern.size() > size)
3637
return {};
3738

3839
std::vector<size_t> lps(pattern.size(), 0);
3940
ComputeLPSArray((void *) pattern.data(), pattern.size(), lps);
4041

4142
size_t i = 0, j = 0;
42-
char *dataa = (char *) data.data();
43-
while (i < data.size()) {
44-
if (*(data.data() + i) == *(pattern.data() + j) ||
43+
while (i < size) {
44+
// test if the page is accessible when switched page
45+
if ((size_t)(data + i) % 0x1000 == 0 || i == 0) {
46+
if(!blook::Pointer(data+i).try_read<int>(0)) {
47+
i += 0x1000 - ((size_t)(data + i) % 0x1000);
48+
continue;
49+
}
50+
}
51+
52+
if (*(data + i) == *(pattern.data() + j) ||
4553
*(pattern.data() + j) == memory_scanner::ANYpattern) {
4654
i++;
4755
j++;

src/platform/windows/module.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,6 @@ void *Module::inject(const std::string &dll_path, Module::InjectMethod method) {
158158
}
159159

160160
std::optional<MemoryRange> Module::section(const std::string &name) {
161-
// if (!proc->is_self())
162-
// throw std::runtime_error("The operation can only be accomplished
163-
// for the "
164-
// "current process currently. "
165-
// "Inject your code into target process
166-
// first.");
167161

168162
auto mod = base();
169163

0 commit comments

Comments
 (0)