forked from DataDog/libddwaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
216 lines (185 loc) · 5.95 KB
/
example.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "ddwaf.h"
#include <iostream>
#include <yaml-cpp/yaml.h>
#define LONG_TIME 1000000
namespace YAML {
template <> struct as_if<ddwaf_object, void> {
explicit as_if(const Node &node_) : node(node_) {}
static ddwaf_object yaml_to_object_helper(const Node &node)
{
ddwaf_object arg;
switch (node.Type()) {
case NodeType::Sequence:
ddwaf_object_array(&arg);
break;
case NodeType::Map:
ddwaf_object_map(&arg);
break;
case NodeType::Scalar:
ddwaf_object_string(&arg, node.Scalar().c_str());
break;
case NodeType::Null:
ddwaf_object_null(&arg);
break;
case NodeType::Undefined:
default:
ddwaf_object_invalid(&arg);
break;
}
return arg;
}
ddwaf_object operator()() const
{
std::list<std::tuple<ddwaf_object &, YAML::Node, YAML::Node::const_iterator>> stack;
ddwaf_object root = yaml_to_object_helper(node);
if (root.type == DDWAF_OBJ_MAP || root.type == DDWAF_OBJ_ARRAY) {
stack.emplace_back(root, node, node.begin());
}
while (!stack.empty()) {
auto current_depth = stack.size();
auto &[parent_obj, parent_node, it] = stack.back();
for (; it != parent_node.end(); ++it) {
YAML::Node child_node = parent_node.IsMap() ? it->second : *it;
auto child_obj = yaml_to_object_helper(child_node);
if (parent_obj.type == DDWAF_OBJ_MAP) {
auto key = it->first.as<std::string>();
ddwaf_object_map_add(&parent_obj, key.c_str(), &child_obj);
} else if (parent_obj.type == DDWAF_OBJ_ARRAY) {
ddwaf_object_array_add(&parent_obj, &child_obj);
}
if (child_obj.type == DDWAF_OBJ_MAP || child_obj.type == DDWAF_OBJ_ARRAY) {
auto &child_ptr = parent_obj.array[parent_obj.nbEntries - 1];
stack.emplace_back(child_ptr, child_node, child_node.begin());
++it;
break;
}
}
if (current_depth == stack.size()) {
stack.pop_back();
}
}
return root;
}
const Node &node;
};
} // namespace YAML
namespace {
YAML::Node object_to_yaml_helper(const ddwaf_object &obj)
{
YAML::Node output;
switch (obj.type) {
case DDWAF_OBJ_BOOL:
output = obj.boolean;
break;
case DDWAF_OBJ_SIGNED:
output = obj.intValue;
break;
case DDWAF_OBJ_UNSIGNED:
output = obj.uintValue;
break;
case DDWAF_OBJ_FLOAT:
output = obj.f64;
break;
case DDWAF_OBJ_STRING:
output = std::string{obj.stringValue, obj.nbEntries};
break;
case DDWAF_OBJ_MAP:
output = YAML::Load("{}");
break;
case DDWAF_OBJ_ARRAY:
output = YAML::Load("[]");
break;
case DDWAF_OBJ_INVALID:
case DDWAF_OBJ_NULL:
output = YAML::Null;
break;
};
return output;
}
} // namespace
YAML::Node object_to_yaml(const ddwaf_object &obj)
{
std::list<std::tuple<const ddwaf_object &, YAML::Node, std::size_t>> stack;
YAML::Node root = object_to_yaml_helper(obj);
if (obj.type == DDWAF_OBJ_MAP || obj.type == DDWAF_OBJ_ARRAY) {
stack.emplace_back(obj, root, 0);
}
while (!stack.empty()) {
auto current_depth = stack.size();
auto &[parent_obj, parent_node, index] = stack.back();
for (; index < parent_obj.nbEntries; ++index) {
auto &child_obj = parent_obj.array[index];
auto child_node = object_to_yaml_helper(child_obj);
if (parent_obj.type == DDWAF_OBJ_MAP) {
std::string key{child_obj.parameterName, child_obj.parameterNameLength};
parent_node[key] = child_node;
} else if (parent_obj.type == DDWAF_OBJ_ARRAY) {
parent_node.push_back(child_node);
}
if (child_obj.type == DDWAF_OBJ_MAP || child_obj.type == DDWAF_OBJ_ARRAY) {
stack.emplace_back(child_obj, child_node, 0);
++index;
break;
}
}
if (current_depth == stack.size()) {
stack.pop_back();
}
}
return root;
}
constexpr std::string_view waf_rule = R"(
version: "2.1"
rules:
- id: "1"
name: rule 1
tags:
type: flow1
category: test
conditions:
- operator: match_regex
parameters:
inputs:
- address: arg1
regex: .*
- operator: match_regex
parameters:
inputs:
- address: arg2
regex: .*
on_match: [ block ]
)";
int main()
{
YAML::Node doc = YAML::Load(waf_rule.data());
auto rule = doc.as<ddwaf_object>(); //= convert_yaml_to_args(doc);
ddwaf_handle handle = ddwaf_init(&rule, nullptr, nullptr);
ddwaf_object_free(&rule);
if (handle == nullptr) {
return EXIT_FAILURE;
}
ddwaf_context context = ddwaf_context_init(handle);
if (context == nullptr) {
ddwaf_destroy(handle);
return EXIT_FAILURE;
}
ddwaf_object root, tmp;
ddwaf_object_map(&root);
ddwaf_object_map_add(&root, "arg1", ddwaf_object_string(&tmp, "string 1"));
ddwaf_object_map_add(&root, "arg2", ddwaf_object_string(&tmp, "string 2"));
ddwaf_result ret;
auto code = ddwaf_run(context, &root, nullptr, &ret, LONG_TIME);
std::cout << "Output second run: " << code << '\n';
if (code == DDWAF_MATCH) {
YAML::Emitter out(std::cout);
out.SetIndent(2);
out.SetMapFormat(YAML::Block);
out.SetSeqFormat(YAML::Block);
out << object_to_yaml(ret.events);
out << object_to_yaml(ret.actions);
}
ddwaf_result_free(&ret);
ddwaf_context_destroy(context);
ddwaf_destroy(handle);
return EXIT_SUCCESS;
}