-
-
Save rqu1/28eede91348990075229b72681016ece to your computer and use it in GitHub Desktop.
Simple py2 implementation of PAN-OS masterkey encryption/decryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from hashlib import md5, sha1 | |
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | |
from cryptography.hazmat.backends import default_backend | |
from base64 import b64encode, b64decode | |
class PanCrypt(): | |
def __init__(self, key='p1a2l3o4a5l6t7o8'): | |
backend=default_backend() | |
key=self._derivekey(key) | |
self.c=Cipher(algorithms.AES(key), modes.CBC('\0'*16), backend=backend) | |
def _derivekey(self,key): | |
return md5(key+md5("pannetwork").digest()).digest()*2 | |
def _pad(self, d): | |
plen=16-(len(d)%16) | |
return d+(chr(plen)*plen) | |
def _unpad(self,d): | |
return d[:-(ord(d[-1]))] | |
def _encrypt(self,data): | |
e=self.c.encryptor() | |
return e.update(self._pad(data)) + e.finalize() | |
def _decrypt(self,data): | |
d=self.c.decryptor() | |
return self._unpad(d.update(data)+d.finalize()) | |
def encrypt(self, data): | |
v=b64encode('\x01') | |
hash=b64encode(sha1(data).digest()) | |
ct=b64encode(self._encrypt(data)) | |
return '-'+v+hash+ct | |
def decrypt(self, data): | |
v=b64decode(data[1:5]) | |
hash=b64decode(data[5:33]) | |
pt=self._decrypt(b64decode(data[33:])) | |
return (v,hash,pt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
encryption:
verifying the ciphertext string matches the ciphertext returned by cryptod:
decrypting data:
this tuple contains the version code (should be \x01 in valid ciphertexts), the SHA-1 hash of the plaintext, and the plaintext.