-
Notifications
You must be signed in to change notification settings - Fork 42
/
py-untwister.cpp
179 lines (146 loc) · 5.25 KB
/
py-untwister.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
/*
Copyright Bishop Fox, 2014
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Notes: Requires Boost C++ libraries
*/
#include <python2.7/Python.h>
#include <boost/python.hpp>
#include <execinfo.h>
#include <signal.h>
#include <iostream>
#include "Untwister.h"
using namespace boost::python;
static const unsigned int TRACE_SIZE = 10;
/* Segfault handler - for debugging only */
void handler(int sig)
{
void *trace[TRACE_SIZE];
size_t size = backtrace(trace, TRACE_SIZE);
std::cerr << "[!] SIGSEGV: " << sig << std::endl;
backtrace_symbols_fd(trace, size, 2);
exit(1);
}
/* Python __init__ function */
void PythonInit()
{
signal(SIGSEGV, handler);
signal(SIGABRT, handler);
if(!Py_IsInitialized())
{
Py_Initialize();
PyEval_InitThreads();
}
}
/* Find a seed */
list BruteforceAttack(std::string prng, list observations, unsigned int threads, float minimumConfidence,
uint32_t lowerBoundSeed, uint32_t upperBoundSeed, uint32_t depth)
{
Untwister *untwister = new Untwister(len(observations));
if(!untwister->isSupportedPRNG(prng))
{
/* Raise Python Exception */
PyErr_SetString(PyExc_ValueError, "Unsupported PRNG");
throw error_already_set();
}
untwister->setPRNG(prng);
untwister->setThreads(threads);
untwister->setDepth(depth);
untwister->setMinConfidence(minimumConfidence);
/* Convert Python list object to observedOutputs's std::vector<uint32_t> */
for(int index = 0; index < len(observations); ++index)
{
uint32_t data = extract<uint32_t>(observations[index]);
untwister->getObservedOutputs()->at(index) = data;
}
/* Suspend Python's thread, so we can use native C++ threads */
PyThreadState *pyThreadState = PyEval_SaveThread();
auto results = untwister->bruteforce(lowerBoundSeed, upperBoundSeed);
PyEval_RestoreThread(pyThreadState);
/* Covert answers to python list of tuples */
list pyResults;
for(unsigned int index = 0; index < results.size(); ++index)
{
tuple pySeed = make_tuple(results[index].first, results[index].second);
pyResults.append(pySeed);
}
delete untwister;
return pyResults;
}
tuple InferStateAttack(std::string prng, list observations, float minimumConfidence)
{
Untwister *untwister = new Untwister(len(observations));
if(!untwister->isSupportedPRNG(prng))
{
/* Raise Python Exception */
PyErr_SetString(PyExc_ValueError, "Unsupported PRNG");
throw error_already_set();
}
untwister->setPRNG(prng);
if(untwister->getStateSize() < (uint32_t) len(observations))
{
PyErr_SetString(PyExc_ValueError, "Cannot infer state, too few observations");
throw error_already_set();
}
untwister->setMinConfidence(minimumConfidence);
/* Convert Python list object to observedOutputs's std::vector<uint32_t> */
for(int index = 0; index < len(observations); ++index)
{
uint32_t data = extract<uint32_t>(observations[index]);
untwister->getObservedOutputs()->at(index) = data;
}
auto state = untwister->inferState();
list pyState;
for(unsigned int index = 0; index < state.first.size(); ++index)
{
pyState.append(state.first[index]);
}
delete untwister;
return make_tuple(pyState, state.second);
}
/* List all supported PRNGs */
list Prngs()
{
Untwister *untwister = new Untwister();
std::vector<std::string> names = untwister->getSupportedPRNGs();
list pyPRNGs;
for(unsigned int index = 0; index < names.size(); ++index)
{
pyPRNGs.append(names[index]);
}
delete untwister;
return pyPRNGs;
}
/* Python interface */
BOOST_PYTHON_MODULE(untwister) {
def("untwister", PythonInit);
scope current;
current.attr("__doc__") = "Multi-threaded seed recovery tool for common PRNGs";
current.attr("MT19937") = "mt19937";
current.attr("GLIBC") = "glibc-rand";
current.attr("RUBY") = "ruby-rand";
unsigned int threads = std::thread::hardware_concurrency();
current.attr("THREADS") = threads;
def("get_supported_prngs", Prngs, "\n Get a list of supported PRNGs");
def(
"bruteforce",
BruteforceAttack,
(arg("prng"), arg("observations"), arg("threads") = threads, arg("confidence") = 100.0, \
arg("lower") = 0, arg("upper") = UINT_MAX, arg("depth") = 1000),
"\nThis function attempts to recover a seed using bruteforce for any supported PRNG"
);
def(
"infer_state",
InferStateAttack,
(arg("prng"), arg("observations"), arg("confidence") = 100.0),
"\nThis function attempts to infer the internal state of the PRNG"
);
}