-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathct.cpp
173 lines (143 loc) · 4.15 KB
/
ct.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
#include <libssh/libssh.h>
#include <string>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <functional>
extern "C" {
#include <curl/curl.h>
}
using namespace std;
/*
CT for trex-pam OTP PAM module
TODOs:
-add RAII everywhere
*/
class sshraii {
string host;
int port, log;
ssh_session session;
public:
sshraii(string hostp = "localhost",
int portp = 2222,
int logp = SSH_LOG_NOLOG)
:host{hostp},
port{portp},
log{logp}
{
session = ssh_new();
if (!session)
throw runtime_error("failed to start a new ssh session");
ssh_options_set(session, SSH_OPTIONS_HOST, hostp.c_str());
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &log);
ssh_options_set(session, SSH_OPTIONS_PORT, &port);
if (ssh_connect(session) != SSH_OK)
throw runtime_error("failed to connect");
}
~sshraii()
{
ssh_disconnect(session);
ssh_free(session);
}
operator ssh_session() {return session;};
};
//TODO: copied from UTs, unify.
class globalRaii {
public:
globalRaii() { curl_global_init(CURL_GLOBAL_ALL); }
~globalRaii() { curl_global_cleanup(); }
};
class curlraii {
CURL *curl;
public:
curlraii(): curl{curl_easy_init()}{}
~curlraii() { curl_easy_cleanup(curl); }
operator CURL*(){return curl;}
};
class poster {
private:
public:
curlraii curl;
stringstream ss;
poster(string formdata="", string host = "https://trex-security.com:1720/webdemo") : curl{} {
ss.clear();
if (!curl)
throw runtime_error{"can't init curl\n"s};
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ss);
curl_easy_setopt(curl, CURLOPT_URL, host.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_REFERER, "https://demo.trex-security.com/");
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, formdata.c_str());
auto res{curl_easy_perform(curl)};
}
static size_t write_data(void *buffer, size_t size, size_t nmemb,
void *userp) {
auto streamPtr{static_cast<stringstream *>(userp)};
*streamPtr << string{static_cast<char *>(buffer)};
return nmemb;
}
string get() { return ss.str(); }
};
void checkresponse(function<string(string)> callback)
{
auto my_ssh{sshraii()};
auto prompt = [](auto& my_ssh){
ssh_userauth_kbdint(my_ssh, "docker", nullptr);
return ssh_userauth_kbdint_getprompt(my_ssh, 0, nullptr);
}(my_ssh);
auto chal = [](auto& my_ssh, string prompt){
auto chal{prompt.substr(0,prompt.find("\n\nFor QR"))};
if (chal=="")
throw runtime_error("invalid challenge");
return chal;
}(my_ssh, prompt);
auto resp = [](string chal, auto& callback){
string resp{callback(chal)};
return resp.substr(resp.find(":")+2, resp.npos);
}(chal, callback);
if (ssh_userauth_kbdint_setanswer(my_ssh, 0, resp.c_str()) < 0)
throw runtime_error("failed to set answear");
auto sshlambda{[&my_ssh](){return ssh_userauth_kbdint(my_ssh, "docker", nullptr);}};
int retries{5};
auto rc{sshlambda()};
for (int i{0}; rc == SSH_AUTH_INFO && i < retries; i++)
{
rc = sshlambda();
}
if (rc != SSH_AUTH_SUCCESS)
{
stringstream ss{};
ss << " err: " << rc << " returned";
throw runtime_error("failed to connect with response: " +
resp +
string{" | "} + string{ssh_get_error(my_ssh)} +
string{" | "} + ss.str());
} else {
cout << "success" << endl;
}
}
int main()
{
globalRaii init{};
cout << "test good response\n";
checkresponse([](string chal){return poster(chal).get();});
cout << "test bad response\n";
string unexpected{"bad response did not fail"};
try
{
checkresponse([](string chal){return string{"bad : response"};});
throw runtime_error(unexpected);
}
catch (runtime_error e)
{
if (e.what() == unexpected)
throw e;
}
cout << "success\n";
//TODO: test grabbing QR via HTTPS with good credentials
//TODO: test that QR == challenge
//TODO: test grabbing QR via HTTP fails
//TODO: test grabbing QR via HTTPS with bad credentials fails
}