Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply type annotations #1042

Merged
merged 24 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
98035fb
progress on typing
ehrenb Mar 23, 2024
c754270
finish typing analysis.py
ehrenb Mar 24, 2024
30db069
progress on typing dex/__init__.py
ehrenb Mar 27, 2024
066b89a
finish pass at dex.__init__.py typing
ehrenb Mar 31, 2024
e30e140
more types
ehrenb Apr 2, 2024
43a7659
more typing, and fix circular imports using 'if TYPE_CHECKING' checking
ehrenb Apr 3, 2024
6506fe6
begin to change Generator->Iterator for typing, begin to returns that…
ehrenb Apr 14, 2024
e837118
type|None only works in Python3.10+, which is too high of an assumpti…
ehrenb Apr 20, 2024
2430407
withoffset->with_offset
ehrenb Apr 20, 2024
59e45ee
fix circular import issue due to adding imports for typing
ehrenb Apr 20, 2024
1de7f23
types for permission loading
ehrenb Apr 20, 2024
ae104ae
apply type hints to bytecode module
ehrenb Apr 23, 2024
dd521fb
convert | to Union for further backwards compatibility, progress towa…
ehrenb Apr 23, 2024
bf590bc
finish typing axml
ehrenb Apr 23, 2024
ccfd728
order imports, standardize type|None -> Union[type,None]
ehrenb Apr 23, 2024
25672a9
fix type for get_certificate_name_string param
ehrenb Apr 23, 2024
f5572de
explicitly import Name for typing
ehrenb Apr 23, 2024
008c1d0
standardize type|None -> Union[type,None]
ehrenb Apr 23, 2024
409f616
type annotate main
ehrenb Apr 27, 2024
0211c25
fix some inaccurate hints
ehrenb Apr 27, 2024
e531aec
type hint fixes
ehrenb Apr 27, 2024
95966f8
add imports for typing
ehrenb Apr 27, 2024
b8545d8
remove unused import
ehrenb Apr 27, 2024
99afe72
remove explicit dependence on typing_extensions, as we can do self-re…
ehrenb Apr 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 32 additions & 18 deletions androguard/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import shutil
import sys
from typing import Union

# 3rd party modules
from lxml import etree
Expand All @@ -13,15 +14,20 @@
from oscrypto import asymmetric

# internal modules
from androguard.core.axml import ARSCParser
from androguard.session import Session
from androguard.core import androconf
from androguard.core import apk
from androguard.core.axml import AXMLPrinter
from androguard.core.dex import get_bytecodes_method
from androguard.util import readFile
from androguard.ui import DynamicUI


def androaxml_main(inp, outp=None, resource=None):
def androaxml_main(
inp:str,
outp:Union[str,None]=None,
resource:Union[str,None]=None) -> None:

ret_type = androconf.is_android(inp)
if ret_type == "APK":
a = apk.APK(inp)
Expand All @@ -47,7 +53,13 @@ def androaxml_main(inp, outp=None, resource=None):
sys.stdout.write(highlight(buff.decode("UTF-8"), get_lexer_by_name("xml"), TerminalFormatter()))


def androarsc_main(arscobj, outp=None, package=None, typ=None, locale=None):
def androarsc_main(
arscobj: ARSCParser,
outp:Union[str,None]=None,
package:Union[str,None]=None,
typ:Union[str,None]=None,
locale:Union[str,None]=None) -> None:

package = package or arscobj.get_packages_names()[0]
ttype = typ or "public"
locale = locale or '\x00\x00'
Expand Down Expand Up @@ -75,13 +87,15 @@ def androarsc_main(arscobj, outp=None, package=None, typ=None, locale=None):
sys.stdout.write(highlight(buff.decode("UTF-8"), get_lexer_by_name("xml"), TerminalFormatter()))


def export_apps_to_format(filename,
s,
output,
methods_filter=None,
jar=None,
decompiler_type=None,
form=None):
def export_apps_to_format(
filename:str,
s: Session,
output: str,
methods_filter:Union[str,None]=None,
jar:bool=False,
decompiler_type:Union[str,None]=None,
form:Union[str,None]=None) -> None:

from androguard.misc import clean_file_name
from androguard.core.bytecode import method2dot, method2format
from androguard.decompiler import decompiler
Expand Down Expand Up @@ -188,18 +202,18 @@ def export_apps_to_format(filename,
print()


def valid_class_name(class_name):
def valid_class_name(class_name:str) -> str:
if class_name[-1] == ";":
class_name = class_name[1:-1]
return os.path.join(*class_name.split("/"))


def create_directory(pathdir):
def create_directory(pathdir:str) -> None:
if not os.path.exists(pathdir):
os.makedirs(pathdir)


def androlyze_main(session, filename):
def androlyze_main(session:Session, filename:str) -> None:
"""
Start an interactive shell

Expand Down Expand Up @@ -275,7 +289,7 @@ def androlyze_main(session, filename):
print(dx)
print()

def shutdown_hook():
def shutdown_hook() -> None:
"""Save the session on exit, if wanted"""
if not s.isOpen():
return
Expand All @@ -298,7 +312,7 @@ def shutdown_hook():
ipshell()


def androsign_main(args_apk, args_hash, args_all, show):
def androsign_main(args_apk:list[str], args_hash:str, args_all:bool, show:bool) -> None:
from androguard.core.apk import APK
from androguard.util import get_certificate_name_string

Expand Down Expand Up @@ -381,7 +395,7 @@ def androsign_main(args_apk, args_hash, args_all, show):
print()


def androdis_main(offset, size, dex_file):
def androdis_main(offset:int, size:int, dex_file:str) -> None:
from androguard.core.dex import DEX

with open(dex_file, "rb") as fp:
Expand Down Expand Up @@ -414,7 +428,7 @@ def androdis_main(offset, size, dex_file):
idx += i.get_length()


def androtrace_main(apk_file, list_modules, live=False, enable_ui=False):
def androtrace_main(apk_file:str, list_modules:list[str], live:bool=False, enable_ui:bool=False) -> None:
from androguard.pentest import Pentest
from androguard.session import Session

Expand Down Expand Up @@ -459,7 +473,7 @@ def inputhook(inputhook_context: InputHookContext):
s = input("Type 'e' to exit:")


def androdump_main(package_name, list_modules):
def androdump_main(package_name:str, list_modules:list[str]) -> None:
from androguard.pentest import Pentest
from androguard.session import Session

Expand Down