vlmcs.c 37 KB

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