def get_output(exp): """ Adds the output property to the parameters of the type Function, to get the executed value, otherwise the object itself. :exp - rvalue of the expression. """ _mod = ast.parse(exp, "<string>", "exec") if isinstance(_mod.body[0].value, ast.Call): if util.assemble(_mod.body[0].value.func) in dir(cf): return f"{exp}.output" return util.assemble(exp)
def parse_if_expression(exp): """ Parses an AST expression recursively to reduce the if-else statements. :exp - an AST object from a string of Python expression. """ if isinstance(exp, ast.IfExp): _cond = util.assemble(exp.test) _test = f"VARIABLE = {_cond}" exec(compile(_test, "<string>", "exec"), globals()) if VARIABLE: VALUE = util.assemble(exp.body) else: VALUE = parse_if_expression(exp.orelse) else: VALUE = util.assemble(exp) return VALUE
def auth(): request = util.assemble(_ClientID, _ServerID, _KDCID) print "Connecting to AS..." try: Client_key_encrypt, KDC_key_encrypt, Client_info = ConnectTGS(request) print "Disconnect with AS!" except Exception, e: print "[!!!]ERROR: ", e return
def addTo(self, client): try: assemble_key = util.assemble(str(public_key.n), str(public_key.e)) client.send(assemble_key) client_public_key = client.recv(self.BUFSIZ) client_info_encrypt = client.recv(self.BUFSIZ) client_info_signature = client.recv(self.BUFSIZ) n, e = util.disassemble(client_public_key) client_key = rsa.PublicKey(int(n), int(e)) rsa.verify(client_info_encrypt, client_info_signature, client_key) client_info = rsa.decrypt(client_info_encrypt, private_key) client_ID, KDCID, timestamp, reverse, client_decrypt = util.disassemble(client_info) except Exception, e: print '[!!!]ERROR :', e self.error_log('KC addTo decrypt', e) client.close() return
def generate_wl(pid, op): records = extract_data(pid) if not records: records = extract_data2(pid) if not records: print('not records for {}'.format(pid)) return def result(record): v = record['DepthToWaterBGS'] if v is not None: return round(v, 2) location_name = 'NMWDI-$autoinc' thingid = pid try: obj = assemble( location_name, thingid, records, { 'n': 'NorthingGG', 'e': 'EastingGG', 'srid': 'SRID', 'time': 'DateTimeMeasured', 'result': result, 'sensor': make_sensor, 'location_properties': make_location_properties, 'observed_property_description': OP_DESCRIPTION, 'datastream_description': DS_DESCRIPTION, 'thing_properties': make_thing_properties }) obj['destination'] = 'https://st.newmexicowaterdata.org/FROST-Server/v1.1' with open(op, 'w') as wfile: json.dump(obj, wfile, indent=2) print('wrote {}. nrecords={}'.format(op, len(records))) return True except BaseException as e: print('Failed making {}. {}'.format(op, e))
_KDCAddr = "172.18.187.103" _KCPORT = 3210 BUFSIZ = 1024 (pub, pri) = rsa.newkeys(1024) ADDR = (_KDCAddr, _KCPORT) sock = socket(AF_INET, SOCK_STREAM) sock.connect(ADDR) sock.send("000") assemble_key = sock.recv(BUFSIZ) n, e = util.disassemble(str(assemble_key)) public_key = rsa.PublicKey(int(n), int(e)) assemble_key = util.assemble(str(pub.n), str(pub.e)) sock.send(assemble_key) message = "abcdefgh" timestamp = str(int(time.time())) info = util.assemble("2222", "10385011", timestamp, 1, message) crypto = rsa.encrypt(info, public_key) sock.send(crypto) time.sleep(0.1) si = rsa.sign(crypto, pri, "SHA-1") sock.send(si) des_key = des(message, CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5) timestamp_encrypt = sock.recv(BUFSIZ) recv_timestamp = des_key.decrypt(timestamp_encrypt)
key += chr(piece) return key new_key = get_session_key() (pub, pri) = rsa.newkeys(1024) ADDR = (_KDCAddr, _KCPORT) sock = socket(AF_INET, SOCK_STREAM) sock.connect(ADDR) sock.send('010') assemble_key = sock.recv(BUFSIZ) n, e = util.disassemble(str(assemble_key)) public_key = rsa.PublicKey(int(n), int(e)) assemble_key = util.assemble(str(pub.n), str(pub.e)) sock.send(assemble_key) timestamp = str(int(time.time())) info = util.assemble('1111', '10385011', timestamp, 1, old_key, new_key) encrypt_info = rsa.encrypt(info, public_key) sock.send(encrypt_info) time.sleep(0.1) si = rsa.sign(encrypt_info, pri, 'SHA-1') sock.send(si) des_key = des(new_key, CBC, '\0\0\0\0\0\0\0\0', pad=None, padmode=PAD_PKCS5) timestamp_encrypt = sock.recv(BUFSIZ) recv_timestamp = des_key.decrypt(timestamp_encrypt) try:
if len(old_key) != 8 or (new_key is not None and len(new_key) != 8): print '[!!!]FATAL: invalid des key' except Exception, e: print '[!!!]ERROR: des key illegal' return False if new_key is None: sock.send('000') else: sock.send('010') try: assemble_key = sock.recv(BUFSIZ) n, e = util.disassemble(str(assemble_key)) public_key = rsa.PublicKey(int(n), int(e)) assemble_key = util.assemble(str(pub.n), str(pub.e)) sock.send(assemble_key) except Exception, e: print '[!!!]ERROR: exchange public key failed!' return False try: timestamp = str(int(time.time())) if new_key is None: client_info = util.assemble(clientID, KDCID, timestamp, str(reverse), old_key) else: client_info = util.assemble(clientID, KDCID, timestamp, str(reverse), old_key, new_key) print client_info crypto = rsa.encrypt(client_info, public_key) sock.send(crypto) time.sleep(0.1)
def ConnectServer(client_encrypt, server_encrypt, server_encrypt_client_info, reverse_auth=1): session_key = Client_k.decrypt(client_encrypt) session_k = des(session_key, CBC, '\0\0\0\0\0\0\0\0', pad = None, padmode=PAD_PKCS5) ADDR = (_ServerAddr, _ServerPORT) ClientSocket = socket(AF_INET, SOCK_STREAM) try: ClientSocket.connect(ADDR) print 'Connect to server', ADDR, '...' except Exception, e: print '[!!!]ERROR: ', e return timestamp = str(int(time.time())) request = util.assemble(_ClientID, _ServerID, _KDCID, timestamp, str(reverse_auth)) encrypt_request = session_k.encrypt(request) try: ClientSocket.send(server_encrypt) time.sleep(0.1) ClientSocket.send(encrypt_request) time.sleep(0.1) ClientSocket.send(server_encrypt_client_info) except Exception, e: print '[!!!]ERROR: ', e return if reverse_auth: encrypt_check = ClientSocket.recv(BUFSIZ) check = session_k.decrypt(encrypt_check)