SimpleYggGen.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #endif
  33. static inline std::string getRandomColor() {
  34. #ifndef __linux__
  35. return "";
  36. #endif
  37. // unsigned short Background_color = (std::rand() % 4 + 41);//41 -> red
  38. unsigned short Foreground_color =
  39. (std::rand() % 3 + 32); // 30-> black 37-> white; 31 = red
  40. // if(Background_color - 10 == Foreground_color) Background_color=4;
  41. auto str = std::ostringstream();
  42. str << "\033[1;4;46;"; // bold, underline, cyan background
  43. str << Foreground_color << "m";
  44. return str.str();
  45. }
  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. inline static void hexKeyToIpv6(const char* arg){
  101. unsigned char pubkey[SHA512_DIGEST_LENGTH];
  102. for(int i = 0; i < strlen(arg) / 2; i++) {
  103. sscanf(arg + i * 2, "%02hhX", pubkey + i);
  104. }
  105. unsigned char hash[SHA512_DIGEST_LENGTH];
  106. getSHA512(pubkey, hash);
  107. std::cout << "IPv6: " << convertSHA512ToIPv6(hash) << std::endl;
  108. exit(0);
  109. }
  110. void usage(void) {
  111. const constexpr char *help = NAMEPROGRAM
  112. " [text-pattern(for just search by text, like to 200:ffff)] [options]\n"
  113. "-h --help, help menu\n"
  114. "-r --reg, (^2.*ffff.*$) regexp instead just text pattern (e.g. '(one|two).*')\n"
  115. "--threads -t, (default count of system)\n"
  116. "-o --output output file (default keys.txt)\n"
  117. "--usage this menu\n"
  118. "--highhead -H mode of high head...\n"
  119. //"--searchadress -s (default) mode\n"
  120. "--limitfound=n -lN limit found\n"
  121. "--ncurses -n start ncurses interface\n"
  122. //"--prefix -p\n"
  123. "";
  124. puts(help);
  125. }
  126. void parsing(int argc, char **args) {
  127. int option_index;
  128. static struct option long_options[] = {
  129. {"help", no_argument, 0, 'h'},
  130. {"reg", required_argument, 0, 'r'},
  131. {"threads", required_argument, 0, 't'},
  132. {"output", required_argument, 0, 'o'},
  133. {"usage", no_argument, 0, 0},
  134. {"highhead", no_argument, 0, 'H'},
  135. // {"searchadress", no_argument, 0, 's'},
  136. {"limitfound", required_argument, 0, 'l'},
  137. {"ncurses", no_argument, 0, 'n'},
  138. {"keytoipv6", no_argument, 0, 'k'},
  139. {0, 0, 0, 0}};
  140. int c;
  141. OptionBox ncursesoptions;
  142. auto setRegExp = [](const char*searchbytext){
  143. options.reg = true;
  144. options.searchtextby = new char[MAXBUF];
  145. memcpy(options.searchtextby, searchbytext, strlen(searchbytext) );
  146. std::cout << "RegExp pattern: "<<searchbytext << std::endl;
  147. options.regex = std::regex( options.searchtextby );
  148. };
  149. while ((c = getopt_long(argc, args, "hr:t:o:Hsl:nk:", long_options,
  150. &option_index)) != -1) {
  151. switch (c) {
  152. case 0:
  153. if (std::string(long_options[option_index].name) ==
  154. std::string("usage")) {
  155. usage();
  156. exit(1);
  157. }
  158. case 'n':
  159. ncursesoptions = getOption();
  160. switch (ncursesoptions.engine) {
  161. case HighHead:
  162. options.mode = ProgramMode::high;
  163. options.outputpath = defaultHighSearchFileName;
  164. break;
  165. case RegExp:
  166. options.reg = true;
  167. setRegExp(ncursesoptions.searchtext);
  168. //goto REGEXP;
  169. break;
  170. case Exit:
  171. exit(0);
  172. break;
  173. default:
  174. options.searchtextby = new char[MAXBUF];
  175. memcpy(options.searchtextby, ncursesoptions.searchtext, MAXBUF);
  176. }
  177. break;
  178. case 'l':
  179. options.limit = atoi(optarg);
  180. break;
  181. case 'H':
  182. options.mode = ProgramMode::high;
  183. options.outputpath = defaultHighSearchFileName;
  184. break;
  185. /*case 's':
  186. options.mode = ProgramMode::search;
  187. options.outputpath = defaultSearchFileName;
  188. break;*/
  189. case 'h':
  190. usage();
  191. exit(0);
  192. break;
  193. case 'r':
  194. setRegExp(optarg);
  195. break;
  196. case 't':
  197. options.threads = atoi(optarg);
  198. break;
  199. case 'o':
  200. options.outputpath = optarg;
  201. break;
  202. case 'k':
  203. hexKeyToIpv6(optarg);
  204. break;
  205. case '?':
  206. std::cerr << "Undefined argument" << std::endl;
  207. default:
  208. std::cerr << args[0] << " --usage / --help" << std::endl;
  209. exit(1);
  210. break;
  211. }
  212. }
  213. }
  214. BoxKeys getKeyPair(void) {
  215. BoxKeys keys;
  216. size_t lenpub = KEYSIZE;
  217. size_t lenpriv = KEYSIZE;
  218. EVP_PKEY_CTX *Ctx;
  219. EVP_PKEY *Pkey = nullptr;
  220. Ctx = EVP_PKEY_CTX_new_id(NID_X25519, NULL);
  221. EVP_PKEY_keygen_init(Ctx);
  222. EVP_PKEY_keygen(Ctx, &Pkey);
  223. EVP_PKEY_get_raw_public_key(Pkey, keys.PublicKey, &lenpub);
  224. EVP_PKEY_get_raw_private_key(Pkey, keys.PrivateKey, &lenpriv);
  225. EVP_PKEY_CTX_free(Ctx);
  226. EVP_PKEY_free(Pkey);
  227. return keys;
  228. }
  229. void getSHA512(void *data, unsigned char *hash) {
  230. SHA512_CTX sha512;
  231. SHA512_Init(&sha512);
  232. SHA512_Update(&sha512, data, KEYSIZE);
  233. SHA512_Final(hash, &sha512);
  234. }
  235. void convertSHA512ToSum(unsigned char hash[SHA512_DIGEST_LENGTH],
  236. char outputBuffer[128]) {
  237. for (int i = 0; i < SHA512_DIGEST_LENGTH; i++) {
  238. sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
  239. }
  240. }
  241. char *convertSHA512ToIPv6(unsigned char hash[SHA512_DIGEST_LENGTH]) {
  242. // char hash[128];
  243. // convertSHA512ToSum(h, hash);
  244. unsigned char byte;
  245. bool done=false;
  246. unsigned char lOnes = 0;
  247. unsigned char nBits = 0;
  248. unsigned char temp[SHA512_DIGEST_LENGTH];
  249. memset(temp, 0, sizeof(temp));
  250. int z = 0;
  251. std::vector<std::bitset<8>> bytes;
  252. for (auto i = 0; i < SHA512_DIGEST_LENGTH; i++)
  253. bytes.push_back(hash[i]);
  254. for (auto bits : bytes) {
  255. for (int i = 7; i >= 0; i--) {
  256. if (bits[i] == 1 && !done) {
  257. lOnes++;
  258. continue;
  259. }
  260. if (!done && bits[i] == 0) {
  261. done = true;
  262. continue;
  263. }
  264. byte = (byte << 1) | (bits[i] > 0 ? 1 : 0);
  265. nBits++;
  266. if (nBits >= 8) {
  267. nBits = 0;
  268. temp[z++] = byte;
  269. // std::cout << "val:" << int(temp[i-1]) << std::endl;
  270. }
  271. }
  272. }
  273. struct in6_addr tmpAdr;
  274. tmpAdr.s6_addr[0] = 0x02;
  275. tmpAdr.s6_addr[1] = lOnes;
  276. for (int i = 2; i < 16; i++)
  277. tmpAdr.s6_addr[i] = temp[i - 2];
  278. char *addr = (char *)calloc(INET6_ADDRSTRLEN, sizeof(char));
  279. inet_ntop(AF_INET6, &tmpAdr, addr, INET6_ADDRSTRLEN);
  280. if(lOnes > maxlones) maxlones=lOnes;
  281. return addr;
  282. }
  283. std::mutex m_writeMutex;
  284. static inline void addKeyPair() {
  285. std::lock_guard<std::mutex> guard(m_writeMutex);
  286. std::ofstream f(options.outputpath, std::ofstream::out | std::ofstream::app);
  287. if (f) {
  288. f << "Your keys: " << std::endl;
  289. f << "Public Key: ";
  290. for(int i = 0; i < 32; ++i)
  291. {
  292. f << std::setw(2) << std::setfill('0') << std::hex << (int)m_dataKey.pk[i];
  293. }
  294. f << std::endl;
  295. f << "Secret key: ";
  296. for(int i = 0; i < KEYSIZE; ++i)
  297. {
  298. f << std::setw(2) << std::setfill('0') << std::hex << (int)m_dataKey.sk[i];
  299. }
  300. f << std::endl;
  301. f << "IPv6: " <<m_dataKey.ip << std::endl;
  302. } else
  303. std::cout << "Can't create/reopen file " << options.outputpath << std::endl;
  304. }
  305. static inline void miner(const char *prefix) {
  306. if (options.reg && prefix[0] != '^' && options.mode != ProgramMode::high) {
  307. std::cerr << "WARNING: "
  308. << "IF YOU DONT KNOW REGEXP PLEASE SEE IT -> https://regexr.com/"
  309. << std::endl << prefix << std::endl;
  310. sleep(15); // magic number
  311. }
  312. auto clearconsole = [](int defsleep = 1) {
  313. #ifndef __linux__
  314. system("clear||cls");
  315. #else
  316. std::cout << "\033[2J\033[1;1H";
  317. #endif
  318. intro();
  319. std::cout << getRandomColor();
  320. std::cout << "\b\b\b..." << std::flush;
  321. usleep(defsleep);
  322. std::cout << "\b\b\b.U." << std::flush;
  323. usleep(defsleep);
  324. std::cout << "\b\b\b..U" << std::flush;
  325. usleep(defsleep);
  326. std::cout << "\b\b\bvvU" << std::flush;
  327. usleep(defsleep);
  328. std::cout << "\b\b\bUvU" << std::flush;
  329. std::cout << "\b\b\b|" << std::flush;
  330. };
  331. while (options.limit < 0 || options.limit > foundAddreses) {
  332. std::cout << "\b/" << std::flush;
  333. std::cout << "\b\\" << std::flush;
  334. auto myKeys = getKeyPair();
  335. unsigned char hash[SHA512_DIGEST_LENGTH];
  336. getSHA512(myKeys.PublicKey, hash);
  337. int o=maxlones;
  338. auto ipv6 = convertSHA512ToIPv6(hash);
  339. //printf("%s\n", ipv6);
  340. if (options.mode == ProgramMode::high) {
  341. //std::cout << "addr: "
  342. // << "(" << maxlones << ") " << ipv6 << std::endl;
  343. if (maxlones > o) {
  344. clearconsole();
  345. std::cout << "Found new max high-addr: "
  346. << "(" << maxlones << ") " << ipv6 << std::endl;
  347. ADDKEYS(m_dataKey, myKeys, ipv6);//To inline?
  348. addKeyPair();
  349. }
  350. } else {
  351. //ipv6 = convertSHA512ToIPv6(hash);
  352. if ((options.reg ? !NotThat(ipv6, options.regex)
  353. : !NotThat(ipv6, prefix))) {
  354. clearconsole();
  355. std::cout << "Address found: "
  356. << "(" << ++foundAddreses << ") " << ipv6;
  357. std::cout << std::flush;
  358. ADDKEYS(m_dataKey, myKeys, ipv6);
  359. addKeyPair();
  360. // delete newKey.sk; // not need. not-heap..
  361. }
  362. }
  363. free(ipv6);
  364. }
  365. }
  366. int main(int argc, char **argv) {
  367. intro();
  368. if (argc < 2) {
  369. usage();
  370. return 0;
  371. }
  372. options.outputpath = defaultSearchFileName;
  373. parsing(argc, argv);
  374. if (options.threads < 0)
  375. options.threads = std::thread::hardware_concurrency();
  376. ;
  377. std::vector<std::thread> threads(options.threads);
  378. for (unsigned int j = options.threads; j--;) {
  379. threads[j] = std::thread(
  380. static_cast<void (*)(const char *)>(miner),
  381. options.searchtextby == nullptr ? argv[1] : options.searchtextby);
  382. }
  383. for (unsigned int j = 0; j < (unsigned int)options.threads; j++)
  384. threads[j].join();
  385. }