-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdie.cpp
More file actions
146 lines (123 loc) · 3.92 KB
/
Copy pathdie.cpp
File metadata and controls
146 lines (123 loc) · 3.92 KB
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
#include "die.hpp"
#include <nanobind/nanobind.h>
#include <nanobind/stl/filesystem.h>
#include <nanobind/stl/optional.h>
#include <nanobind/stl/string.h>
#include <nanobind/stl/vector.h>
#include <array>
#include <filesystem>
#include <functional>
#include <memory>
#include <string_view>
namespace nb = nanobind;
using namespace nb::literals;
namespace DIE
{
std::optional<std::string>
ScanFileA(std::string& pszFileName, uint32_t nFlags, std::string& pszDatabase)
{
auto res = ::DIE_ScanFileA(pszFileName.data(), static_cast<int>(nFlags), pszDatabase.data());
if ( res == nullptr )
{
return std::nullopt;
}
auto const res_str = std::string(res);
::DIE_FreeMemoryA(res);
return res_str;
}
std::optional<std::string>
ScanFileExA(std::string& pszFileName, uint32_t nFlags)
{
auto res = ::DIE_ScanFileExA(pszFileName.data(), static_cast<int>(nFlags));
if ( res == nullptr )
{
return std::nullopt;
}
auto const res_str = std::string(res);
::DIE_FreeMemoryA(res);
return res_str;
}
std::optional<std::string>
ScanMemoryA(std::vector<uint8_t>& memory, uint32_t flags, std::string& database)
{
char* pMemory = (char*)memory.data();
int nMemorySize = static_cast<int>(memory.size());
int nFlags = static_cast<int>(flags);
char* pszDatabase = database.data();
auto res = ::DIE_ScanMemoryA(pMemory, nMemorySize, nFlags, pszDatabase);
if ( res == nullptr )
{
return std::nullopt;
}
auto const res_str = std::string(res);
::DIE_FreeMemoryA(res);
return res_str;
}
std::optional<std::string>
ScanMemoryExA(std::vector<uint8_t>& memory, uint32_t flags)
{
char* pMemory = (char*)memory.data();
int nMemorySize = static_cast<int>(memory.size());
int nFlags = static_cast<int>(flags);
auto res = ::DIE_ScanMemoryExA(pMemory, nMemorySize, nFlags);
if ( res == nullptr )
{
return std::nullopt;
}
auto const res_str = std::string(res);
::DIE_FreeMemoryA(res);
return res_str;
}
int32_t
LoadDatabaseA(std::string& pszDatabase)
{
return ::DIE_LoadDatabaseA(pszDatabase.data());
}
#ifdef _WIN32
int
VB_ScanFile(
std::wstring& pwszFileName,
uint32_t nFlags,
std::wstring& pwszDatabase,
std::wstring& pwszBuffer,
uint32_t nBufferSize)
{
return ::DIE_VB_ScanFile(
pwszFileName.data(),
static_cast<int>(nFlags),
pwszDatabase.data(),
pwszBuffer.data(),
static_cast<int>(nBufferSize));
}
#endif // _WIN32
} // namespace DIE
NB_MODULE(_die, m)
{
nb::enum_<DIE::DieFlags>(m, "DieFlags")
.value("Deepscan", DIE::DieFlags::Deepscan)
.value("HeuristicScan", DIE::DieFlags::HeuristicScan)
.value("AlltypesScan", DIE::DieFlags::AlltypesScan)
.value("RecursiveScan", DIE::DieFlags::RecursiveScan)
.value("Verbose", DIE::DieFlags::Verbose)
.value("ResultAsXml", DIE::DieFlags::ResultAsXml)
.value("ResultAsJson", DIE::DieFlags::ResultAsJson)
.value("ResultAsTsv", DIE::DieFlags::ResultAsTsv)
.value("ResultAsCsv", DIE::DieFlags::ResultAsCsv)
.export_values();
m.doc() = "The native `die` module";
m.attr("__version__") = "0.5.0";
m.attr("die_version") = DIE_VERSION;
m.attr("dielib_version") = DIELIB_VERSION;
m.attr("dielib_tag") = DIELIB_TAG;
m.def("ScanFileA", DIE::ScanFileA, "filename"_a, "flags"_a, "database"_a, "Scan a file against known signatures");
m.def("ScanFileExA", DIE::ScanFileExA, "filename"_a, "flags"_a, "Scan a file");
m.def(
"ScanMemoryA",
DIE::ScanMemoryA,
"memory"_a,
"flags"_a,
"database"_a,
"Scan sequence of bytes against known signatures");
m.def("ScanMemoryExA", DIE::ScanMemoryExA, "memory"_a, "flags"_a, "Scan sequence of bytes");
m.def("LoadDatabaseA", DIE::LoadDatabaseA, "database"_a, "Load signature database");
}