SimpleYggGen.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Спасибо PurpleI2P Project за активное содействие в написании этого кода.
  3. * notabug.org/acetone/SimpleYggGen-CPP
  4. *
  5. * acetone (c) GPLv3
  6. *
  7. */
  8. #include "SimpleYggGen.hpp"
  9. #include "ncurses/ncurses-interface.h"
  10. #include <mutex>
  11. #include <unistd.h>
  12. // extern OptionBox getOption(void);
  13. static unsigned long long foundAddreses = 0;
  14. //////////////////////////////////////////////////begin Заставка и прочая вода
  15. const char randomtable[90] = {
  16. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
  17. 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  18. 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
  19. 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  20. 'Y', 'Z', '!', '@', '(', ')', '/', '-', '#', '+', '$', '%', '^', '&', '*',
  21. '`', '~', '>', '<', '?', '{', '}', '[', ']', ';', ':', '_', '=', '|', '\''};
  22. static inline std::string getrandom(int entropy, unsigned int size_of_line) {
  23. std::string random_value;
  24. while (random_value.size() < size_of_line) {
  25. random_value += randomtable[(std::rand() % entropy)];
  26. }
  27. random_value.shrink_to_fit();
  28. return random_value;
  29. }
  30. #ifdef __linux__
  31. constexpr const char *RST = "\x1B[0m";
  32. static inline std::string getRandomColor() {
  33. #ifndef __linux__
  34. return "";
  35. #endif
  36. // unsigned short Background_color = (std::rand() % 4 + 41);//41 -> red
  37. unsigned short Foreground_color =
  38. (std::rand() % 3 + 32); // 30-> black 37-> white; 31 = red
  39. // if(Background_color - 10 == Foreground_color) Background_color=4;
  40. auto str = std::ostringstream();
  41. str << "\033[1;4;46;"; // bold, underline, cyan background
  42. str << Foreground_color << "m";
  43. return str.str();
  44. }
  45. #endif
  46. void intro() {
  47. srand(time(NULL));
  48. int rv = 60;
  49. std::cout << getRandomColor() << std::endl
  50. << "| |" << getrandom(2, 44)
  51. << std::endl
  52. << "| " << NAMEPROGRAM << " C++ 1.0-headhunter 2020 |"
  53. << getrandom(rv, 2) << " " << getrandom(rv, 5) << " "
  54. << getrandom(rv, 6) << " " << getrandom(rv, 5) << " "
  55. << getrandom(rv, 2) << std::endl
  56. << "| OpenSSL inside: x25519 -> sha512 |" << getrandom(rv, 2)
  57. << " " << getrandom(rv, 13) << " " << getrandom(rv, 6) << " "
  58. << getrandom(rv, 5) << " " << getrandom(rv, 10) << std::endl
  59. << "| notabug.org/acetone/SimpleYggGen-CPP |" << getrandom(rv, 2)
  60. << " " << getrandom(rv, 5) << " "
  61. << getrandom(rv, 5) << " " << getrandom(rv, 3) << " "
  62. << getrandom(rv, 2) << std::endl
  63. << "| acetone (c) GPLv3 |" << getrandom(rv, 10)
  64. << " " << getrandom(rv, 13) << " " << getrandom(rv, 5) << " "
  65. << getrandom(rv, 6) << " " << getrandom(rv, 2) << std::endl
  66. << "| |" << getrandom(rv, 2)
  67. << " " << getrandom(rv, 5) << " "
  68. << getrandom(rv, 5) << " " << getrandom(rv, 2) << std::endl
  69. << "| " << __DATE__ << " " << __TIME__ << " |"
  70. << getrandom(2, 44) << std::endl
  71. << "Co-authors: " << COAUTHORS << std::endl
  72. #ifdef __linux__
  73. << RST << std::endl;
  74. #else
  75. << std::endl;
  76. #endif
  77. }
  78. // end
  79. static unsigned int maxlones = 0;
  80. static dataKey m_dataKey;
  81. static struct {
  82. bool reg = false;
  83. int threads = -1;
  84. std::string outputpath = "";
  85. std::regex regex;
  86. ProgramMode mode = ProgramMode::search;
  87. unsigned long long limit = -1;
  88. char *searchtextby = nullptr; // from ncurses if set
  89. } options;
  90. static inline bool NotThat(const char *what, const std::regex &reg) {
  91. return std::regex_match(what, reg) == 1 ? false : true;
  92. }
  93. static inline bool NotThat(const char *a, const char *b) {
  94. //while (*b)
  95. // if (*a++ != *b++)
  96. // return true;
  97. //return false;
  98. return strstr(a, b) == 0 ? true : false;
  99. }
  100. void usage(void) {
  101. const constexpr char *help = NAMEPROGRAM
  102. " [text-pattern(for just search by text, like to 200:ffff)] [options]\n"
  103. "-h --help, help menu\n"
  104. "-r --reg, (^2.*ffff.*$) regexp instead just text pattern (e.g. '(one|two).*')\n"
  105. "--threads -t, (default count of system)\n"
  106. "-o --output output file (default keys.txt)\n"
  107. "--usage this menu\n"
  108. "--highhead -H mode of high head...\n"
  109. //"--searchadress -s (default) mode\n"
  110. "--limitfound=n -lN limit found\n"
  111. "--ncurses -n start ncurses interface\n"
  112. //"--prefix -p\n"
  113. "";
  114. puts(help);
  115. }
  116. void parsing(int argc, char **args) {
  117. int option_index;
  118. static struct option long_options[] = {
  119. {"help", no_argument, 0, 'h'},
  120. {"reg", required_argument, 0, 'r'},
  121. {"threads", required_argument, 0, 't'},
  122. {"output", required_argument, 0, 'o'},
  123. {"usage", no_argument, 0, 0},
  124. {"highhead", no_argument, 0, 'H'},
  125. // {"searchadress", no_argument, 0, 's'},
  126. {"limitfound", required_argument, 0, 'l'},
  127. {"ncurses", no_argument, 0, 'n'},
  128. {0, 0, 0, 0}};
  129. int c;
  130. OptionBox ncursesoptions;
  131. auto setRegExp = [](const char*searchbytext){
  132. options.reg = true;
  133. options.searchtextby = new char[MAXBUF];
  134. memcpy(options.searchtextby, searchbytext, strlen(searchbytext) );
  135. std::cout << "RegExp pattern: "<<searchbytext << std::endl;
  136. options.regex = std::regex( options.searchtextby );
  137. };
  138. while ((c = getopt_long(argc, args, "hr:t:o:Hsl:n", long_options,
  139. &option_index)) != -1) {
  140. switch (c) {
  141. case 0:
  142. if (std::string(long_options[option_index].name) ==
  143. std::string("usage")) {
  144. usage();
  145. exit(1);
  146. }
  147. case 'n':
  148. ncursesoptions = getOption();
  149. switch (ncursesoptions.engine) {
  150. case HighHead:
  151. options.mode = ProgramMode::high;
  152. options.outputpath = defaultHighSearchFileName;
  153. break;
  154. case RegExp:
  155. options.reg = true;
  156. setRegExp(ncursesoptions.searchtext);
  157. //goto REGEXP;
  158. break;
  159. case Exit:
  160. exit(0);
  161. break;
  162. default:
  163. options.searchtextby = new char[MAXBUF];
  164. memcpy(options.searchtextby, ncursesoptions.searchtext, MAXBUF);
  165. }
  166. break;
  167. case 'l':
  168. options.limit = atoi(optarg);
  169. break;
  170. case 'H':
  171. options.mode = ProgramMode::high;
  172. options.outputpath = defaultHighSearchFileName;
  173. break;
  174. /*case 's':
  175. options.mode = ProgramMode::search;
  176. options.outputpath = defaultSearchFileName;
  177. break;*/
  178. case 'h':
  179. usage();
  180. exit(0);
  181. break;
  182. case 'r':
  183. setRegExp(optarg);
  184. break;
  185. case 't':
  186. options.threads = atoi(optarg);
  187. break;
  188. case 'o':
  189. options.outputpath = optarg;
  190. break;
  191. case '?':
  192. std::cerr << "Undefined argument" << std::endl;
  193. default:
  194. std::cerr << args[0] << " --usage / --help" << std::endl;
  195. exit(1);
  196. break;
  197. }
  198. }
  199. }
  200. BoxKeys getKeyPair(void) {
  201. BoxKeys keys;
  202. size_t lenpub = KEYSIZE;
  203. size_t lenpriv = KEYSIZE;
  204. EVP_PKEY_CTX *Ctx;
  205. EVP_PKEY *Pkey = nullptr;
  206. Ctx = EVP_PKEY_CTX_new_id(NID_X25519, NULL);
  207. EVP_PKEY_keygen_init(Ctx);
  208. EVP_PKEY_keygen(Ctx, &Pkey);
  209. EVP_PKEY_get_raw_public_key(Pkey, keys.PublicKey, &lenpub);
  210. EVP_PKEY_get_raw_private_key(Pkey, keys.PrivateKey, &lenpriv);
  211. EVP_PKEY_CTX_free(Ctx);
  212. EVP_PKEY_free(Pkey);
  213. return keys;
  214. }
  215. void getSHA512(void *data, unsigned char *hash) {
  216. SHA512_CTX sha512;
  217. SHA512_Init(&sha512);
  218. SHA512_Update(&sha512, data, KEYSIZE);
  219. SHA512_Final(hash, &sha512);
  220. }
  221. void convertSHA512ToSum(unsigned char hash[SHA512_DIGEST_LENGTH],
  222. char outputBuffer[128]) {
  223. for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) {
  224. sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
  225. }
  226. }
  227. char *convertSHA512ToIPv6(unsigned char hash[SHA512_DIGEST_LENGTH]) {
  228. // char hash[128];
  229. // convertSHA512ToSum(h, hash);
  230. unsigned char byte;
  231. bool done=false;
  232. unsigned char lOnes = 0;
  233. unsigned char nBits = 0;
  234. unsigned char temp[SHA512_DIGEST_LENGTH];
  235. memset(temp, 0, sizeof(temp));
  236. int z = 0;
  237. std::vector<std::bitset<8>> bytes;
  238. for (auto i = 0; i < SHA512_DIGEST_LENGTH; i++)
  239. bytes.push_back(hash[i]);
  240. for (auto bits : bytes) {
  241. for (int i = 7; i >= 0; i--) {
  242. if (bits[i] == 1 && !done) {
  243. lOnes++;
  244. continue;
  245. }
  246. if (!done && bits[i] == 0) {
  247. done = true;
  248. continue;
  249. }
  250. byte = (byte << 1) | (bits[i] > 0 ? 1 : 0);
  251. nBits++;
  252. if (nBits >= 8) {
  253. nBits = 0;
  254. temp[z++] = byte;
  255. // std::cout << "val:" << int(temp[i-1]) << std::endl;
  256. }
  257. }
  258. }
  259. struct in6_addr tmpAdr;
  260. tmpAdr.s6_addr[0] = 0x02;
  261. tmpAdr.s6_addr[1] = lOnes;
  262. for (int i = 2; i < 16; i++)
  263. tmpAdr.s6_addr[i] = temp[i - 2];
  264. char *addr = (char *)calloc(INET6_ADDRSTRLEN, sizeof(char));
  265. inet_ntop(AF_INET6, &tmpAdr, addr, INET6_ADDRSTRLEN);
  266. if(lOnes > maxlones) maxlones=lOnes;
  267. return addr;
  268. }
  269. std::mutex m_writeMutex;
  270. static inline void addKeyPair() {
  271. std::lock_guard<std::mutex> guard(m_writeMutex);
  272. std::ofstream f(options.outputpath, std::ofstream::out | std::ofstream::app);
  273. if (f) {
  274. f << "Your keys: " << std::endl;
  275. f << "Public Key: ";
  276. for(int i = 0; i < 32; ++i)
  277. {
  278. f << std::setw(2) << std::setfill('0') << std::hex << (int)m_dataKey.pk[i];
  279. }
  280. f << std::endl;
  281. f << "Secret key: ";
  282. for(int i = 0; i < KEYSIZE; ++i)
  283. {
  284. f << std::setw(2) << std::setfill('0') << std::hex << (int)m_dataKey.sk[i];
  285. }
  286. f << std::endl;
  287. f << "IPv6: " <<m_dataKey.ip << std::endl;
  288. } else
  289. std::cout << "Can't create/reopen file " << options.outputpath << std::endl;
  290. }
  291. static inline void miner(const char *prefix) {
  292. if (options.reg && prefix[0] != '^' && options.mode != ProgramMode::high) {
  293. std::cerr << "WARNING: "
  294. << "IF YOU DONT KNOW REGEXP PLEASE SEE IT -> https://regexr.com/"
  295. << std::endl << prefix << std::endl;
  296. sleep(15); // magic number
  297. }
  298. auto clearconsole = [](int defsleep = 1) {
  299. #ifndef __linux__
  300. system("clear||cls");
  301. #else
  302. std::cout << "\033[2J\033[1;1H";
  303. #endif
  304. intro();
  305. std::cout << getRandomColor();
  306. std::cout << "\b\b\b..." << std::flush;
  307. usleep(defsleep);
  308. std::cout << "\b\b\b.U." << std::flush;
  309. usleep(defsleep);
  310. std::cout << "\b\b\b..U" << std::flush;
  311. usleep(defsleep);
  312. std::cout << "\b\b\bvvU" << std::flush;
  313. usleep(defsleep);
  314. std::cout << "\b\b\bUvU" << std::flush;
  315. std::cout << "\b\b\b|" << std::flush;
  316. };
  317. while (options.limit < 0 || options.limit > foundAddreses) {
  318. std::cout << "\b/" << std::flush;
  319. std::cout << "\b\\" << std::flush;
  320. auto myKeys = getKeyPair();
  321. unsigned char hash[SHA512_DIGEST_LENGTH];
  322. getSHA512(myKeys.PublicKey, hash);
  323. int o=maxlones;
  324. auto ipv6 = convertSHA512ToIPv6(hash);
  325. //printf("%s\n", ipv6);
  326. if (options.mode == ProgramMode::high) {
  327. //std::cout << "addr: "
  328. // << "(" << maxlones << ") " << ipv6 << std::endl;
  329. if (maxlones > o) {
  330. clearconsole();
  331. std::cout << "Found new max high-addr: "
  332. << "(" << maxlones << ") " << ipv6 << std::endl;
  333. ADDKEYS(m_dataKey, myKeys, ipv6);//To inline?
  334. addKeyPair();
  335. }
  336. } else {
  337. //ipv6 = convertSHA512ToIPv6(hash);
  338. if ((options.reg ? !NotThat(ipv6, options.regex)
  339. : !NotThat(ipv6, prefix))) {
  340. clearconsole();
  341. std::cout << "Address found: "
  342. << "(" << ++foundAddreses << ") " << ipv6;
  343. std::cout << std::flush;
  344. ADDKEYS(m_dataKey, myKeys, ipv6);
  345. addKeyPair();
  346. // delete newKey.sk; // not need. not-heap..
  347. }
  348. }
  349. free(ipv6);
  350. }
  351. }
  352. int main(int argc, char **argv) {
  353. intro();
  354. if (argc < 2) {
  355. usage();
  356. return 0;
  357. }
  358. options.outputpath = defaultSearchFileName;
  359. parsing(argc, argv);
  360. if (options.threads < 0)
  361. options.threads = std::thread::hardware_concurrency();
  362. ;
  363. std::vector<std::thread> threads(options.threads);
  364. for (unsigned int j = options.threads; j--;) {
  365. threads[j] = std::thread(
  366. static_cast<void (*)(const char *)>(miner),
  367. options.searchtextby == nullptr ? argv[1] : options.searchtextby);
  368. }
  369. for (unsigned int j = 0; j < (unsigned int)options.threads; j++)
  370. threads[j].join();
  371. }