示例#1
0
class User:
    def __init__(self):
        self.user = None
        self.filename = None
        self.cipher = None
        self.content = None

    def get_cipher(self):
        return self.cipher

    def get_username(self):
        return self.user

    def get_filename(self):
        return self.filename

    def update_content(self, new_content):
        self.content = new_content

    def new_account(self, user, pwd, custom_file=None):
        self.cipher = AESCipher(user, pwd)
        self.content = '{"username":"******", "accounts":{}}'

        if custom_file:
            self.filename = custom_file
        else:
            self.filename = "account_" + user

        encrypted = self.cipher.encrypt(self.content)

        with open(self.filename, 'wb') as f:
            f.write(encrypted)

    def existing_account(self, user, pwd, custom_file=None):
        self.user = user
        self.cipher = AESCipher(user, pwd)

        if custom_file:
            self.filename = custom_file
        else:
            self.filename = "account_" + user

        with open(self.filename, 'rb') as f:
            data = f.read()

        try:
            self.content = json.loads(self.cipher.decrypt(data))
            return True
        except json.decoder.JSONDecodeError:
            print("Decryption failed.")
            return False
示例#2
0
server.connect((IP_address, Port))
SEND(hashlib.sha256(RECV()).hexdigest())
if RECV() != "AUTHORISED":
    server.close()
    sys.exit()

SEND("OUTPUT")
os.system("title Output " + sys.argv[2])

SEND(sys.argv[1] + " " + sys.argv[2])
if RECV() != "ACCEPTED":
    server.close()
    sys.exit()

key = AESCipher(sys.argv[3])

while True:
    message = RECV()
    if message == "EXIT":
        server.close()
        print "Logged off"
        sys.exit()
    elif message == "DODGE":
        server.close()
        print sys.argv[2] + " has disconnected"
        os.system("pause")
        sys.exit()
    message = key.decrypt(message)
    print message

server.close()
示例#3
0
def acknowledge_token(request, token):
    """Acknowledge a host or service alert using an encrypted token."""
    time_now = time.time()
    cipher = AESCipher('ABCDEF0123456789', iv='iv1234567890ABCD')
    host_command_line = "COMMAND [{timestamp}] {command};" \
                        "{hostname};" \
                        "{sticky};" \
                        "{notify};" \
                        "{persistent};" \
                        "{author};" \
                        "Ack by email, working on it."
    svc_command_line = "COMMAND [{timestamp}] {command};" \
                       "{hostname};" \
                       "{service_description};" \
                       "{sticky};" \
                       "{notify};" \
                       "{persistent};" \
                       "{author};" \
                       "Ack by email, working on it."

    # Try to decode the encrypted token to a python object (dict)
    try:
        token = str(token)
        json_token = cipher.decrypt(urlsafe_b64decode(token))
        ack_data = json.loads(json_token)
    except:
        logger.exception("Unable to decrypt the provided token !")
        logger.debug("Token received: %s", token)
        return HttpResponse('Token is not valid !\n', status=400)

    # Check token validity in time
    if time_now > ack_data['expire_time']:
        if 'service_description' in ack_data:
            logger.warning(
                "Token validity for service alert \"%s / %s\" has expired !",
                ack_data['hostname'], ack_data['service_description'])
        else:
            logger.warning(
                "Token validity for host alert \"%s\" has expired !",
                ack_data['hostname'])
        return render(request, 'nagios/ack_email_expired.html', ack_data)

    # Send the ack command to Nagios
    if 'service_description' in ack_data:
        command_line = svc_command_line.format(timestamp=time_now, **ack_data)
    else:
        command_line = host_command_line.format(timestamp=time_now, **ack_data)

    # Establish a connection to satellites and send the command to ack
    try:
        satellites = Satellite.live_connect()
        for conn in satellites.connections:
            site = conn[0]
            satellites.command(command_line, sitename=site)
    except Satellite.SatelliteConnectError as e:
        logger.exception('Error connecting on satellites !')
        return HttpResponse('Unable to connect to Nagios.\n'
                            'Error: {}\n'.format(e),
                            status=400)

    logger.info("Processed ack by email: %s", command_line)

    return render(request, 'nagios/ack_email_passed.html', ack_data)