Skip to content

Commit

Permalink
πŸ“… Commit Sat, 26 Jun 2021 00:13:43
Browse files Browse the repository at this point in the history
🚧 3.3
πŸ’š CICD minor changes
βœ… tests added/updated
🎨 minor refactors
⚑️ performance improvements with more lazy imports
✨ convert_to_nato
✨ rsa_encrypt
✨ rsa_decrypt
✨ rsa_sign
✨ rsa_verify
✨ generate_rsa_keypair
  • Loading branch information
securisec committed Jun 26, 2021
1 parent 45f53dc commit ff64956
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 49 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tests_multi_os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ jobs:
max-parallel: 6
matrix:
os: [ubuntu-18.04, windows-latest, macOS-latest]
python-version: ["3.8", "3.9"]
python-version:
- "3.8"
# - "3.9"

steps:
- uses: actions/checkout@v2.3.4
Expand Down
8 changes: 8 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ Code:
New ideas:
☐ rubber ducky encode/decode
☐ save registers https://t.co/pBNRufibY8?amp=1
βœ” convert to nato alphabets: a = alpha, b = bravo
☐ cbor encode/decode https://github.com/agronholm/cbor2 (plugin)
☐ fuzzy search
βœ” rsa sign/verify
βœ” rsa generate random key (private and public)
βœ” rsa encrypt/decrpt
☐ pgp, generate, encrypt, decrypt, verify
☐ swap little and big endian

Cli:
☐ optionally show output window in a split screen window
Expand Down
2 changes: 1 addition & 1 deletion chepy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "3.2.0" # pragma: no cover
__version__ = "3.3.0" # pragma: no cover
__author__ = "Hapsida @securisec" # pragma: no cover
2 changes: 1 addition & 1 deletion chepy/chepy_plugins
Submodule chepy_plugins updated 1 files
+36 βˆ’34 chepy_pcaps.py
14 changes: 7 additions & 7 deletions chepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io
import itertools
import logging
import pathlib
from pathlib import Path
import subprocess
import sys
import webbrowser
Expand Down Expand Up @@ -47,7 +47,7 @@ def call_stack(func, *args, **kwargs):
func_sig["args"] = func_arguments
func_self._stack.append(func_sig)

return func(*args, **kwargs) # lgtm [py/call-to-non-callable]
return func(*args, **kwargs) # lgtm [py/call-to-non-callable]


class ChepyCore(object):
Expand Down Expand Up @@ -151,7 +151,7 @@ def _abs_path(self, path: str):
Returns:
object: Path object
"""
return pathlib.Path(path).expanduser().absolute()
return Path(path).expanduser().absolute()

def _info_logger(self, data: str) -> None:
"""Just a binding for logger.info
Expand Down Expand Up @@ -796,7 +796,7 @@ def load_dir(self, pattern: str = "*"):
Returns:
Chepy: The Chepy object.
"""
files = [x for x in pathlib.Path(self.state).glob(pattern) if x.is_file()]
files = [x for x in Path(self.state).glob(pattern) if x.is_file()]
self.states = {x[0]: str(x[1]) for x in enumerate(files) if x[1].is_file()}
return self

Expand All @@ -815,7 +815,7 @@ def load_file(self, binary_mode: bool = False):
>>> # at the moment, the state only contains the string "/path/to/file"
>>> c.load_file() # this will load the file content into the state
"""
path = pathlib.Path(str(self.state)).expanduser().absolute()
path = Path(str(self.state)).expanduser().absolute()
if binary_mode:
with open(path, "rb") as f:
self.states[self._current_index] = bytearray(f.read())
Expand Down Expand Up @@ -1192,7 +1192,7 @@ def plugins(self, enable: str) -> None: # pragma: no cover
None
"""
assert enable in ["true", "false"], "Valid values are true and false"
conf_path = pathlib.Path().home() / ".chepy" / "chepy.conf"
conf_path = Path().home() / ".chepy" / "chepy.conf"
c = ConfigParser()
c.read(conf_path)
c.set("Plugins", "enableplugins", enable)
Expand Down Expand Up @@ -1224,7 +1224,7 @@ def set_plugin_path(self, path: str) -> None: # pragma: no cover
"""
expand_path = self._abs_path(path)
if expand_path.exists():
conf_path = pathlib.Path().home() / ".chepy" / "chepy.conf"
conf_path = Path().home() / ".chepy" / "chepy.conf"
c = ConfigParser()
c.read(conf_path)
c.set("Plugins", "pluginpath", str(expand_path))
Expand Down
23 changes: 23 additions & 0 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,3 +1029,26 @@ def trim(self) -> DataFormatT:
"""
self.state = self._convert_to_str().strip()
return self

@ChepyDecorators.call_stack
def convert_to_nato(self, join_by: str = " ") -> DataFormatT:
"""Convert string to NATO phonetic format.
Example: abc = Alpha Bravo Charlie
Args:
join_by (str, optional): [description]. Defaults to " ".
Returns:
Chepy: The Chepy object
"""
nato_chars = Encoding.NATO_CONSTANTS_DICT
hold = []
data: str = self._convert_to_str()
for d in data:
if d.isalpha():
hold.append(nato_chars[d.upper()])
else:
hold.append(d)
self.state = join_by.join(hold)
return self
1 change: 1 addition & 0 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ class DataFormat(ChepyCore):
def to_braille(self: DataFormatT) -> DataFormatT: ...
def from_braille(self: DataFormatT) -> DataFormatT: ...
def trim(self: DataFormatT) -> DataFormatT: ...
def convert_to_nato(self: DataFormatT, join_by:str) -> DataFormatT: ...
107 changes: 90 additions & 17 deletions chepy/modules/encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
ARC4 = lazy_import.lazy_module("Crypto.Cipher.ARC4")
DES = lazy_import.lazy_module("Crypto.Cipher.DES")
DES3 = lazy_import.lazy_module("Crypto.Cipher.DES3")
RSA = lazy_import.lazy_module("Crypto.PublicKey.RSA")
Hash = lazy_import.lazy_module("Crypto.Hash")
PKCS1_15 = lazy_import.lazy_module("Crypto.Signature.pkcs1_15")
PKCS1_OAEP = lazy_import.lazy_module("Crypto.Cipher.PKCS1_OAEP")
Blowfish = lazy_import.lazy_module("Crypto.Cipher.Blowfish")
from Crypto.Util.Padding import pad, unpad
Padding = lazy_import.lazy_module("Crypto.Util.Padding")

from ..core import ChepyCore, ChepyDecorators
from ..extras.combinatons import hex_chars
Expand Down Expand Up @@ -423,11 +427,11 @@ def des_encrypt(

if mode == "CBC":
cipher = DES.new(key, mode=DES.MODE_CBC, iv=iv)
self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 8))
return self
elif mode == "ECB":
cipher = DES.new(key, mode=DES.MODE_ECB)
self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 8))
return self
elif mode == "CTR":
cipher = DES.new(key, mode=DES.MODE_CTR, nonce=b"")
Expand Down Expand Up @@ -478,11 +482,11 @@ def des_decrypt(

if mode == "CBC":
cipher = DES.new(key, mode=DES.MODE_CBC, iv=iv)
self.state = unpad(cipher.decrypt(self._convert_to_bytes()), 8)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 8)
return self
elif mode == "ECB":
cipher = DES.new(key, mode=DES.MODE_ECB)
self.state = unpad(cipher.decrypt(self._convert_to_bytes()), 8)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 8)
return self
elif mode == "CTR":
cipher = DES.new(key, mode=DES.MODE_CTR, nonce=b"")
Expand Down Expand Up @@ -532,11 +536,11 @@ def triple_des_encrypt(

if mode == "CBC":
cipher = DES3.new(key, mode=DES3.MODE_CBC, iv=iv)
self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 8))
return self
elif mode == "ECB":
cipher = DES3.new(key, mode=DES3.MODE_ECB)
self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 8))
return self
elif mode == "CTR":
cipher = DES3.new(key, mode=DES3.MODE_CTR, nonce=b"")
Expand Down Expand Up @@ -589,11 +593,11 @@ def triple_des_decrypt(

if mode == "CBC":
cipher = DES3.new(key, mode=DES3.MODE_CBC, iv=iv)
self.state = unpad(cipher.decrypt(self._convert_to_bytes()), 8)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 8)
return self
elif mode == "ECB":
cipher = DES3.new(key, mode=DES3.MODE_ECB)
self.state = unpad(cipher.decrypt(self._convert_to_bytes()), 8)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 8)
return self
elif mode == "CTR":
cipher = DES3.new(key, mode=DES3.MODE_CTR, nonce=b"")
Expand Down Expand Up @@ -650,11 +654,11 @@ def aes_encrypt(

if mode == "CBC":
cipher = AES.new(key, mode=AES.MODE_CBC, iv=iv)
self.state = cipher.encrypt(pad(self._convert_to_bytes(), 16))
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 16))
return self
elif mode == "ECB":
cipher = AES.new(key, mode=AES.MODE_ECB)
self.state = cipher.encrypt(pad(self._convert_to_bytes(), 16))
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 16))
return self
elif mode == "CTR":
cipher = AES.new(key, mode=AES.MODE_CTR, nonce=b"")
Expand Down Expand Up @@ -723,11 +727,11 @@ def aes_decrypt(

if mode == "CBC":
cipher = AES.new(key, mode=AES.MODE_CBC, iv=iv)
self.state = unpad(cipher.decrypt(self._convert_to_bytes()), 16)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 16)
return self
elif mode == "ECB":
cipher = AES.new(key, mode=AES.MODE_ECB)
self.state = unpad(cipher.decrypt(self._convert_to_bytes()), 16)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 16)
return self
elif mode == "CTR":
cipher = AES.new(key, mode=AES.MODE_CTR, nonce=b"")
Expand Down Expand Up @@ -783,11 +787,11 @@ def blowfish_encrypt(

if mode == "CBC":
cipher = Blowfish.new(key, mode=Blowfish.MODE_CBC, iv=iv)
self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 8))
return self
elif mode == "ECB":
cipher = Blowfish.new(key, mode=Blowfish.MODE_ECB)
self.state = cipher.encrypt(pad(self._convert_to_bytes(), 8))
self.state = cipher.encrypt(Padding.pad(self._convert_to_bytes(), 8))
return self
elif mode == "CTR":
cipher = Blowfish.new(key, mode=Blowfish.MODE_CTR, nonce=b"")
Expand Down Expand Up @@ -839,11 +843,11 @@ def blowfish_decrypt(

if mode == "CBC":
cipher = Blowfish.new(key, mode=Blowfish.MODE_CBC, iv=iv)
self.state = unpad(cipher.decrypt(self._convert_to_bytes()), 8)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 8)
return self
elif mode == "ECB":
cipher = Blowfish.new(key, mode=Blowfish.MODE_ECB)
self.state = unpad(cipher.decrypt(self._convert_to_bytes()), 8)
self.state = Padding.unpad(cipher.decrypt(self._convert_to_bytes()), 8)
return self
elif mode == "CTR":
cipher = Blowfish.new(key, mode=Blowfish.MODE_CTR, nonce=b"")
Expand Down Expand Up @@ -1019,3 +1023,72 @@ def from_morse_code(
decode += morse_code_dict.get(chars)
self.state = decode
return self

@ChepyDecorators.call_stack
def rsa_encrypt(self, pub_key_path: str) -> EncryptionEncodingT:
"""Encrypt data with RSA Public key in PEM format
Args:
pub_key_path (str): Path to Public key
Returns:
Chepy: The Chepy object
"""
with open(str(self._abs_path(pub_key_path)), "r") as f:
pub_key = f.read()
key = RSA.importKey(pub_key)
cipher = PKCS1_OAEP.new(key)
self.state = cipher.encrypt(self._convert_to_bytes())
return self

@ChepyDecorators.call_stack
def rsa_decrypt(self, priv_key_path: str) -> EncryptionEncodingT:
"""Decrypt data with RSA Private key in PEM format
Args:
priv_key_path (str): Path to Private key
Returns:
Chepy: The Chepy object
"""
with open(str(self._abs_path(priv_key_path)), "r") as f:
priv_key = f.read()
key = RSA.importKey(priv_key)
cipher = PKCS1_OAEP.new(key)
self.state = cipher.decrypt(self._convert_to_bytes())
return self

@ChepyDecorators.call_stack
def rsa_sign(self, priv_key_path: str) -> EncryptionEncodingT:
"""Sign data in state with RSA Private key in PEM format
Args:
priv_key_path (str): Path to Private key
Returns:
Chepy: The Chepy object
"""
with open(str(self._abs_path(priv_key_path)), "r") as f:
priv_key = f.read()
key = RSA.importKey(priv_key)
h = Hash.SHA256.new(self._convert_to_bytes())
self.state = PKCS1_15.new(key).sign(h)
return self

@ChepyDecorators.call_stack
def rsa_verify(self, signature: bytes, public_key_path: str) -> EncryptionEncodingT: # pragma: no cover
"""Verify data in state with RSA Public key in PEM format
Args:
signature (bytes): The signature as bytes
public_key_path (str): Path to Private key
Returns:
Chepy: The Chepy object
"""
with open(str(self._abs_path(public_key_path)), "r") as f:
pub_key = f.read()
key = RSA.importKey(pub_key)
h = Hash.SHA256.new(self._convert_to_bytes())
self.state = PKCS1_15.new(key).verify(h, signature)
return self
4 changes: 4 additions & 0 deletions chepy/modules/encryptionencoding.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ class EncryptionEncoding(ChepyCore):
def atbash_decode(self: EncryptionEncodingT) -> EncryptionEncodingT: ...
def to_morse_code(self: EncryptionEncodingT, dot: str=..., dash: str=..., letter_delim: str=..., word_delim: str=...) -> Any: ...
def from_morse_code(self: EncryptionEncodingT, dot: str=..., dash: str=..., letter_delim: str=..., word_delim: str=...) -> Any: ...
def rsa_encrypt(self: EncryptionEncodingT, pub_key_path: str) -> EncryptionEncodingT: ...
def rsa_decrypt(self: EncryptionEncodingT, priv_key_path: str) -> EncryptionEncodingT: ...
def rsa_sign(self: EncryptionEncodingT, priv_key_path: str) -> EncryptionEncodingT: ...
def rsa_verify(self: EncryptionEncodingT, signature: bytes, public_key_path: str) -> EncryptionEncodingT: ...
30 changes: 29 additions & 1 deletion chepy/modules/internal/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
class Encoding(object):
NATO_CONSTANTS_DICT = {
"A": "Alpha",
"B": "Bravo",
"C": "Charlie",
"D": "Delta",
"E": "Echo",
"F": "Foxtrot",
"G": "Golf",
"H": "Hotel",
"I": "India",
"J": "Juliett",
"K": "Kilo",
"L": "Lima",
"M": "Mike",
"N": "November",
"O": "Oscar",
"P": "Papa",
"Q": "Quebec",
"R": "Romeo",
"S": "Sierra",
"T": "Tango",
"U": "Uniform",
"V": "Victor",
"W": "Whiskey",
"X": "Xray",
"Y": "Yankee",
"Z": "Zulu",
}

py_encodings = [
"ascii",
"big5",
Expand Down Expand Up @@ -459,4 +488,3 @@ class PcapUSB:
"37": ">",
"38": "?",
}

3 changes: 2 additions & 1 deletion chepy/modules/internal/constants.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import Any
from typing import Any, Dict

class Encoding:
py_encodings: Any = ...
py_text_encodings: Any = ...
asciichars: Any = ...
brailles: Any = ...
NATO_CONSTANTS_DICT: Dict[str, str] = ...

class EncryptionConsts:
MORSE_CODE_DICT: Any = ...
Expand Down

0 comments on commit ff64956

Please sign in to comment.