vlmcs.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  1. #ifndef CONFIG
  2. #define CONFIG "config.h"
  3. #endif // CONFIG
  4. #include CONFIG
  5. #ifndef _GNU_SOURCE
  6. #define _GNU_SOURCE
  7. #endif
  8. #include "vlmcs.h"
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <stdint.h>
  14. #include <getopt.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #ifndef _WIN32
  19. #include <sys/ioctl.h>
  20. #include <termios.h>
  21. #else // _WIN32
  22. #endif // _WIN32
  23. #include "endian.h"
  24. #include "shared_globals.h"
  25. #include "output.h"
  26. #ifndef USE_MSRPC
  27. #include "network.h"
  28. #include "rpc.h"
  29. #else // USE_MSRPC
  30. #include "msrpc-client.h"
  31. #endif // USE_MSRPC
  32. #include "kms.h"
  33. #include "helpers.h"
  34. #include "dns_srv.h"
  35. #define VLMCS_OPTION_GRAB_INI 1
  36. #define VLMCS_OPTION_NO_GRAB_INI 2
  37. //#define kmsVersionMinor 0 // Currently constant. May change in future KMS versions
  38. #ifndef IS_LIBRARY
  39. // Function Prototypes
  40. static void CreateRequestBase(REQUEST *Request);
  41. // KMS Parameters
  42. #ifndef NO_VERBOSE_LOG
  43. static int_fast8_t verbose = FALSE;
  44. #endif
  45. static int_fast8_t VMInfo = FALSE;
  46. static int_fast8_t dnsnames = TRUE;
  47. static int FixedRequests = 0;
  48. static DWORD LicenseStatus = 0x02;
  49. static const char *CMID = NULL;
  50. static const char *CMID_prev = NULL;
  51. static const char *WorkstationName = NULL;
  52. static int BindingExpiration = 43200; //30 days
  53. static const char *RemoteAddr;
  54. static int_fast8_t ReconnectForEachRequest = FALSE;
  55. static int AddressFamily = AF_UNSPEC;
  56. static int_fast8_t incompatibleOptions = 0;
  57. static const char* fn_ini_client = NULL;
  58. static int_fast16_t kmsVersionMinor = 0;
  59. static const char* ePidGroup[] = { "Windows", "Office2010", "Office2013", "Office2016" };
  60. #ifndef NO_DNS
  61. static int_fast8_t NoSrvRecordPriority = FALSE;
  62. #endif // NO_DNS
  63. // Structure for handling "License Packs" (e.g. Office2013v5 or WindowsVista)
  64. typedef struct
  65. {
  66. const char *names; //This is a list of strings. Terminate with additional Zero!!!
  67. int N_Policy;
  68. int kmsVersionMajor;
  69. const GUID *AppID;
  70. GUID ActID;
  71. GUID KMSID;
  72. } LicensePack;
  73. typedef char iniFileEpidLines[4][256];
  74. // Well known "license packs"
  75. static const LicensePack LicensePackList[] =
  76. {
  77. // List of names min lics version appID skuId KMSCountedID
  78. /* 000 */ { "Vista\000W6\000"
  79. "WindowsVista\000"
  80. "Windows\000", 25, 4, PWINGUID, { 0x4f3d1606, 0x3fea, 0x4c01, { 0xbe, 0x3c, 0x8d, 0x67, 0x1c, 0x40, 0x1e, 0x3b, } }, { 0x212a64dc, 0x43b1, 0x4d3d, { 0xa3, 0x0c, 0x2f, 0xc6, 0x9d, 0x20, 0x95, 0xc6 } } },
  81. /* 001 */ { "W7\000Windows7\000", 25, 4, PWINGUID, { 0xb92e9980, 0xb9d5, 0x4821, { 0x9c, 0x94, 0x14, 0x0f, 0x63, 0x2f, 0x63, 0x12, } }, { 0x7fde5219, 0xfbfa, 0x484a, { 0x82, 0xc9, 0x34, 0xd1, 0xad, 0x53, 0xe8, 0x56 } } },
  82. /* 002 */ { "W8\000Windows8\000", 25, 5, PWINGUID, { 0xa98bcd6d, 0x5343, 0x4603, { 0x8a, 0xfe, 0x59, 0x08, 0xe4, 0x61, 0x11, 0x12, } }, { 0x3c40b358, 0x5948, 0x45af, { 0x92, 0x3b, 0x53, 0xd2, 0x1f, 0xcc, 0x7e, 0x79 } } },
  83. /* 003 */ { "W8C\000Windows8C\000", 25, 5, PWINGUID, { 0xc04ed6bf, 0x55c8, 0x4b47, { 0x9f, 0x8e, 0x5a, 0x1f, 0x31, 0xce, 0xee, 0x60, } }, { 0xbbb97b3b, 0x8ca4, 0x4a28, { 0x97, 0x17, 0x89, 0xfa, 0xbd, 0x42, 0xc4, 0xac } } },
  84. /* 004 */ { "W81\000Windows81\000", 25, 6, PWINGUID, { 0xc06b6981, 0xd7fd, 0x4a35, { 0xb7, 0xb4, 0x05, 0x47, 0x42, 0xb7, 0xaf, 0x67, } }, { 0xcb8fc780, 0x2c05, 0x495a, { 0x97, 0x10, 0x85, 0xaf, 0xff, 0xc9, 0x04, 0xd7 } } },
  85. /* 005 */ { "W81C\000Windows81C\000", 25, 6, PWINGUID, { 0xfe1c3238, 0x432a, 0x43a1, { 0x8e, 0x25, 0x97, 0xe7, 0xd1, 0xef, 0x10, 0xf3, } }, { 0x6d646890, 0x3606, 0x461a, { 0x86, 0xab, 0x59, 0x8b, 0xb8, 0x4a, 0xce, 0x82 } } },
  86. /* 006 */ { "W10\000Windows10\000", 25, 6, PWINGUID, { 0x73111121, 0x5638, 0x40f6, { 0xbc, 0x11, 0xf1, 0xd7, 0xb0, 0xd6, 0x43, 0x00, } }, { 0x58e2134f, 0x8e11, 0x4d17, { 0x9c, 0xb2, 0x91, 0x06, 0x9c, 0x15, 0x11, 0x48 } } },
  87. /* 007 */ { "W10C\000Windows10C\000", 25, 6, PWINGUID, { 0x58e97c99, 0xf377, 0x4ef1, { 0x81, 0xd5, 0x4a, 0xd5, 0x52, 0x2b, 0x5f, 0xd8, } }, { 0xe1c51358, 0xfe3e, 0x4203, { 0xa4, 0xa2, 0x3b, 0x6b, 0x20, 0xc9, 0x73, 0x4e } } },
  88. /* 008 */ { "2008" "\0" "2008A\000", 5, 4, PWINGUID, { 0xddfa9f7c, 0xf09e, 0x40b9, { 0x8c, 0x1a, 0xbe, 0x87, 0x7a, 0x9a, 0x7f, 0x4b, } }, { 0x33e156e4, 0xb76f, 0x4a52, { 0x9f, 0x91, 0xf6, 0x41, 0xdd, 0x95, 0xac, 0x48 } } },
  89. /* 009 */ { "2008B\000", 5, 4, PWINGUID, { 0xc1af4d90, 0xd1bc, 0x44ca, { 0x85, 0xd4, 0x00, 0x3b, 0xa3, 0x3d, 0xb3, 0xb9, } }, { 0x8fe53387, 0x3087, 0x4447, { 0x89, 0x85, 0xf7, 0x51, 0x32, 0x21, 0x5a, 0xc9 } } },
  90. /* 010 */ { "2008C\000", 5, 4, PWINGUID, { 0x68b6e220, 0xcf09, 0x466b, { 0x92, 0xd3, 0x45, 0xcd, 0x96, 0x4b, 0x95, 0x09, } }, { 0x8a21fdf3, 0xcbc5, 0x44eb, { 0x83, 0xf3, 0xfe, 0x28, 0x4e, 0x66, 0x80, 0xa7 } } },
  91. /* 011 */ { "2008R2" "\0" "2008R2A\000", 5, 4, PWINGUID, { 0xa78b8bd9, 0x8017, 0x4df5, { 0xb8, 0x6a, 0x09, 0xf7, 0x56, 0xaf, 0xfa, 0x7c, } }, { 0x0fc6ccaf, 0xff0e, 0x4fae, { 0x9d, 0x08, 0x43, 0x70, 0x78, 0x5b, 0xf7, 0xed } } },
  92. /* 012 */ { "2008R2B\000", 5, 4, PWINGUID, { 0x620e2b3d, 0x09e7, 0x42fd, { 0x80, 0x2a, 0x17, 0xa1, 0x36, 0x52, 0xfe, 0x7a, } }, { 0xca87f5b6, 0xcd46, 0x40c0, { 0xb0, 0x6d, 0x8e, 0xcd, 0x57, 0xa4, 0x37, 0x3f } } },
  93. /* 013 */ { "2008R2C\000", 5, 4, PWINGUID, { 0x7482e61b, 0xc589, 0x4b7f, { 0x8e, 0xcc, 0x46, 0xd4, 0x55, 0xac, 0x3b, 0x87, } }, { 0xb2ca2689, 0xa9a8, 0x42d7, { 0x93, 0x8d, 0xcf, 0x8e, 0x9f, 0x20, 0x19, 0x58 } } },
  94. /* 014 */ { "2012\000", 5, 5, PWINGUID, { 0xf0f5ec41, 0x0d55, 0x4732, { 0xaf, 0x02, 0x44, 0x0a, 0x44, 0xa3, 0xcf, 0x0f, } }, { 0x8665cb71, 0x468c, 0x4aa3, { 0xa3, 0x37, 0xcb, 0x9b, 0xc9, 0xd5, 0xea, 0xac } } },
  95. /* 015 */ { "2012R2\000" "12R2\000", 5, 6, PWINGUID, { 0x00091344, 0x1ea4, 0x4f37, { 0xb7, 0x89, 0x01, 0x75, 0x0b, 0xa6, 0x98, 0x8c, } }, { 0x8456EFD3, 0x0C04, 0x4089, { 0x87, 0x40, 0x5b, 0x72, 0x38, 0x53, 0x5a, 0x65 } } },
  96. /* 016 */ { "Office2010\000O14\000", 5, 4, POFFICE2010GUID, { 0x6f327760, 0x8c5c, 0x417c, { 0x9b, 0x61, 0x83, 0x6a, 0x98, 0x28, 0x7e, 0x0c, } }, { 0xe85af946, 0x2e25, 0x47b7, { 0x83, 0xe1, 0xbe, 0xbc, 0xeb, 0xea, 0xc6, 0x11 } } },
  97. /* 017 */ { "Office2013\000O15\000", 5, 6, POFFICE2013GUID, { 0xb322da9c, 0xa2e2, 0x4058, { 0x9e, 0x4e, 0xf5, 0x9a, 0x69, 0x70, 0xbd, 0x69, } }, { 0xe6a6f1bf, 0x9d40, 0x40c3, { 0xaa, 0x9f, 0xc7, 0x7b, 0xa2, 0x15, 0x78, 0xc0 } } },
  98. /* 018 */ { "Office2013V5\000", 5, 5, POFFICE2013GUID, { 0xb322da9c, 0xa2e2, 0x4058, { 0x9e, 0x4e, 0xf5, 0x9a, 0x69, 0x70, 0xbd, 0x69, } }, { 0xe6a6f1bf, 0x9d40, 0x40c3, { 0xaa, 0x9f, 0xc7, 0x7b, 0xa2, 0x15, 0x78, 0xc0 } } },
  99. /* 019 */ { "Office2016\000" "O16\000", 5, 6, POFFICE2013GUID, { 0xd450596f, 0x894d, 0x49e0, { 0x96, 0x6a, 0xfd, 0x39, 0xed, 0x4c, 0x4c, 0x64, } }, { 0x85b5f61b, 0x320b, 0x4be3, { 0x81, 0x4a, 0xb7, 0x6b, 0x2b, 0xfa, 0xfc, 0x82 } } },
  100. /* 020 */ { NULL, 0, 0, NULL, { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } }, { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } } }
  101. };
  102. typedef struct
  103. {
  104. const char* first[16];
  105. const char* second[16];
  106. const char* tld[22];
  107. } DnsNames;
  108. // Some names for the DNS name random generator
  109. static DnsNames ClientDnsNames =
  110. {
  111. { "www", "ftp", "kms", "hack-me", "smtp", "ns1", "mx1", "ns1", "pop3", "imap", "mail", "dns", "headquarter", "we-love", "_vlmcs._tcp", "ceo-laptop" },
  112. { ".microsoft", ".apple", ".amazon", ".samsung", ".adobe", ".google", ".yahoo", ".facebook", ".ubuntu", ".oracle", ".borland", ".htc", ".acer", ".windows", ".linux", ".sony" },
  113. { ".com", ".net", ".org", ".cn", ".co.uk", ".de", ".com.tw", ".us", ".fr", ".it", ".me", ".info", ".biz", ".co.jp", ".ua", ".at", ".es", ".pro", ".by", ".ru", ".pl", ".kr" }
  114. };
  115. // This is the one, we are actually using. We use Vista, if user selects nothing
  116. LicensePack ActiveLicensePack;
  117. // Request Count Control Variables
  118. static int RequestsToGo = 1;
  119. static BOOL firstRequestSent = FALSE;
  120. static void string2UuidOrExit(const char *const restrict input, GUID *const restrict guid)
  121. {
  122. if (strlen(input) != GUID_STRING_LENGTH || !string2Uuid(input, guid))
  123. {
  124. errorout("Fatal: Command line contains an invalid GUID.\n");
  125. exit(!0);
  126. }
  127. }
  128. #ifndef NO_HELP
  129. __noreturn static void clientUsage(const char* const programName)
  130. {
  131. errorout(
  132. "vlmcs %s \n\n"
  133. # ifndef NO_DNS
  134. "Usage: %s [options] [ <host>[:<port>] | .<domain> | - ] [options]\n\n"
  135. # else // DNS
  136. "Usage: %s [options] [<host>[:<port>]] [options]\n\n"
  137. # endif // DNS
  138. "Options:\n\n"
  139. # ifndef NO_VERBOSE_LOG
  140. " -v Be verbose\n"
  141. # endif
  142. " -l <app>\n"
  143. " -4 Force V4 protocol\n"
  144. " -5 Force V5 protocol\n"
  145. " -6 Force V6 protocol\n"
  146. # ifndef USE_MSRPC
  147. " -i <IpVersion> Use IP protocol (4 or 6)\n"
  148. # endif // USE_MSRPC
  149. " -e Show some valid examples\n"
  150. " -x Show valid Apps\n"
  151. " -d no DNS names, use Netbios names (no effect if -w is used)\n"
  152. " -V show version information and exit\n\n"
  153. "Advanced options:\n\n"
  154. " -a <AppGUID> Use custom Application GUID\n"
  155. " -s <ActGUID> Use custom Activation Configuration GUID\n"
  156. " -k <KmsGUID> Use custom KMS GUID\n"
  157. " -c <ClientGUID> Use custom Client GUID. Default: Use random\n"
  158. " -o <PreviousClientGUID> Use custom Prevoius Client GUID. Default: ZeroGUID\n"
  159. " -K <ProtocolVersion> Use a specific (possibly invalid) protocol version\n"
  160. " -w <Workstation> Use custom workstation name. Default: Use random\n"
  161. " -r <RequiredClientCount> Fake required clients\n"
  162. " -n <Requests> Fixed # of requests (Default: Enough to charge)\n"
  163. " -m Pretend to be a virtual machine\n"
  164. " -G <file> Get ePID/HwId data and write to <file>. Can't be used with -l, -4, -5, -6, -a, -s, -k, -r and -n\n"
  165. # ifndef USE_MSRPC
  166. " -T Use a new TCP connection for each request.\n"
  167. " -N <0|1> disable or enable NDR64. Default: 1\n"
  168. " -B <0|1> disable or enable RPC bind time feature negotiation. Default: 1\n"
  169. # endif // USE_MSRPC
  170. " -t <LicenseStatus> Use specfic license status (0 <= T <= 6)\n"
  171. " -g <BindingExpiration> Use a specfic binding expiration time in minutes. Default 43200\n"
  172. # ifndef NO_DNS
  173. " -P Ignore priority and weight in DNS SRV records\n"
  174. # endif // NO_DNS
  175. # ifndef USE_MSRPC
  176. " -p Don't use multiplexed RPC bind\n"
  177. # endif // USE_MSRPC
  178. "\n"
  179. "<port>:\t\tTCP port name of the KMS to use. Default 1688.\n"
  180. "<host>:\t\thost name of the KMS to use. Default 127.0.0.1\n"
  181. # ifndef NO_DNS
  182. ".<domain>:\tfind KMS server in <domain> via DNS\n"
  183. # endif // NO_DNS
  184. "<app>:\t\t(Type %s -x to see a list of valid apps)\n\n",
  185. Version, programName, programName
  186. );
  187. exit(!0);
  188. }
  189. __pure static int getLineWidth(void)
  190. {
  191. #ifdef TERMINAL_FIXED_WIDTH // For Toolchains that to not have winsize
  192. return TERMINAL_FIXED_WIDTH;
  193. #else // Can determine width of terminal
  194. #ifndef _WIN32
  195. struct winsize w;
  196. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &w))
  197. {
  198. return 80; // Return this if stdout is not a tty
  199. }
  200. return w.ws_col;
  201. #else // _WIN32
  202. CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
  203. HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  204. if (!GetConsoleScreenBufferInfo(hStdout, &csbiInfo))
  205. {
  206. return 80; // Return this if stdout is not a Console
  207. }
  208. return csbiInfo.srWindow.Right - csbiInfo.srWindow.Left;
  209. #endif // WIN32
  210. #endif // Can determine width of terminal
  211. }
  212. __noreturn static void showProducts(PRINTFUNC p)
  213. {
  214. int cols = getLineWidth();
  215. int itemsPerLine;
  216. uint8_t i;
  217. p(
  218. "The following "
  219. #if !defined(NO_EXTENDED_PRODUCT_LIST) && !defined(NO_BASIC_PRODUCT_LIST)
  220. "aliases "
  221. #else
  222. "names "
  223. #endif
  224. "can be used with -l:\n\n"
  225. );
  226. const LicensePack* lp;
  227. itemsPerLine = cols / 20;
  228. if (!itemsPerLine) itemsPerLine = 1;
  229. for (i = 1, lp = LicensePackList; lp->names; lp++)
  230. {
  231. const char* name;
  232. for (name = lp->names; *name; name += strlen(name) + 1, i++)
  233. {
  234. p("%-20s", name);
  235. if (!(i % itemsPerLine)) p("\n");
  236. }
  237. }
  238. p("\n\n");
  239. #if !defined(NO_EXTENDED_PRODUCT_LIST) && !defined(NO_BASIC_PRODUCT_LIST)
  240. const KmsIdList* currentProduct;
  241. uint_fast8_t longestString = 0;
  242. uint8_t k, items = getExtendedProductListSize();
  243. p("You may also use these product names or numbers:\n\n");
  244. for (currentProduct = ExtendedProductList; currentProduct->name; currentProduct++)
  245. {
  246. uint_fast8_t len = strlen(currentProduct->name);
  247. if (len > longestString)
  248. longestString = len;
  249. }
  250. itemsPerLine = cols / (longestString + 10);
  251. if (!itemsPerLine) itemsPerLine = 1;
  252. uint8_t lines = items / itemsPerLine;
  253. if (items % itemsPerLine) lines++;
  254. for (i = 0; i < lines; i++)
  255. {
  256. for (k = 0; k < itemsPerLine; k++)
  257. {
  258. uint8_t j;
  259. uint8_t index = k * lines + i;
  260. if (index >= items) break;
  261. p("%3u = %s", index + 1, ExtendedProductList[index].name);
  262. for (j = 0; j < longestString + 4 - strlen(ExtendedProductList[index].name); j++)
  263. {
  264. p(" ");
  265. }
  266. }
  267. p("\n");
  268. }
  269. p("\n");
  270. #endif // !defined(NO_EXTENDED_PRODUCT_LIST) && !defined(NO_BASIC_PRODUCT_LIST)
  271. exit(0);
  272. }
  273. __noreturn static void examples(const char* const programName)
  274. {
  275. printf(
  276. "\nRequest activation for Office2013 using V4 protocol from 192.168.1.5:1688\n"
  277. "\t%s -l O15 -4 192.168.1.5\n"
  278. "\t%s -l O15 -4 192.168.1.5:1688\n\n"
  279. "Request activation for Windows Server 2012 using V4 protocol from localhost:1688\n"
  280. "\t%s -4 -l Windows -k 8665cb71-468c-4aa3-a337-cb9bc9d5eaac\n"
  281. "\t%s -4 -l 2012\n"
  282. "\t%s -4 -l 2012 [::1]:1688\n"
  283. "\t%s -4 -l 12 127.0.0.2:1688\n\n"
  284. "Send 100,000 requests to localhost:1688\n"
  285. "\t%s -n 100000 -l Office2010\n\n"
  286. "Request Activation for Windows 8 from 10.0.0.1:4711 and pretend to be Steve Ballmer\n"
  287. "\t%s -l Windows8 -w steveb1.redmond.microsoft.com 10.0.0.1:4711\n\n",
  288. programName, programName, programName, programName, programName, programName, programName, programName
  289. );
  290. exit(0);
  291. }
  292. static void parseProtocolVersion(void)
  293. {
  294. char *endptr_major, *endptr_minor, *period = strchr(optarg, (int)'.');
  295. if (!period)
  296. {
  297. errorout("Fatal: Protocol version must be in the format #.#\n");
  298. exit(!0);
  299. }
  300. long major = strtol(optarg, &endptr_major, 10);
  301. long minor = strtol(period + 1, &endptr_minor, 10);
  302. if ((*endptr_major && *endptr_major != '.') || *endptr_minor || *optarg == '.' || !period[1])
  303. {
  304. errorout("Fatal: Protocol version must be in the format #.#\n");
  305. exit(!0);
  306. }
  307. if (major < 0 || major > 0xffff || minor < 0 || minor > 0xffff)
  308. {
  309. errorout("Fatal: Major and minor protocol version number must be between 0 and 65535\n");
  310. exit(!0);
  311. }
  312. ActiveLicensePack.kmsVersionMajor = (int)major;
  313. kmsVersionMinor = (int_fast16_t)minor;
  314. }
  315. #else // NO_HELP
  316. __noreturn static void clientUsage(const char* const programName)
  317. {
  318. errorout("Incorrect parameter specified.\n");
  319. exit(!0);
  320. }
  321. #endif // NO_HELP
  322. static BOOL findLicensePackByName(const char* const name, LicensePack* const lp)
  323. {
  324. // Try to find a package in the short list first
  325. LicensePack *licensePack;
  326. for (licensePack = (LicensePack*)&LicensePackList; licensePack->names; licensePack ++)
  327. {
  328. const char *currentName;
  329. for (currentName = licensePack->names; *currentName; currentName += strlen(currentName) + 1)
  330. {
  331. if (!strcasecmp(name, currentName))
  332. {
  333. *lp = *licensePack;
  334. return TRUE;
  335. }
  336. }
  337. }
  338. #if defined(NO_BASIC_PRODUCT_LIST) || defined(NO_EXTENDED_PRODUCT_LIST)
  339. return FALSE;
  340. #else // Both Lists are available
  341. // search extended product list
  342. uint8_t items = getExtendedProductListSize();
  343. unsigned int index;
  344. if (stringToInt(name, 1, items, &index))
  345. {
  346. index--;
  347. }
  348. else
  349. {
  350. for (index = 0; index < items; index++)
  351. {
  352. if (!strcasecmp(ExtendedProductList[index].name, name)) break;
  353. }
  354. if (index >= items) return FALSE;
  355. }
  356. lp->AppID = &AppList[ExtendedProductList[index].AppIndex].guid;
  357. lp->KMSID = ProductList[ExtendedProductList[index].KmsIndex].guid;
  358. lp->ActID = ExtendedProductList[index].guid;
  359. lp->N_Policy = ProductList[ExtendedProductList[index].KmsIndex].KMS_PARAM_REQUIREDCOUNT;
  360. lp->kmsVersionMajor = ProductList[ExtendedProductList[index].KmsIndex].KMS_PARAM_MAJOR;
  361. return TRUE;
  362. #endif // Both Lists are available
  363. }
  364. static const char* const client_optstring = "+N:B:i:l:a:s:k:c:w:r:n:t:g:G:o:K:pPTv456mexdV";
  365. //First pass. We handle only "-l". Since -a -k -s -4 -5 and -6 are exceptions to -l, we process -l first
  366. static void parseCommandLinePass1(const int argc, CARGV argv)
  367. {
  368. int o;
  369. optReset();
  370. for (opterr = 0; ( o = getopt(argc, (char* const*)argv, client_optstring) ) > 0; ) switch (o)
  371. {
  372. case 'l': // Set "License Pack" and protocol version (e.g. Windows8, Office2013v5, ...)
  373. if (!findLicensePackByName(optarg, &ActiveLicensePack))
  374. {
  375. errorout("Invalid client application. \"%s\" is not valid for -l.\n\n", optarg);
  376. #ifndef NO_HELP
  377. showProducts(&errorout);
  378. #endif // !NO_HELP
  379. }
  380. break;
  381. default:
  382. break;
  383. }
  384. }
  385. // Second Pass. Handle all options except "-l"
  386. static void parseCommandLinePass2(const char *const programName, const int argc, CARGV argv)
  387. {
  388. int o;
  389. optReset();
  390. for (opterr = 0; ( o = getopt(argc, (char* const*)argv, client_optstring) ) > 0; ) switch (o)
  391. {
  392. #ifndef NO_HELP
  393. case 'e': // Show examples
  394. examples(programName);
  395. break;
  396. case 'x': // Show Apps
  397. showProducts(&printf);
  398. break;
  399. #endif // NO_HELP
  400. # ifndef NO_DNS
  401. case 'P':
  402. NoSrvRecordPriority = TRUE;
  403. break;
  404. # endif // NO_DNS
  405. case 'G':
  406. incompatibleOptions |= VLMCS_OPTION_GRAB_INI;
  407. fn_ini_client = optarg;
  408. break;
  409. # ifndef USE_MSRPC
  410. case 'N':
  411. if (!getArgumentBool(&UseRpcNDR64, optarg)) clientUsage(programName);
  412. break;
  413. case 'B':
  414. if (!getArgumentBool(&UseRpcBTFN, optarg)) clientUsage(programName);
  415. break;
  416. case 'i':
  417. switch(getOptionArgumentInt(o, 4, 6))
  418. {
  419. case 4:
  420. AddressFamily = AF_INET;
  421. break;
  422. case 6:
  423. AddressFamily = AF_INET6;
  424. break;
  425. default:
  426. errorout("IPv5 does not exist.\n");
  427. exit(!0);
  428. break;
  429. }
  430. break;
  431. case 'p': // Multiplexed RPC
  432. UseMultiplexedRpc = FALSE;
  433. break;
  434. # endif // USE_MSRPC
  435. case 'n': // Fixed number of Requests (regardless, whether they are required)
  436. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  437. FixedRequests = getOptionArgumentInt(o, 1, INT_MAX);
  438. break;
  439. case 'r': // Fake minimum required client count
  440. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  441. ActiveLicensePack.N_Policy = getOptionArgumentInt(o, 0, INT_MAX);
  442. break;
  443. case 'c': // use a specific client GUID
  444. // If using a constant Client ID, send only one request unless /N= explicitly specified
  445. if (!FixedRequests) FixedRequests = 1;
  446. CMID = optarg;
  447. break;
  448. case 'o': // use a specific previous client GUID
  449. CMID_prev = optarg;
  450. break;
  451. case 'a': // Set specific App Id
  452. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  453. ActiveLicensePack.AppID = (GUID*)vlmcsd_malloc(sizeof(GUID));
  454. string2UuidOrExit(optarg, (GUID*)ActiveLicensePack.AppID);
  455. break;
  456. case 'g': // Set custom "grace" time in minutes (default 30 days)
  457. BindingExpiration = getOptionArgumentInt(o, 0, INT_MAX);
  458. break;
  459. case 's': // Set specfic SKU ID
  460. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  461. string2UuidOrExit(optarg, &ActiveLicensePack.ActID);
  462. break;
  463. case 'k': // Set specific KMS ID
  464. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  465. string2UuidOrExit(optarg, &ActiveLicensePack.KMSID);
  466. break;
  467. case '4': // Force V4 protocol
  468. case '5': // Force V5 protocol
  469. case '6': // Force V5 protocol
  470. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  471. ActiveLicensePack.kmsVersionMajor = o - 0x30;
  472. kmsVersionMinor = 0;
  473. break;
  474. case 'K': // Use specific protocol (may be invalid)
  475. parseProtocolVersion();
  476. break;
  477. case 'd': // Don't use DNS names
  478. dnsnames = FALSE;
  479. break;
  480. # ifndef NO_VERBOSE_LOG
  481. case 'v': // Be verbose
  482. verbose = TRUE;
  483. break;
  484. # endif // NO_VERBOSE_LOG
  485. case 'm': // Pretend to be a virtual machine
  486. VMInfo = TRUE;
  487. break;
  488. case 'w': // WorkstationName (max. 63 chars)
  489. WorkstationName = optarg;
  490. if (strlen(WorkstationName) > 63)
  491. {
  492. errorout("\007WARNING! Truncating workstation name to 63 characters (%s).\n", WorkstationName);
  493. }
  494. break;
  495. case 't':
  496. LicenseStatus = getOptionArgumentInt(o, 0, 0x7fffffff);
  497. if ((unsigned int)LicenseStatus > 6) errorout("Warning: Correct license status is 0 <= license status <= 6.\n");
  498. break;
  499. # ifndef USE_MSRPC
  500. case 'T':
  501. ReconnectForEachRequest = TRUE;
  502. break;
  503. # endif // USE_MSRPC
  504. case 'l':
  505. incompatibleOptions |= VLMCS_OPTION_NO_GRAB_INI;
  506. break;
  507. # ifndef NO_VERSION_INFORMATION
  508. case 'V':
  509. # if defined(__s390__) && !defined(__zarch__) && !defined(__s390x__)
  510. printf("vlmcs %s %i-bit\n", Version, sizeof(void*) == 4 ? 31 : (int)sizeof(void*) << 3);
  511. # else
  512. printf("vlmcs %s %i-bit\n", Version, (int)sizeof(void*) << 3);
  513. # endif // defined(__s390__) && !defined(__zarch__) && !defined(__s390x__)
  514. printPlatform();
  515. printCommonFlags();
  516. printClientFlags();
  517. exit(0);
  518. # endif // NO_VERSION_INFORMATION
  519. default:
  520. clientUsage(programName);
  521. }
  522. if ((incompatibleOptions & (VLMCS_OPTION_NO_GRAB_INI | VLMCS_OPTION_GRAB_INI)) == (VLMCS_OPTION_NO_GRAB_INI | VLMCS_OPTION_GRAB_INI))
  523. clientUsage(programName);
  524. }
  525. /*
  526. * Compares 2 GUIDs where one is host-endian and the other is little-endian (network byte order)
  527. */
  528. int_fast8_t IsEqualGuidLEHE(const GUID* const guid1, const GUID* const guid2)
  529. {
  530. GUID tempGuid;
  531. LEGUID(&tempGuid, guid2);
  532. return IsEqualGUID(guid1, &tempGuid);
  533. }
  534. #ifndef USE_MSRPC
  535. static void checkRpcLevel(const REQUEST* request, RESPONSE* response)
  536. {
  537. if (!RpcFlags.HasNDR32)
  538. errorout("\nWARNING: Server's RPC protocol does not support NDR32.\n");
  539. if (UseRpcBTFN && UseRpcNDR64 && RpcFlags.HasNDR64 && !RpcFlags.HasBTFN)
  540. errorout("\nWARNING: Server's RPC protocol has NDR64 but no BTFN.\n");
  541. # ifndef NO_BASIC_PRODUCT_LIST
  542. if (!IsEqualGuidLEHE(&request->KMSID, &ProductList[15].guid) && UseRpcBTFN && !RpcFlags.HasBTFN)
  543. errorout("\nWARNING: A server with pre-Vista RPC activated a product other than Office 2010.\n");
  544. # endif // NO_BASIC_PRODUCT_LIST
  545. }
  546. #endif // USE_MSRPC
  547. static void displayResponse(const RESPONSE_RESULT result, const REQUEST* request, RESPONSE* response, BYTE *hwid)
  548. {
  549. fflush(stdout);
  550. if (!result.RpcOK) errorout("\n\007ERROR: Non-Zero RPC result code.\n");
  551. if (!result.DecryptSuccess) errorout("\n\007ERROR: Decryption of V5/V6 response failed.\n");
  552. if (!result.IVsOK) errorout("\n\007ERROR: AES CBC initialization vectors (IVs) of request and response do not match.\n");
  553. if (!result.PidLengthOK) errorout("\n\007ERROR: The length of the PID is not valid.\n");
  554. if (!result.HashOK) errorout("\n\007ERROR: Computed hash does not match hash in response.\n");
  555. if (!result.ClientMachineIDOK) errorout("\n\007ERROR: Client machine GUIDs of request and response do not match.\n");
  556. if (!result.TimeStampOK) errorout("\n\007ERROR: Time stamps of request and response do not match.\n");
  557. if (!result.VersionOK) errorout("\n\007ERROR: Protocol versions of request and response do not match.\n");
  558. if (!result.HmacSha256OK) errorout("\n\007ERROR: Keyed-Hash Message Authentication Code (HMAC) is incorrect.\n");
  559. if (!result.IVnotSuspicious) errorout("\nWARNING: The KMS server is an emulator because the response uses an IV following KMSv5 rules in KMSv6 protocol.\n");
  560. if (result.effectiveResponseSize != result.correctResponseSize)
  561. {
  562. errorout("\n\007WARNING: Size of RPC payload (KMS Message) should be %u but is %u.", result.correctResponseSize, result.effectiveResponseSize);
  563. }
  564. # ifndef USE_MSRPC
  565. checkRpcLevel(request, response);
  566. # endif // USE_MSRPC
  567. if (!result.DecryptSuccess) return; // Makes no sense to display anything
  568. char ePID[3 * PID_BUFFER_SIZE];
  569. if (!ucs2_to_utf8(response->KmsPID, ePID, PID_BUFFER_SIZE, 3 * PID_BUFFER_SIZE))
  570. {
  571. memset(ePID + 3 * PID_BUFFER_SIZE - 3, 0, 3);
  572. }
  573. // Read KMSPID from Response
  574. # ifndef NO_VERBOSE_LOG
  575. if (!verbose)
  576. # endif // NO_VERBOSE_LOG
  577. {
  578. printf(" -> %s", ePID);
  579. if (LE16(response->MajorVer) > 5)
  580. {
  581. # ifndef _WIN32
  582. printf(" (%016llX)", (unsigned long long)BE64(*(uint64_t*)hwid));
  583. # else // _WIN32
  584. printf(" (%016I64X)", (unsigned long long)BE64(*(uint64_t*)hwid));
  585. # endif // _WIN32
  586. }
  587. printf("\n");
  588. }
  589. # ifndef NO_VERBOSE_LOG
  590. else
  591. {
  592. printf(
  593. "\n\nResponse from KMS server\n========================\n\n"
  594. "Size of KMS Response : %u (0x%x)\n", result.effectiveResponseSize, result.effectiveResponseSize
  595. );
  596. logResponseVerbose(ePID, hwid, response, &printf);
  597. printf("\n");
  598. }
  599. # endif // NO_VERBOSE_LOG
  600. }
  601. static void connectRpc(RpcCtx *s)
  602. {
  603. # ifdef NO_DNS
  604. *s = connectToAddress(RemoteAddr, AddressFamily, FALSE);
  605. if (*s == INVALID_RPCCTX)
  606. {
  607. errorout("Fatal: Could not connect to %s\n", RemoteAddr);
  608. exit(!0);
  609. }
  610. if (verbose)
  611. printf("\nPerforming RPC bind ...\n");
  612. if (rpcBindClient(*s, verbose))
  613. {
  614. errorout("Fatal: Could not bind RPC\n");
  615. exit(!0);
  616. }
  617. if (verbose) printf("... successful\n");
  618. # else // DNS
  619. static kms_server_dns_ptr* serverlist = NULL;
  620. static int numServers = 0;
  621. //static int_fast8_t ServerListAlreadyPrinted = FALSE;
  622. int i;
  623. if (!strcmp(RemoteAddr, "-") || *RemoteAddr == '.') // Get KMS server via DNS SRV record
  624. {
  625. if (!serverlist)
  626. numServers = getKmsServerList(&serverlist, RemoteAddr);
  627. if (numServers < 1)
  628. {
  629. errorout("Fatal: No KMS servers found\n");
  630. exit(!0);
  631. }
  632. if (!NoSrvRecordPriority) sortSrvRecords(serverlist, numServers);
  633. # ifndef NO_VERBOSE_LOG
  634. if (verbose /*&& !ServerListAlreadyPrinted*/)
  635. {
  636. for (i = 0; i < numServers; i++)
  637. {
  638. printf(
  639. "Found %-40s (priority: %hu, weight: %hu, randomized weight: %i)\n",
  640. serverlist[i]->serverName,
  641. serverlist[i]->priority, serverlist[i]->weight,
  642. NoSrvRecordPriority ? 0 : serverlist[i]->random_weight
  643. );
  644. }
  645. printf("\n");
  646. //ServerListAlreadyPrinted = TRUE;
  647. }
  648. # endif // NO_VERBOSE_LOG
  649. }
  650. else // Just use the server supplied on the command line
  651. {
  652. if (!serverlist)
  653. {
  654. serverlist = (kms_server_dns_ptr*)vlmcsd_malloc(sizeof(kms_server_dns_ptr));
  655. *serverlist = (kms_server_dns_ptr)vlmcsd_malloc(sizeof(kms_server_dns_t));
  656. numServers = 1;
  657. strncpy((*serverlist)->serverName, RemoteAddr, sizeof((*serverlist)->serverName));
  658. }
  659. }
  660. for (i = 0; i < numServers; i++)
  661. {
  662. *s = connectToAddress(serverlist[i]->serverName, AddressFamily, (*RemoteAddr == '.' || *RemoteAddr == '-'));
  663. if (*s == INVALID_RPCCTX) continue;
  664. # ifndef NO_VERBOSE_LOG
  665. if (verbose) printf("\nPerforming RPC bind ...\n");
  666. if (rpcBindClient(*s, verbose))
  667. # else
  668. if (rpcBindClient(*s, FALSE))
  669. # endif
  670. {
  671. errorout("Warning: Could not bind RPC\n");
  672. continue;
  673. }
  674. # ifndef NO_VERBOSE_LOG
  675. if (verbose) printf("... successful\n");
  676. # endif
  677. return;
  678. }
  679. errorout("Fatal: Could not connect to any KMS server\n");
  680. exit(!0);
  681. # endif // DNS
  682. }
  683. #endif // IS_LIBRARY
  684. int SendActivationRequest(const RpcCtx sock, RESPONSE *baseResponse, REQUEST *baseRequest, RESPONSE_RESULT *result, BYTE *const hwid)
  685. {
  686. size_t requestSize, responseSize;
  687. BYTE *request, *response;
  688. int status;
  689. result->mask = 0;
  690. if (LE16(baseRequest->MajorVer) < 5)
  691. request = CreateRequestV4(&requestSize, baseRequest);
  692. else
  693. request = CreateRequestV6(&requestSize, baseRequest);
  694. if (!(status = rpcSendRequest(sock, request, requestSize, &response, &responseSize)))
  695. {
  696. if (LE16(((RESPONSE*)(response))->MajorVer) == 4)
  697. {
  698. RESPONSE_V4 response_v4;
  699. *result = DecryptResponseV4(&response_v4, responseSize, response, request);
  700. memcpy(baseResponse, &response_v4.ResponseBase, sizeof(RESPONSE));
  701. }
  702. else
  703. {
  704. RESPONSE_V6 response_v6;
  705. *result = DecryptResponseV6(&response_v6, responseSize, response, request, hwid);
  706. memcpy(baseResponse, &response_v6.ResponseBase, sizeof(RESPONSE));
  707. }
  708. result->RpcOK = TRUE;
  709. }
  710. if (response) free(response);
  711. free(request);
  712. return status;
  713. }
  714. #ifndef IS_LIBRARY
  715. static int sendRequest(RpcCtx *const s, REQUEST *const request, RESPONSE *const response, hwid_t hwid, RESPONSE_RESULT *const result)
  716. {
  717. CreateRequestBase(request);
  718. if (*s == INVALID_RPCCTX )
  719. connectRpc(s);
  720. else
  721. {
  722. // Check for lame KMS emulators that close the socket after each request
  723. int_fast8_t disconnected = isDisconnected(*s);
  724. if (disconnected)
  725. errorout("\nWarning: Server closed RPC connection (probably non-multitasked KMS emulator)\n");
  726. if (ReconnectForEachRequest || disconnected)
  727. {
  728. closeRpc(*s);
  729. connectRpc(s);
  730. }
  731. }
  732. printf("Sending activation request (KMS V%u) ", ActiveLicensePack.kmsVersionMajor);
  733. fflush(stdout);
  734. return SendActivationRequest(*s, response, request, result, hwid);
  735. }
  736. static void displayRequestError(RpcCtx *const s, const int status, const int currentRequest, const int totalRequests)
  737. {
  738. errorout("\nError 0x%08X while sending request %u of %u\n", status, currentRequest, RequestsToGo + totalRequests);
  739. switch(status)
  740. {
  741. case 0xC004F042: // not licensed
  742. errorout("The server refused to activate the requested product\n");
  743. break;
  744. case 0x8007000D: // e.g. v6 protocol on a v5 server
  745. errorout("The server didn't understand the request\n");
  746. break;
  747. case 1:
  748. errorout("An RPC protocol error has occured\n");
  749. closeRpc(*s);
  750. connectRpc(s);
  751. break;
  752. default:
  753. break;
  754. }
  755. }
  756. static void newIniBackupFile(const char* const restrict fname)
  757. {
  758. FILE *restrict f = fopen(fname, "wb");
  759. if (!f)
  760. {
  761. errorout("Fatal: Cannot create %s: %s\n", fname, strerror(errno));
  762. exit(!0);
  763. }
  764. if (fclose(f))
  765. {
  766. errorout("Fatal: Cannot write to %s: %s\n", fname, strerror(errno));
  767. unlink(fname);
  768. exit(!0);
  769. }
  770. }
  771. static void updateIniFile(iniFileEpidLines* const restrict lines)
  772. {
  773. int_fast8_t lineWritten[_countof(*lines)];
  774. struct stat statbuf;
  775. uint_fast8_t i;
  776. int_fast8_t iniFileExistedBefore = TRUE;
  777. unsigned int lineNumber;
  778. memset(lineWritten, FALSE, sizeof(lineWritten));
  779. char* restrict fn_bak = (char*)vlmcsd_malloc(strlen(fn_ini_client) + 2);
  780. strcpy(fn_bak, fn_ini_client);
  781. strcat(fn_bak, "~");
  782. if (stat(fn_ini_client, &statbuf))
  783. {
  784. if (errno != ENOENT)
  785. {
  786. errorout("Fatal: %s: %s\n", fn_ini_client, strerror(errno));
  787. exit(!0);
  788. }
  789. else
  790. {
  791. iniFileExistedBefore = FALSE;
  792. newIniBackupFile(fn_bak);
  793. }
  794. }
  795. else
  796. {
  797. unlink(fn_bak); // Required for Windows. Most Unix systems don't need it.
  798. if (rename(fn_ini_client, fn_bak))
  799. {
  800. errorout("Fatal: Cannot create %s: %s\n", fn_bak, strerror(errno));
  801. exit(!0);
  802. }
  803. }
  804. printf("\n%s file %s\n", iniFileExistedBefore ? "Updating" : "Creating", fn_ini_client);
  805. FILE *restrict in, *restrict out;
  806. in = fopen(fn_bak, "rb");
  807. if (!in)
  808. {
  809. errorout("Fatal: Cannot open %s: %s\n", fn_bak, strerror(errno));
  810. exit(!0);
  811. }
  812. out = fopen(fn_ini_client, "wb");
  813. if (!out)
  814. {
  815. errorout("Fatal: Cannot create %s: %s\n", fn_ini_client, strerror(errno));
  816. exit(!0);
  817. }
  818. char sourceLine[256];
  819. for (lineNumber = 1; fgets(sourceLine, sizeof(sourceLine), in); lineNumber++)
  820. {
  821. for (i = 0; i < _countof(*lines); i++)
  822. {
  823. if (*(*lines)[i] && !strncasecmp(sourceLine, (*lines)[i], strlen(ePidGroup[i])))
  824. {
  825. if (lineWritten[i]) break;
  826. fprintf(out, "%s", (*lines)[i]);
  827. printf("line %2i: %s", lineNumber, (*lines)[i]);
  828. lineWritten[i] = TRUE;
  829. break;
  830. }
  831. }
  832. if (i >= _countof(*lines))
  833. {
  834. fprintf(out, "%s", sourceLine);
  835. }
  836. }
  837. if (ferror(in))
  838. {
  839. errorout("Fatal: Cannot read from %s: %s\n", fn_bak, strerror(errno));
  840. exit(!0);
  841. }
  842. fclose(in);
  843. for (i = 0; i < _countof(*lines); i++)
  844. {
  845. if (!lineWritten[i] && *(*lines)[i])
  846. {
  847. fprintf(out, "%s", (*lines)[i]);
  848. printf("line %2i: %s", lineNumber + i, (*lines)[i]);
  849. }
  850. }
  851. if (fclose(out))
  852. {
  853. errorout("Fatal: Cannot write to %s: %s\n", fn_ini_client, strerror(errno));
  854. exit(!0);
  855. }
  856. if (!iniFileExistedBefore) unlink(fn_bak);
  857. free(fn_bak);
  858. }
  859. static void grabServerData()
  860. {
  861. RpcCtx s = INVALID_RPCCTX;
  862. WORD MajorVer = 6;
  863. iniFileEpidLines lines;
  864. static int_fast8_t Licenses[_countof(lines)] = { 0, 16, 17, 19 };
  865. uint_fast8_t i;
  866. RESPONSE response;
  867. RESPONSE_RESULT result;
  868. REQUEST request;
  869. hwid_t hwid;
  870. int status;
  871. size_t len;
  872. for (i = 0; i < _countof(lines); i++) *lines[i] = 0;
  873. for (i = 0; i < _countof(Licenses) && MajorVer > 3; i++)
  874. {
  875. ActiveLicensePack = LicensePackList[Licenses[i]];
  876. ActiveLicensePack.kmsVersionMajor = MajorVer;
  877. status = sendRequest(&s, &request, &response, hwid, &result);
  878. printf("%-11s", ActiveLicensePack.names);
  879. if (status)
  880. {
  881. displayRequestError(&s, status, i + 7 - MajorVer, 9 - MajorVer);
  882. if (status == 1) break;
  883. if ((status & 0xF0000000) == 0x80000000)
  884. {
  885. MajorVer--;
  886. i--;
  887. }
  888. continue;
  889. }
  890. printf("%i of %i", (int)(i + 7 - MajorVer), (int)(10 - MajorVer));
  891. displayResponse(result, &request, &response, hwid);
  892. char ePID[3 * PID_BUFFER_SIZE];
  893. if (!ucs2_to_utf8(response.KmsPID, ePID, PID_BUFFER_SIZE, 3 * PID_BUFFER_SIZE))
  894. {
  895. memset(ePID + 3 * PID_BUFFER_SIZE - 3, 0, 3);
  896. }
  897. snprintf(lines[i], sizeof(lines[0]), "%s = %s", ePidGroup[i], ePID);
  898. if (response.MajorVer > 5)
  899. {
  900. len = strlen(lines[i]);
  901. snprintf (lines[i] + len, sizeof(lines[0]) - len, " / %02X %02X %02X %02X %02X %02X %02X %02X", hwid[0], hwid[1], hwid[2], hwid[3], hwid[4], hwid[5], hwid[6], hwid[7]);
  902. }
  903. len = strlen(lines[i]);
  904. snprintf(lines[i] + len, sizeof(lines[0]) - len, "\n");
  905. }
  906. if (strcmp(fn_ini_client, "-"))
  907. {
  908. updateIniFile(&lines);
  909. }
  910. else
  911. {
  912. printf("\n");
  913. for (i = 0; i < _countof(lines); i++) printf("%s", lines[i]);
  914. }
  915. }
  916. int client_main(const int argc, CARGV argv)
  917. {
  918. #if defined(_WIN32) && !defined(USE_MSRPC)
  919. // Windows Sockets must be initialized
  920. WSADATA wsadata;
  921. int error;
  922. if ((error = WSAStartup(0x0202, &wsadata)))
  923. {
  924. printerrorf("Fatal: Could not initialize Windows sockets (Error: %d).\n", error);
  925. return error;
  926. }
  927. #endif // _WIN32
  928. #ifdef _NTSERVICE
  929. // We are not a service
  930. IsNTService = FALSE;
  931. // Set console output page to UTF-8
  932. // SetConsoleOutputCP(65001);
  933. #endif // _NTSERVICE
  934. randomNumberInit();
  935. ActiveLicensePack = *LicensePackList; //first license is Windows Vista
  936. parseCommandLinePass1(argc, argv);
  937. int_fast8_t useDefaultHost = FALSE;
  938. if (optind < argc)
  939. RemoteAddr = argv[optind];
  940. else
  941. useDefaultHost = TRUE;
  942. int hostportarg = optind;
  943. if (optind < argc - 1)
  944. {
  945. parseCommandLinePass1(argc - hostportarg, argv + hostportarg);
  946. if (optind < argc - hostportarg)
  947. clientUsage(argv[0]);
  948. }
  949. parseCommandLinePass2(argv[0], argc, argv);
  950. if (optind < argc - 1)
  951. parseCommandLinePass2(argv[0], argc - hostportarg, argv + hostportarg);
  952. if (useDefaultHost)
  953. RemoteAddr = AddressFamily == AF_INET6 ? "::1" : "127.0.0.1";
  954. if (fn_ini_client != NULL)
  955. grabServerData();
  956. else
  957. {
  958. int requests;
  959. RpcCtx s = INVALID_RPCCTX;
  960. for (requests = 0, RequestsToGo = ActiveLicensePack.N_Policy == 1 ? 1 : ActiveLicensePack.N_Policy - 1; RequestsToGo; requests++)
  961. {
  962. RESPONSE response;
  963. REQUEST request;
  964. RESPONSE_RESULT result;
  965. hwid_t hwid;
  966. int status = sendRequest(&s, &request, &response, hwid, &result);
  967. if (FixedRequests) RequestsToGo = FixedRequests - requests - 1;
  968. if (status)
  969. {
  970. displayRequestError(&s, status, requests + 1, RequestsToGo + requests + 1);
  971. if (!FixedRequests) RequestsToGo = 0;
  972. }
  973. else
  974. {
  975. if (!FixedRequests)
  976. {
  977. if (firstRequestSent && ActiveLicensePack.N_Policy - (int)response.Count >= RequestsToGo)
  978. {
  979. errorout("\nThe KMS server does not increment it's active clients. Aborting...\n");
  980. RequestsToGo = 0;
  981. }
  982. else
  983. {
  984. RequestsToGo = ActiveLicensePack.N_Policy - response.Count;
  985. if (RequestsToGo < 0) RequestsToGo = 0;
  986. }
  987. }
  988. fflush(stderr);
  989. printf("%i of %i ", requests + 1, RequestsToGo + requests + 1);
  990. displayResponse(result, &request, &response, hwid);
  991. firstRequestSent = TRUE;
  992. }
  993. }
  994. }
  995. return 0;
  996. }
  997. // Create Base KMS Client Request
  998. static void CreateRequestBase(REQUEST *Request)
  999. {
  1000. Request->MinorVer = LE16((WORD)kmsVersionMinor);
  1001. Request->MajorVer = LE16((WORD)ActiveLicensePack.kmsVersionMajor);
  1002. Request->VMInfo = LE32(VMInfo);
  1003. Request->LicenseStatus = LE32(LicenseStatus);
  1004. Request->BindingExpiration = LE32(BindingExpiration);
  1005. LEGUID(&Request->AppID, ActiveLicensePack.AppID);
  1006. LEGUID(&Request->ActID, &ActiveLicensePack.ActID);
  1007. LEGUID(&Request->KMSID, &ActiveLicensePack.KMSID);
  1008. getUnixTimeAsFileTime(&Request->ClientTime);
  1009. Request->N_Policy = LE32(ActiveLicensePack.N_Policy);
  1010. {
  1011. GUID tempGUID;
  1012. if (CMID)
  1013. {
  1014. string2UuidOrExit(CMID, &tempGUID);
  1015. LEGUID(&Request->CMID, &tempGUID);
  1016. }
  1017. else
  1018. {
  1019. get16RandomBytes(&Request->CMID);
  1020. // Set reserved UUID bits
  1021. Request->CMID.Data4[0] &= 0x3F;
  1022. Request->CMID.Data4[0] |= 0x80;
  1023. // Set UUID type 4 (random UUID)
  1024. Request->CMID.Data3 &= LE16(0xfff);
  1025. Request->CMID.Data3 |= LE16(0x4000);
  1026. }
  1027. if (CMID_prev)
  1028. {
  1029. string2UuidOrExit(CMID_prev, &tempGUID);
  1030. LEGUID(&Request->CMID_prev, &tempGUID);
  1031. }
  1032. else
  1033. {
  1034. memset(&Request->CMID_prev, 0, sizeof(Request->CMID_prev));
  1035. }
  1036. }
  1037. static const char alphanum[] = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" /*"abcdefghijklmnopqrstuvwxyz" */;
  1038. if (WorkstationName)
  1039. {
  1040. utf8_to_ucs2(Request->WorkstationName, WorkstationName, WORKSTATION_NAME_BUFFER, WORKSTATION_NAME_BUFFER * 3);
  1041. }
  1042. else if (dnsnames)
  1043. {
  1044. int len, len2;
  1045. unsigned int index = rand() % _countof(ClientDnsNames.first);
  1046. len = utf8_to_ucs2(Request->WorkstationName, ClientDnsNames.first[index], WORKSTATION_NAME_BUFFER, WORKSTATION_NAME_BUFFER * 3);
  1047. index = rand() % _countof(ClientDnsNames.second);
  1048. len2 = utf8_to_ucs2(Request->WorkstationName + len, ClientDnsNames.second[index], WORKSTATION_NAME_BUFFER, WORKSTATION_NAME_BUFFER * 3);
  1049. index = rand() % _countof(ClientDnsNames.tld);
  1050. utf8_to_ucs2(Request->WorkstationName + len + len2, ClientDnsNames.tld[index], WORKSTATION_NAME_BUFFER, WORKSTATION_NAME_BUFFER * 3);
  1051. }
  1052. else
  1053. {
  1054. unsigned int size = (rand() % 14) + 1;
  1055. const unsigned char *dummy;
  1056. unsigned int i;
  1057. for (i = 0; i < size; i++)
  1058. {
  1059. Request->WorkstationName[i] = utf8_to_ucs2_char((unsigned char*)alphanum + (rand() % (sizeof(alphanum) - 1)), &dummy);
  1060. }
  1061. Request->WorkstationName[size] = 0;
  1062. }
  1063. # ifndef NO_VERBOSE_LOG
  1064. if (verbose)
  1065. {
  1066. printf("\nRequest Parameters\n==================\n\n");
  1067. logRequestVerbose(Request, &printf);
  1068. printf("\n");
  1069. }
  1070. # endif // NO_VERBOSE_LOG
  1071. }
  1072. #endif // IS_LIBRARY