Esempio n. 1
0
    def get_msg(self):
        while True:
            msg, address = self.s.recvfrom(config.buffer_size)
            data = json.loads(msg.decode('utf-8'))
            # print(data)

            if data['query'] == 'user_request':
                app_package = json.loads(
                    config.bin_list_to_string(
                        self.des.decrypt(data['app_package'],
                                         config.string_to_bin_list(self.KEY))))
                print("GET app_package" + "['uid': {},"
                      "'address': {},"
                      "'ts': {},"
                      "'exp_ts': {}, "
                      "app_session_key]".format(
                          app_package['uid'], app_package['address'],
                          app_package['ts'], app_package['exp_ts'],
                          app_package['app_session_key']))

                user_package = json.loads(
                    config.bin_list_to_string(
                        self.des.decrypt(data['user_package'],
                                         app_package['app_session_key'])))
                print("GET user data" + "['uid': {}, 'ts': {}]\n".format(
                    user_package['uid'], user_package['ts']))

                if user_package['uid'] != app_package['uid']:
                    raise RuntimeError(
                        "uid of the tgt package and user package don't match")

                print(
                    "SEND " +
                    "['name': 'APP1', 'ts': {}]".format(str(int(time.time()))))
                app_response = {
                    'query':
                    'app_response',
                    'app_response':
                    self.des.encrypt(
                        config.json_to_bin_list({
                            'name': 'APP1',
                            'ts': str(int(time.time()))
                        }), app_package['app_session_key'])
                }
                self.s.sendto(str.encode(json.dumps(app_response)), address)
Esempio n. 2
0
    return file.read()


if __name__ == '__main__':
    data = read_from_file('input.txt')
    KEY = '1234567'
    KEY2 = 'KILLAL'
    print(COLOR.FAIL + 'DOUBLE DES' + COLOR.ENDC)
    double_des = DoubleDes(KEY, KEY2)
    double_des.encrypt_and_decrypt(data, True)

    print('\n' + COLOR.FAIL + 'TRIPLE DES' + COLOR.ENDC)
    triple_des = TripleDes(KEY, KEY2)
    triple_des.encrypt_and_decrypt(data, True)

    print('\n' + COLOR.FAIL + 'GOST' + COLOR.ENDC)
    KEY3 = 'FKJFLKEJF1234563FEFNWNKLQL135676'
    gost = GOST()

    enc = gost.encrypt(string_to_bin_list(data), string_to_bin_list(KEY3))

    dec = gost.decrypt(enc, string_to_bin_list(KEY3))
    print('KEY: ' + COLOR.OKBLUE + KEY3 + COLOR.ENDC)
    print('Text: ' + COLOR.HEADER + data + COLOR.ENDC)
    print('Encoded string: ' + COLOR.HEADER + bin_list_to_string(enc) + COLOR.ENDC)
    print(COLOR.ENDC + 'Decoded string: ' + COLOR.HEADER + bin_list_to_string(dec) + COLOR.ENDC)




Esempio n. 3
0
    def get_and_send(self):
        while True:
            json_data, address = self.s.recvfrom(
                config.buffer_size)  # receive data from client
            data = dict()
            if json_data and address:
                data = json.loads(json_data.decode('utf-8'))
                # print(address, data)
            else:
                continue
            if data['query'] == 'tgs_request':
                tgt_data = json.loads(
                    config.bin_list_to_string(
                        self.des.decrypt(data['tgt_package'],
                                         config.string_to_bin_list(TGS_KEY))))

                session_key = tgt_data['session_key']
                user_data = json.loads(
                    config.bin_list_to_string(
                        self.des.decrypt(data['my_package'], session_key)))
                if self.show_bin:
                    print("TGS GETS TGT " + "['uid': {},"
                          "'ts': {},"
                          "'session_key': {},"
                          "'exp_ts': {}]".format(tgt_data['uid'],
                                                 tgt_data['ts'], session_key,
                                                 tgt_data['exp_ts']))
                else:
                    print("TGS GETS TGT " + "['uid': {},"
                          "'ts': {},"
                          "'session_key': {},"
                          "'exp_ts': {}]".format(
                              tgt_data['uid'], tgt_data['ts'],
                              config.bin_list_to_string(session_key),
                              tgt_data['exp_ts']))

                if tgt_data['uid'] != user_data['uid']:
                    raise RuntimeError(
                        "uid of the tgt package and user package don't match")

                key = ''.join(
                    random.choices(string.ascii_uppercase + string.digits,
                                   k=7))
                app_session_key = config.string_to_bin_list(key)

                app_ans = {
                    'uid': user_data['uid'],
                    'address': address,
                    'ts': tgt_data['ts'],
                    'exp_ts': tgt_data['exp_ts'],
                    'app_session_key': app_session_key
                }
                user_ans = {
                    'dest': 'APP1',
                    'ts': tgt_data['ts'],
                    'exp_ts': tgt_data['exp_ts'],
                    'app_session_key': app_session_key
                }

                if self.show_bin:
                    print("TGS SEND "
                          "['uid': {},"
                          "'address': {},"
                          "'ts': {},"
                          "'exp_ts': {}, "
                          "app_session_key]\n".format(
                              user_data['uid'], address, tgt_data['ts'],
                              tgt_data['exp_ts'], app_session_key) +
                          "TGS SEND  "
                          "['dest': APP1,"
                          "'ts': {},"
                          "'exp_ts': {}"
                          "'app_session_key': {}]".format(
                              tgt_data['ts'], tgt_data['exp_ts'],
                              app_session_key))
                else:
                    print("TGS SEND "
                          "['uid': {},"
                          "'address': {},"
                          "'ts': {},"
                          "'exp_ts': {}, "
                          "app_session_key]\n".format(
                              user_data['uid'], address, tgt_data['ts'],
                              tgt_data['exp_ts'], key) + "TGS SEND  "
                          "['dest': APP1,"
                          "'ts': {},"
                          "'exp_ts': {}, "
                          "'app_session_key': {}]".format(
                              tgt_data['ts'], tgt_data['exp_ts'], key))

                ans = {
                    'query':
                    'tgt_response',
                    'user_package':
                    self.des.encrypt(config.json_to_bin_list(user_ans),
                                     session_key),
                    'app_package':
                    self.des.encrypt(config.json_to_bin_list(app_ans),
                                     config.string_to_bin_list(SERVER_KEY))
                }
                self.s.sendto(str.encode(json.dumps(ans)), address)
Esempio n. 4
0
    def get_and_send(self):
        while True:
            json_data, address = self.s.recvfrom(
                config.buffer_size)  # receive data from client
            data = dict()
            if json_data and address:
                data = json.loads(json_data.decode('utf-8'))
                # print(address, data)
            else:
                continue
            if data['query'] == 'first_time_package':
                uid = data['uid']
                enc_ts = data['ts']
                attempt = data['attempt']

                if uid not in self.all_uid_and_hashes:
                    ans_package = {
                        'query': 'no_such_user',
                        'msg': 'no user with such username',
                    }
                    self.s.sendto(str.encode(json.dumps(ans_package)), address)
                    continue

                uid_key = config.string_to_bin_list(
                    self.all_uid_and_hashes[uid])

                try:
                    dec_ts = int(
                        config.bin_list_to_string(
                            self.des.decrypt(enc_ts, uid_key)))
                    if self.show_bin:
                        print("AS GETS " + "['query': 'first_time_package', "
                              "'ts': {}, 'uid': {}, 'attempt': {}]".format(
                                  enc_ts, uid, attempt))
                    else:
                        print("AS GETS " + "['query': 'first_time_package', "
                              "'ts': {}, 'uid': {}, 'attempt': {}]".format(
                                  dec_ts, uid, attempt))
                except RuntimeError:
                    ans_package = {
                        'query': 'hacking_attempt',
                        'msg': 'invalid password',
                        'attempt': attempt
                    }

                    self.s.sendto(str.encode(json.dumps(ans_package)), address)
                    continue
                except ValueError:
                    ans_package = {
                        'query': 'hacking_attempt',
                        'msg': 'you are not {}'.format(uid),
                        'attempt': attempt
                    }

                    self.s.sendto(str.encode(json.dumps(ans_package)), address)
                    continue

                if dec_ts and time.time() - dec_ts > 300:
                    ans_package = {
                        'query': 'hacking_attempt',
                        'msg': 'you are not {}'.format(uid)
                    }
                    self.s.sendto(str.encode(json.dumps(ans_package)), address)
                    continue

                session_key = ''.join(
                    random.choices(string.ascii_uppercase + string.digits,
                                   k=7))
                self.session_bin_key = config.string_to_bin_list(session_key)
                ts = int(time.time())
                exp_ts = ts + 28800
                ans = {
                    'session_key': self.session_bin_key,
                    'ts': ts,
                    'exp_ts': exp_ts,
                    'TGS_addr': (config.IP, config.TGS_PORT)
                }
                enc_ans = self.des.encrypt(config.json_to_bin_list(ans),
                                           uid_key)

                # part with tgt
                tgt = {
                    'uid': uid,
                    'ts': ts,
                    'session_key': self.session_bin_key,
                    'exp_ts': exp_ts
                }
                enc_tgt = self.des.encrypt(config.json_to_bin_list(tgt),
                                           config.string_to_bin_list(TGS_KEY))

                ans_package = {
                    'query': 'first_time_and_tgt_package',
                    'first_time_json_enc': enc_ans,
                    'tgt_json_enc': enc_tgt
                }

                if self.show_bin:
                    print("AS SENDS session_key " + "['session_key': {},"
                          "'ts': {},"
                          "'exp_ts': {},"
                          "'TGS_addr': {}]\n".format(
                              self.session_bin_key, ts, exp_ts,
                              (config.IP, config.TGS_PORT)) + "SEND TGT "
                          "['uid': {},"
                          "'ts': {},"
                          "'session_key': {},"
                          "'exp_ts': {}]".format(uid, ts, session_key, exp_ts))
                else:
                    print("AS SENDS session_key " + "['session_key': {},"
                          "'ts': {},"
                          "'exp_ts': {},"
                          "'TGS_addr': {}]\n".format(
                              session_key, ts, exp_ts,
                              [config.IP, config.TGS_PORT]) + "SEND TGT " +
                          "['uid': {},"
                          "'ts': {},"
                          "'session_key': {},"
                          "'exp_ts': {}]".format(uid, ts, session_key, exp_ts))

                self.s.sendto(str.encode(json.dumps(ans_package)), address)
Esempio n. 5
0
    def get_msg(self):
        while True:
            msg, address = self.s.recvfrom(config.buffer_size)
            data = json.loads(msg.decode('utf-8'))
            # print(data)

            if data['query'] == 'first_time_and_tgt_package':
                enc_ans = data['first_time_json_enc']
                dec_ans = self.des.decrypt(enc_ans, self.bin_key)

                ans = json.loads(config.bin_list_to_string(dec_ans))
                self.session_bin_key = ans['session_key']
                tgs_address = ans['TGS_addr']

                self.TGT_package = data['tgt_json_enc']
                if self.show_bin:
                    print("GET session key {} and save TGT package".format(
                        self.session_bin_key))
                else:
                    print("GET session key {} and save TGT package".format(
                        config.bin_list_to_string(self.session_bin_key)))

                my_package = {'uid': self.name, 'ts': ans['ts']}

                print(
                    "SEND ['uid': {}, 'ts': {}]".format(self.name, ans['ts']) +
                    " and TGT package")
                enc_my_package = self.des.encrypt(
                    config.json_to_bin_list(my_package), self.session_bin_key)

                full_package = {
                    'query': 'tgs_request',
                    'my_package': enc_my_package,
                    'tgt_package': self.TGT_package
                }
                self.s.sendto(str.encode(json.dumps(full_package)),
                              tuple(tgs_address))
            elif data['query'] == 'tgt_response':
                user_package = json.loads(
                    config.bin_list_to_string(
                        self.des.decrypt(data['user_package'],
                                         self.session_bin_key)))
                self.app_session_key = user_package['app_session_key']
                self.app_package = data['app_package']

                print("GET app_session_key and save app_package")

                package_to_app = {
                    'query':
                    'user_request',
                    'user_package':
                    self.des.encrypt(
                        config.json_to_bin_list({
                            'uid': self.name,
                            'ts': user_package['ts'],
                        }), self.app_session_key),
                    'app_package':
                    self.app_package
                }

                print("SEND app_package and ['uid': {}, 'ts': {}]".format(
                    self.name, user_package['ts']))

                self.s.sendto(str.encode(json.dumps(package_to_app)),
                              (config.IP, config.SERVER_PORT))
            elif data['query'] == 'app_response':
                app_response = json.loads(
                    config.bin_list_to_string(
                        self.des.decrypt(data['app_response'],
                                         self.app_session_key)))
                print("GET response from app with name {}".format("APP1"))
                # print(app_response['name'], app_response['ts'])
                exit(0)
            elif data['query'] == 'hacking_attempt':
                # print(data['msg'])
                if data['attempt'] == 3:
                    print('it was last attempt')
                    exit(0)
                print('try again. attempt {}'.format(data['attempt']))
                print('password: '******' ')
                self.password = input()

                hash_ = hashlib.md5(self.password.encode())
                self.KEY = hash_.hexdigest()[:7]
                self.bin_key = config.string_to_bin_list(self.KEY)

                ts = config.string_to_bin_list(str(int(time.time())))

                package = {
                    'query': 'first_time_package',
                    'ts': self.des.encrypt(ts, self.bin_key),
                    'uid': self.name,
                    'attempt': data['attempt'] + 1
                }
                self.s.sendto(str.encode(json.dumps(package)),
                              (config.IP, config.AS_PORT))
            elif data['query'] == 'no_such_user':
                print(data['msg'])
                exit(0)