vlmcs.c 38 KB

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