-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
365 lines (316 loc) · 11.7 KB
/
Copy pathmain.cpp
File metadata and controls
365 lines (316 loc) · 11.7 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* isweb++
*
* Created by b1twsi3
*/
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <pthread.h>
#include <functional>
#include <memory>
#include <atomic>
#include <mutex>
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using namespace std;
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
using boost::asio::ip::tcp;
#define NUM_THREADS 100
std::mutex req_mutex;
struct Domain
{
string host;
string port;
};
class client
{
public:
client(boost::asio::io_service &io_service,
const std::string &server, const std::string &path)
: resolver_(io_service),
socket_(io_service)
{
server_ = server;
// Form the request. We specify the "Connection: close" header so that the
// server will close the socket after transmitting the response. This will
// allow us to treat all data up until the EOF as the content.
std::ostream request_stream(&request_);
request_stream << "HEAD " << path << " HTTP/1.0\r\n";
request_stream << "Host: " << server << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
// Start an asynchronous resolve to translate the server and service names
// into a list of endpoints.
// cout << "Checking: " << server << endl;
// tcp::resolver::query query(server, "http");
tcp::resolver::query query(server, "80", boost::asio::ip::resolver_query_base::numeric_service);
resolver_.async_resolve(query,
boost::bind(&client::handle_resolve, this,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
}
private:
void handle_resolve(const boost::system::error_code &err,
tcp::resolver::iterator endpoint_iterator)
{
if (!err)
{
// Attempt a connection to the first endpoint in the list. Each endpoint
// will be tried until we successfully establish a connection.
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
boost::bind(&client::handle_connect, this,
boost::asio::placeholders::error, ++endpoint_iterator));
}
else
{
// std::cout << "Error4: " << err.message() << "\n";
}
}
void handle_connect(const boost::system::error_code &err,
tcp::resolver::iterator endpoint_iterator)
{
if (!err)
{
// The connection was successful. Send the request.
boost::asio::async_write(socket_, request_,
boost::bind(&client::handle_write_request, this,
boost::asio::placeholders::error));
}
// Check if the connect operation failed before the deadline expired.
// else if (err)
// {
// // std::cout << "Connect error: " << err.message() << "\n";
// // We need to close the socket used in the previous connection attempt
// // before starting a new one.
// socket_.close();
// // Try the next available endpoint.
// // handle_resolve(err, ++endpoint_iterator);
// }
else if (endpoint_iterator != tcp::resolver::iterator())
{
// The connection failed. Try the next endpoint in the list.
socket_.close();
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint,
boost::bind(&client::handle_connect, this,
boost::asio::placeholders::error, ++endpoint_iterator));
}
else
{
// Check HTTPS
try
{
boost::asio::io_context ioc;
// Look up the domain name
// These objects perform our I/O
tcp::resolver resolver(ioc);
beast::tcp_stream stream(ioc);
auto const results = resolver.resolve(this->server_, "443");
// Make the connection on the IP address we get from a lookup
stream.connect(results);
// Set the logical operation timer to 100 milliseconds.
stream.expires_after(std::chrono::milliseconds(100));
// std::cout << "Error3: " << err.message() << "\n";
// Gracefully close the socket
beast::error_code ec;
stream.socket().shutdown(tcp::socket::shutdown_both, ec);
if (ec && ec != beast::errc::not_connected)
{
throw beast::system_error{ec};
}
else
{
// Set up an HTTP GET request message
auto const target = "/";
int version = 11;
http::request<http::string_body> req{http::verb::head, target, version};
req.set(http::field::host, this->server_);
// req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
// Send the HTTP request to the remote host
http::write(stream, req);
// // NULL buffer is used for reading and must be persisted
beast::flat_buffer buffer;
// // Declare a container to hold the response
http::response<http::dynamic_body> res;
// // Receive the HTTP response
http::read(stream, buffer, res);
// // Write the message to standard out
std::cout << res.has_content_length() << std::endl;
// cout << res.has_content_length() << endl;
http::status status_code = res.result();
// cout << "[+] https://" << this->server_ << endl;
req_mutex.lock();
printf("[+] https://%s\n", this->server_.c_str());
req_mutex.unlock();
}
// If we get here then the connection is closed gracefully
stream.close();
}
catch (std::exception const &e)
{
// printf("[-] \t%s - %s\n", this->server_.c_str(), e.what());
}
}
}
void handle_write_request(const boost::system::error_code &err)
{
if (!err)
{
// Read the response status line.
boost::asio::async_read_until(socket_, response_, "\r\n",
boost::bind(&client::handle_read_status_line, this,
boost::asio::placeholders::error));
}
// else
// {
// std::cout << "Error2: " << err.message() << "\n";
// }
}
void handle_read_status_line(const boost::system::error_code &err)
{
if (!err)
{
// Check that response is OK.
std::istream response_stream(&response_);
std::string http_version;
response_stream >> http_version;
unsigned int status_code;
response_stream >> status_code;
std::string status_message;
std::getline(response_stream, status_message);
req_mutex.lock();
cout << "[+] http://" << this->server_ << " - Status Code: " << status_code << endl;
req_mutex.unlock();
// cout<< "status code: " << status_code << endl;
}
// else
// {
// std::cout << "Error1: " << err << endl;
// }
}
tcp::resolver resolver_;
tcp::socket socket_;
boost::asio::streambuf request_;
boost::asio::streambuf response_;
string server_;
};
vector<Domain> split_vec(vector<Domain> domains, size_t start, size_t size, size_t vec_size)
{
vector<Domain> temp;
for (size_t i = start; i <= size; i++)
{
if (i < vec_size)
{
// cout << "start: " << start << " " << domains[i].host << endl;
temp.push_back(domains[i]);
}
}
// cout << "temp size: " << temp.size() << endl;
return temp;
}
std::atomic <unsigned int> g_thread(0);
void *is_web(void *d1)
{
vector<Domain> *domains_vector = (vector<Domain> *)d1;
// printf("vec size: %d\n", domains_vector->size());
for (auto d : *domains_vector)
{
// cout << "Printing Start Host: " << d.host << endl;
// std::this_thread::sleep_for (std::chrono::seconds(3));
// cout << "Printing End Host: " << d.host << endl;
string sub_host = d.host;
string port = d.port;
// cout << "test: " << sub_host << endl;
auto const target = "/";
int version = 11;
try
{
// The io_context is required for all I/O
boost::asio::io_service io_service;
client c(
io_service,
sub_host,
target);
// Run
// req_mutex.lock();
io_service.run();
// req_mutex.unlock();
}
catch (std::exception const &e)
{
// printf("[-] \t%s - %s\n", sub_host.c_str(), e.what());
}
// pthread_exit(NULL);
g_thread++;
}
}
int main(int argc, char **argv)
{
// Get the host/domain list
ofstream host_file;
// Open the File
std::ifstream in(argv[1]);
string sub_host;
vector<Domain> domains;
pthread_t threads[NUM_THREADS];
int rc;
// Read the next line from File untill it reaches the end.
while (getline(in, sub_host))
{
// Line contains string of length > 0 then save it in vector
if (sub_host.size() > 0)
{
struct Domain d1;
d1.host = sub_host;
d1.port = "443";
domains.push_back(d1);
}
}
//Close The File
in.close();
// Chunk the domains/hosts list
int chunk_size = domains.size() / NUM_THREADS;
int current_index = 0;
int chunk_end = chunk_size;
vector <vector<Domain>> m_vec;
for (size_t i = 0; i < NUM_THREADS; i++)
{
// printf("%d: %d - %d\n",i,current_index,chunk_end-1);
current_index += chunk_size;
chunk_end += chunk_size;
vector<Domain> temp;
temp = split_vec(domains, current_index, chunk_end - 1, domains.size());
m_vec.push_back(temp);
// printf("joinable? %s\n",chunk.joinable()?"true":"false");
// cout << "Testing start.." << endl;
// printf("joinable? %s\n",chunk.joinable()?"true":"false");
// std::this_thread::sleep_for (std::chrono::seconds(3));
// cout << "Testing End.." << endl;
// rc = pthread_create(&threads[i], NULL, is_web, &temp);
// pthread_join(threads[i], NULL);
// chunk.join();
}
for (size_t i = 0; i < m_vec.size(); i++)
{
std::thread chunk(is_web,&m_vec[i]);
chunk.detach();
}
while(g_thread != domains.size()){
g_thread.load();
}
// pthread_exit(NULL);
return EXIT_SUCCESS;
}