crypter.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-"
  3. """
  4. This file is part of the UFONet project, https://ufonet.03c8.net
  5. Copyright (c) 2013/2020 | psy <epsylon@riseup.net>
  6. You should have received a copy of the GNU General Public License along
  7. with UFONet; if not, write to the Free Software Foundation, Inc., 51
  8. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  9. """
  10. ###################################################################
  11. # Code extracted from project: AnonTwi (anontwi.03c8.net)
  12. ###################################################################
  13. KEY_SIZE = 32
  14. BLOCK_SIZE = 16
  15. MAC_SIZE = 20
  16. import base64
  17. from os import urandom
  18. from hashlib import sha1, sha256
  19. from Crypto.Cipher import AES
  20. trans_5C = ''.join([chr (x ^ 0x5c) for x in range(256)])
  21. trans_36 = ''.join([chr (x ^ 0x36) for x in range(256)])
  22. trans_5C = trans_5C.encode("latin-1")
  23. trans_36 = trans_36.encode("latin-1")
  24. def hmac_sha1(key, msg):
  25. if len(key) > 20:
  26. key = sha1(key).digest()
  27. key += chr(0).encode('utf-8') * (20 - len(key))
  28. o_key_pad = key.translate(trans_5C)
  29. i_key_pad = key.translate(trans_36)
  30. return sha1(o_key_pad + sha1(i_key_pad + msg).digest()).digest()
  31. def derive_keys(key):
  32. h = sha256()
  33. h.update(key)
  34. h.update('cipher'.encode('utf-8'))
  35. cipher_key = h.digest()
  36. h = sha256()
  37. h.update(key)
  38. h.update('mac'.encode('utf-8'))
  39. mac_key = h.digest()
  40. return (cipher_key, mac_key)
  41. def generate_key():
  42. return base64.b64encode(urandom(KEY_SIZE))
  43. class Cipher(object):
  44. def __init__(self, key="", text=""):
  45. self.block_size = 16
  46. self.mac_size = 20
  47. self.key = self.set_key(key)
  48. self.text = self.set_text(text)
  49. self.mode = AES.MODE_CFB
  50. def set_key(self, key):
  51. try:
  52. key = base64.b64decode(key)
  53. except TypeError:
  54. raise ValueError
  55. self.key = key
  56. return self.key
  57. def set_text(self, text):
  58. self.text = text
  59. return self.text
  60. def encrypt(self):
  61. if BLOCK_SIZE + len(self.text) + MAC_SIZE > 105:
  62. self.text = self.text[:105 - BLOCK_SIZE - MAC_SIZE]
  63. (cipher_key, mac_key) = derive_keys(self.key)
  64. iv = urandom(BLOCK_SIZE)
  65. aes = AES.new(cipher_key, self.mode, iv)
  66. ciphertext = aes.encrypt(self.text)
  67. mac = hmac_sha1(mac_key, iv + ciphertext)
  68. return base64.b64encode(iv + ciphertext + mac)
  69. def decrypt(self):
  70. try:
  71. iv_ciphertext_mac = base64.urlsafe_b64decode(self.text)
  72. except:
  73. try:
  74. padding = len(self.text) % 4
  75. if padding == 1:
  76. return ''
  77. elif padding == 2:
  78. self.text += b'=='
  79. elif padding == 3:
  80. self.text += b'='
  81. iv_ciphertext_mac = base64.urlsafe_b64decode(self.text)
  82. except TypeError:
  83. return None
  84. iv = iv_ciphertext_mac[:BLOCK_SIZE]
  85. ciphertext = iv_ciphertext_mac[BLOCK_SIZE:-MAC_SIZE]
  86. mac = iv_ciphertext_mac[-MAC_SIZE:]
  87. (cipher_key, mac_key) = derive_keys(self.key)
  88. expected_mac = hmac_sha1(mac_key, iv + ciphertext)
  89. if mac != expected_mac:
  90. return None
  91. aes = AES.new(cipher_key, self.mode, iv)
  92. return aes.decrypt(ciphertext)
  93. if __name__ == "__main__":
  94. print("\nUFONet Crypter (AES256+HMAC-SHA1) -> (140 plain text chars = 69 encrypted chars)\n")
  95. text = str(input("-> Enter TEXT: "))
  96. input_key = str(input("\n-> Enter KEY: "))
  97. key = base64.b64encode(input_key.encode('utf-8'))
  98. c = Cipher(key, text)
  99. msg = c.encrypt()
  100. msg = msg.decode('utf-8')
  101. c.set_text(msg)
  102. print("\n" + " " + '-'*44)
  103. print('\n-> Ciphertext: [', msg, ']')
  104. print('\n-> Length:', len(msg))
  105. print("\n" + " " + '-'*44)
  106. print('\n-> Key (share it using SNEAKNET!):', input_key)
  107. print('\n-> Decryption PoC:', c.decrypt().decode('utf-8'), "\n")