diff --git a/mobsf/MobSF/utils.py b/mobsf/MobSF/utils.py index b4c645ffcc..f2f84b9b92 100755 --- a/mobsf/MobSF/utils.py +++ b/mobsf/MobSF/utils.py @@ -295,7 +295,9 @@ def sha256_object(file_obj): def gen_sha256_hash(msg): """Generate SHA 256 Hash of the message.""" - hash_object = hashlib.sha256(msg.encode('utf-8')) + if isinstance(msg, str): + msg = msg.encode('utf-8') + hash_object = hashlib.sha256(msg) return hash_object.hexdigest() diff --git a/mobsf/StaticAnalyzer/views/android/cert_analysis.py b/mobsf/StaticAnalyzer/views/android/cert_analysis.py index b4c50d5e7c..239d71cc9c 100755 --- a/mobsf/StaticAnalyzer/views/android/cert_analysis.py +++ b/mobsf/StaticAnalyzer/views/android/cert_analysis.py @@ -1,7 +1,6 @@ # -*- coding: utf_8 -*- """Module holding the functions for code analysis.""" -import binascii import hashlib import logging import os @@ -13,12 +12,19 @@ from asn1crypto import x509 -from oscrypto import asymmetric +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ( + dsa, + ec, + rsa, +) from django.utils.html import escape from mobsf.MobSF.utils import ( find_java_binary, + gen_sha256_hash, ) logger = logging.getLogger(__name__) @@ -83,11 +89,35 @@ def get_cert_details(data): def get_pub_key_details(data): """Get public key details.""" certlist = [] - x509_public_key = asymmetric.load_public_key(data) - certlist.append(f'PublicKey Algorithm: {x509_public_key.algorithm}') - certlist.append(f'Bit Size: {x509_public_key.bit_size}') - fp = binascii.hexlify(x509_public_key.fingerprint).decode('utf-8') - certlist.append(f'Fingerprint: {fp}') + + x509_public_key = serialization.load_der_public_key( + data, + backend=default_backend()) + alg = 'unknown' + fingerprint = '' + if isinstance(x509_public_key, rsa.RSAPublicKey): + alg = 'rsa' + modulus = x509_public_key.public_numbers().n + public_exponent = x509_public_key.public_numbers().e + to_hash = f'{modulus}:{public_exponent}' + elif isinstance(x509_public_key, dsa.DSAPublicKey): + alg = 'dsa' + dsa_parameters = x509_public_key.parameters() + p = dsa_parameters.p + q = dsa_parameters.q + g = dsa_parameters.g + y = x509_public_key.public_numbers().y + to_hash = f'{p}:{q}:{g}:{y}' + elif isinstance(x509_public_key, ec.EllipticCurvePublicKey): + alg = 'ec' + to_hash = f'{x509_public_key.public_numbers().curve.name}:' + to_hash = to_hash.encode('utf-8') + # Untested possibly wrong key size and fingerprint + to_hash += data[25:] + fingerprint = gen_sha256_hash(to_hash) + certlist.append(f'PublicKey Algorithm: {alg}') + certlist.append(f'Bit Size: {x509_public_key.key_size}') + certlist.append(f'Fingerprint: {fingerprint}') return certlist diff --git a/poetry.lock b/poetry.lock index c7420ac2b6..886d4334dc 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1061,20 +1061,20 @@ files = [ [[package]] name = "importlib-metadata" -version = "6.8.0" +version = "6.9.0" description = "Read metadata from Python packages" optional = false python-versions = ">=3.8" files = [ - {file = "importlib_metadata-6.8.0-py3-none-any.whl", hash = "sha256:3ebb78df84a805d7698245025b975d9d67053cd94c79245ba4b3eb694abe68bb"}, - {file = "importlib_metadata-6.8.0.tar.gz", hash = "sha256:dbace7892d8c0c4ac1ad096662232f831d4e64f4c4545bd53016a3e9d4654743"}, + {file = "importlib_metadata-6.9.0-py3-none-any.whl", hash = "sha256:1c8dc6839ddc9771412596926f24cb5a553bbd40624ee2c7e55e531542bed3b8"}, + {file = "importlib_metadata-6.9.0.tar.gz", hash = "sha256:e8acb523c335a91822674e149b46c0399ec4d328c4d1f6e49c273da5ff0201b9"}, ] [package.dependencies] zipp = ">=0.5" [package.extras] -docs = ["furo", "jaraco.packaging (>=9)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (>=3.5)", "sphinx-lint"] +docs = ["furo", "jaraco.packaging (>=9.3)", "jaraco.tidelift (>=1.4)", "rst.linker (>=1.9)", "sphinx (<7.2.5)", "sphinx (>=3.5)", "sphinx-lint"] perf = ["ipython"] testing = ["flufl.flake8", "importlib-resources (>=1.3)", "packaging", "pyfakefs", "pytest (>=6)", "pytest-black (>=0.3.7)", "pytest-checkdocs (>=2.4)", "pytest-cov", "pytest-enabler (>=2.2)", "pytest-mypy (>=0.9.1)", "pytest-perf (>=0.9.2)", "pytest-ruff"] @@ -1853,20 +1853,6 @@ files = [ {file = "openstep_parser-1.5.4.tar.gz", hash = "sha256:f03c7fd9c78ac718c470cb0f667c1f09fa7f58786314f77fef8767302abe1658"}, ] -[[package]] -name = "oscrypto" -version = "1.3.0" -description = "TLS (SSL) sockets, key generation, encryption, decryption, signing, verification and KDFs using the OS crypto libraries. Does not require a compiler, and relies on the OS for patching. Works on Windows, OS X and Linux/BSD." -optional = false -python-versions = "*" -files = [ - {file = "oscrypto-1.3.0-py2.py3-none-any.whl", hash = "sha256:2b2f1d2d42ec152ca90ccb5682f3e051fb55986e1b170ebde472b133713e7085"}, - {file = "oscrypto-1.3.0.tar.gz", hash = "sha256:6f5fef59cb5b3708321db7cca56aed8ad7e662853351e7991fcf60ec606d47a4"}, -] - -[package.dependencies] -asn1crypto = ">=1.5.1" - [[package]] name = "packaging" version = "21.3" @@ -3262,4 +3248,4 @@ cffi = ["cffi (>=1.11)"] [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "b51407dc27c9dce91ed0e29093bbaab49f1bd3ad43666841690054a2b7f5f617" +content-hash = "964e294192c5aa0a22704692b726f616276c6b493c44e351113a9afd0d335b63" diff --git a/pyproject.toml b/pyproject.toml index 20265b56aa..dd422e72bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,7 +38,6 @@ gunicorn = {version = ">=20.0.4", markers = "sys_platform != 'win32'"} psutil = ">=5.8.0" shelljob = ">=0.6.2" asn1crypto = ">=1.4.0" -oscrypto = ">=1.2.1" distro = ">=1.5.0" ip2location = "8.10.0" lief = ">=0.12.3"