def parseRequest(self): request = MSRPCHeader(self.data) ShellMessage.Process(3).run() request = byterize(request) loggersrv.debug("RPC Bind Request Bytes: \n%s\n" % justify(deco(binascii.b2a_hex(self.data), 'utf-8'))) loggersrv.debug( "RPC Bind Request: \n%s\n%s\n" % (justify(request.dump(print_to_stdout=False)), justify( MSRPCBind(request['pduData']).dump(print_to_stdout=False)))) return request
def generateRequest(self): firstCtxItem = CtxItem() firstCtxItem['ContextID'] = 0 firstCtxItem['TransItems'] = 1 firstCtxItem['Pad'] = 0 firstCtxItem['AbstractSyntaxUUID'] = uuid.UUID( '51c82175-844e-4750-b0d8-ec255555bc06').bytes_le firstCtxItem['AbstractSyntaxVer'] = 1 firstCtxItem['TransferSyntaxUUID'] = uuidNDR32.bytes_le firstCtxItem['TransferSyntaxVer'] = 2 secondCtxItem = CtxItem() secondCtxItem['ContextID'] = 1 secondCtxItem['TransItems'] = 1 secondCtxItem['Pad'] = 0 secondCtxItem['AbstractSyntaxUUID'] = uuid.UUID( '51c82175-844e-4750-b0d8-ec255555bc06').bytes_le secondCtxItem['AbstractSyntaxVer'] = 1 secondCtxItem['TransferSyntaxUUID'] = uuidTime.bytes_le secondCtxItem['TransferSyntaxVer'] = 1 bind = MSRPCBind() bind['max_tfrag'] = 5840 bind['max_rfrag'] = 5840 bind['assoc_group'] = 0 bind['ctx_num'] = 2 bind['ctx_items'] = str( bind.CtxItemArray(str(firstCtxItem) + str(secondCtxItem))) request = MSRPCHeader() request['ver_major'] = 5 request['ver_minor'] = 0 request['type'] = self.packetType['bindReq'] request['flags'] = self.packetFlags['firstFrag'] | self.packetFlags[ 'lastFrag'] | self.packetFlags['multiplex'] request['call_id'] = self.srv_config['call_id'] request['pduData'] = str(bind) ShellMessage.Process(0).run() bind = byterize(bind) request = byterize(request) loggersrv.debug( "RPC Bind Request: \n%s\n%s\n" % (justify(request.dump(print_to_stdout=False)), justify( MSRPCBind(request['pduData']).dump(print_to_stdout=False)))) loggersrv.debug("RPC Bind Request Bytes: \n%s\n" % justify( deco(binascii.b2a_hex(enco(str(request), 'latin-1')), 'utf-8'))) return request
def handle(self): self.request.settimeout(srv_config['timeoutsndrcv']) while True: # self.request is the TCP socket connected to the client try: self.data = self.request.recv(1024) if self.data == '' or not self.data: pretty_printer( log_obj=loggersrv.warning, put_text="{reverse}{yellow}{bold}No data received.{end}" ) break except socket.error as e: pretty_printer( log_obj=loggersrv.error, put_text="{reverse}{red}{bold}While receiving: %s{end}" % str(e)) break packetType = MSRPCHeader(self.data)['type'] if packetType == rpcBase.packetType['bindReq']: loggersrv.info("RPC bind request received.") pretty_printer(num_text=[-2, 2], where="srv") handler = pykms_RpcBind.handler(self.data, srv_config) elif packetType == rpcBase.packetType['request']: loggersrv.info("Received activation request.") pretty_printer(num_text=[-2, 13], where="srv") handler = pykms_RpcRequest.handler(self.data, srv_config) else: pretty_printer( log_obj=loggersrv.error, put_text= "{reverse}{red}{bold}Invalid RPC request type %s.{end}" % packetType) break res = enco(str(handler.populate()), 'latin-1') if packetType == rpcBase.packetType['bindReq']: loggersrv.info("RPC bind acknowledged.") pretty_printer(num_text=[-3, 5, 6], where="srv") elif packetType == rpcBase.packetType['request']: loggersrv.info("Responded to activation request.") pretty_printer(num_text=[-3, 18, 19], where="srv") try: self.request.send(res) if packetType == rpcBase.packetType['request']: break except socket.error as e: pretty_printer( log_obj=loggersrv.error, put_text="{reverse}{red}{bold}While sending: %s{end}" % str(e)) break
def handle(self): while True: # self.request is the TCP socket connected to the client try: self.data = self.request.recv(1024) except socket.error as e: if e.errno == errno.ECONNRESET: loggersrv.error("Connection reset by peer.") break else: raise if self.data == '' or not self.data: loggersrv.warning("No data received !") break packetType = MSRPCHeader(self.data)['type'] if packetType == rpcBase.packetType['bindReq']: loggersrv.info("RPC bind request received.") ShellMessage.Process([-2, 2]).run() handler = pykms_RpcBind.handler(self.data, srv_config) elif packetType == rpcBase.packetType['request']: loggersrv.info("Received activation request.") ShellMessage.Process([-2, 13]).run() handler = pykms_RpcRequest.handler(self.data, srv_config) else: loggersrv.error("Invalid RPC request type ", packetType) break res = enco(str(handler.populate()), 'latin-1') self.request.send(res) if packetType == rpcBase.packetType['bindReq']: loggersrv.info("RPC bind acknowledged.") ShellMessage.Process([-3, 5, 6]).run() elif packetType == rpcBase.packetType['request']: loggersrv.info("Responded to activation request.") ShellMessage.Process([-3, 18, 19]).run() break
def client_create(clt_sock): binder = pykms_RpcBind.handler(None, clt_config) RPC_Bind = enco(str(binder.generateRequest()), 'latin-1') try: loggerclt.info("Sending RPC bind request...") pretty_printer(num_text = [-1, 1], where = "clt") clt_sock.send(RPC_Bind) except socket.error as e: pretty_printer(log_obj = loggerclt.error, to_exit = True, where = "clt", put_text = "{reverse}{red}{bold}While sending: %s. Exiting...{end}" %str(e)) try: bindResponse = clt_sock.recv(1024) if bindResponse == '' or not bindResponse: pretty_printer(log_obj = loggerclt.warning, to_exit = True, where = "clt", put_text = "{reverse}{yellow}{bold}No data received. Exiting...{end}") pretty_printer(num_text = [-4, 7], where = "clt") except socket.error as e: pretty_printer(log_obj = loggerclt.error, to_exit = True, where = "clt", put_text = "{reverse}{red}{bold}While receiving: %s. Exiting...{end}" %str(e)) packetType = MSRPCHeader(bindResponse)['type'] if packetType == rpcBase.packetType['bindAck']: loggerclt.info("RPC bind acknowledged.") pretty_printer(num_text = 8, where = "clt") kmsRequest = createKmsRequest() requester = pykms_RpcRequest.handler(kmsRequest, clt_config) try: loggerclt.info("Sending RPC activation request...") RPC_Actv = enco(str(requester.generateRequest()), 'latin-1') pretty_printer(num_text = [-1, 12], where = "clt") clt_sock.send(RPC_Actv) except socket.error as e: pretty_printer(log_obj = loggerclt.error, to_exit = True, where = "clt", put_text = "{reverse}{red}{bold}While sending: %s. Exiting...{end}" %str(e)) try: response = clt_sock.recv(1024) pretty_printer(num_text = [-4, 20], where = "clt") except socket.error as e: pretty_printer(log_obj = loggerclt.error, to_exit = True, where = "clt", put_text = "{reverse}{red}{bold}While receiving: %s. Exiting...{end}" %str(e)) loggerclt.debug("Response: \n%s\n" % justify(deco(binascii.b2a_hex(response), 'latin-1'))) parsed = MSRPCRespHeader(response) kmsData = readKmsResponse(parsed['pduData'], kmsRequest, clt_config) kmsResp = kmsData['response'] try: hwid = kmsData['hwid'] loggerclt.info("KMS Host HWID: %s" % deco(binascii.b2a_hex(enco(hwid, 'latin-1')).upper(), 'utf-8')) except KeyError: pass loggerclt.info("KMS Host ePID: %s" % kmsResp['kmsEpid'].encode('utf-8').decode('utf-16le')) loggerclt.info("KMS Host Current Client Count: %s" % kmsResp['currentClientCount']) loggerclt.info("KMS VL Activation Interval: %s" % kmsResp['vLActivationInterval']) loggerclt.info("KMS VL Renewal Interval: %s" % kmsResp['vLRenewalInterval']) if clt_config['loglevel'] == 'MININFO': loggerclt.mininfo("", extra = {'host': str(clt_sock.getpeername()), 'status' : kmsBase.licenseStates[requester.srv_config['KMSClientLicenseStatus']], 'product' : clt_config["mode"]}) pretty_printer(num_text = 21, where = "clt") elif packetType == rpcBase.packetType['bindNak']: loggerclt.info(justify(MSRPCBindNak(bindResponse).dump(print_to_stdout = False))) sys.exit(0) else: pretty_printer(log_obj = loggerclt.warning, to_exit = True, where = "clt", put_text = "{reverse}{magenta}{bold}Something went wrong. Exiting...{end}")
def client_create(): loggerclt.info("Connecting to %s on port %d..." % (clt_config['ip'], clt_config['port'])) s = socket.create_connection((clt_config['ip'], clt_config['port'])) loggerclt.info("Connection successful !") binder = pykms_RpcBind.handler(None, clt_config) RPC_Bind = enco(str(binder.generateRequest()), 'latin-1') loggerclt.info("Sending RPC bind request...") ShellMessage.Process([-1, 1]).run() s.send(RPC_Bind) try: ShellMessage.Process([-4, 7]).run() bindResponse = s.recv(1024) except socket.error as e: if e.errno == errno.ECONNRESET: loggerclt.error("Connection reset by peer. Exiting...") sys.exit() else: raise if bindResponse == '' or not bindResponse: loggerclt.error("No data received ! Exiting...") sys.exit() packetType = MSRPCHeader(bindResponse)['type'] if packetType == rpcBase.packetType['bindAck']: loggerclt.info("RPC bind acknowledged.") ShellMessage.Process(8).run() kmsRequest = createKmsRequest() requester = pykms_RpcRequest.handler(kmsRequest, clt_config) s.send(enco(str(requester.generateRequest()), 'latin-1')) ShellMessage.Process([-1, 12]).run() response = s.recv(1024) loggerclt.debug("Response: \n%s\n" % justify(deco(binascii.b2a_hex(response), 'latin-1'))) ShellMessage.Process([-4, 20]).run() parsed = MSRPCRespHeader(response) kmsData = readKmsResponse(parsed['pduData'], kmsRequest, clt_config) kmsResp = kmsData['response'] try: hwid = kmsData['hwid'] loggerclt.info( "KMS Host HWID: %s" % deco(binascii.b2a_hex(enco(hwid, 'latin-1')).upper(), 'utf-8')) except KeyError: pass loggerclt.info("KMS Host ePID: %s" % kmsResp['kmsEpid'].encode('utf-8').decode('utf-16le')) loggerclt.info("KMS Host Current Client Count: %s" % kmsResp['currentClientCount']) loggerclt.info("KMS VL Activation Interval: %s" % kmsResp['vLActivationInterval']) loggerclt.info("KMS VL Renewal Interval: %s" % kmsResp['vLRenewalInterval']) if clt_config['loglevel'] == 'MINI': loggerclt.mini("", extra={ 'host': socket.gethostname() + " [" + clt_config["ip"] + "]", 'status': "Activated", 'product': clt_config["mode"] }) ShellMessage.Process(21).run() elif packetType == rpcBase.packetType['bindNak']: loggerclt.info( justify(MSRPCBindNak(bindResponse).dump(print_to_stdout=False))) sys.exit() else: loggerclt.critical("Something went wrong.") sys.exit()