From 7f45550ea610a832817edc8cc18248deea90cf59 Mon Sep 17 00:00:00 2001 From: hugsy Date: Sun, 22 May 2022 08:14:12 -0700 Subject: [PATCH] Type Hinting Fixes - Part 1 (#827) * better linting, by creating a proper .editorconfig & pylintrc * major drop of linting errors, from 786 errors to 145 * using Py3.6 `__init_subclass__` allows to remove the use of `abc` and `@register_architecture` * using `__init_subclass__` to create base class for commands * using `__init_subclass__` to create base class for functions --- .editorconfig | 21 ++ .pylintrc | 886 ++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 13 +- gef.py | 636 ++++++++++++++++-------------------- 4 files changed, 1192 insertions(+), 364 deletions(-) create mode 100644 .editorconfig create mode 100644 .pylintrc diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..30d459c4d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +# https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties + +root = true + +[*] +end_of_line = lf +insert_final_newline = true +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.py] +indent_style = space +indent_size = 4 + +[Makefile] +indent_style = tab + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 000000000..8d6c69538 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,886 @@ +[MASTER] + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-allow-list= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. (This is an alternative name to extension-pkg-allow-list +# for backward compatibility.) +extension-pkg-whitelist= + +# Return non-zero exit code if any of these messages/categories are detected, +# even if score is above --fail-under value. Syntax same as enable. Messages +# specified are enabled, while categories only check already-enabled messages. +fail-on= + +# Specify a score threshold to be exceeded before program exits with error. +fail-under=10.0 + +# Files or directories to be skipped. They should be base names, not paths. +ignore=CVS + +# Add files or directories matching the regex patterns to the ignore-list. The +# regex matches against paths and can be in Posix or Windows format. +ignore-paths= + +# Files or directories matching the regex patterns are skipped. The regex +# matches against base names, not paths. +ignore-patterns= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use. +jobs=0 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=100 + +# List of plugins (as comma separated values of python module names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Minimum Python version to use for version dependent checks. Will default to +# the version used to run pylint. +py-version=3.6 + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. +confidence= + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +; disable=invalid-name, +; disallowed-name, +; empty-docstring, +; missing-module-docstring, +; missing-class-docstring, +; missing-function-docstring, +; unidiomatic-typecheck, +; non-ascii-name, +; consider-using-enumerate, +; consider-iterating-dictionary, +; bad-classmethod-argument, +; bad-mcs-method-argument, +; bad-mcs-classmethod-argument, +; single-string-used-for-slots, +; consider-using-dict-items, +; use-maxsplit-arg, +; use-sequence-for-iteration, +; too-many-lines, +; missing-final-newline, +; trailing-newlines, +; superfluous-parens, +; mixed-line-endings, +; unexpected-line-ending-format, +; wrong-spelling-in-comment, +; wrong-spelling-in-docstring, +; invalid-characters-in-docstring, +; multiple-imports, +; wrong-import-order, +; ungrouped-imports, +; wrong-import-position, +; useless-import-alias, +; import-outside-toplevel, +; use-implicit-booleaness-not-len, +; use-implicit-booleaness-not-comparison, +; raw-checker-failed, +; bad-inline-option, +; locally-disabled, +; file-ignored, +; suppressed-message, +; useless-suppression, +; deprecated-pragma, +; use-symbolic-message-instead, +; c-extension-no-member, +; literal-comparison, +; comparison-with-itself, +; no-self-use, +; no-classmethod-decorator, +; no-staticmethod-decorator, +; useless-object-inheritance, +; property-with-parameters, +; cyclic-import, +; consider-using-from-import, +; duplicate-code, +; too-many-ancestors, +; too-many-instance-attributes, +; too-few-public-methods, +; too-many-public-methods, +; too-many-return-statements, +; too-many-branches, +; too-many-arguments, +; too-many-locals, +; too-many-statements, +; too-many-boolean-expressions, +; consider-merging-isinstance, +; too-many-nested-blocks, +; simplifiable-if-statement, +; redefined-argument-from-local, +; no-else-return, +; consider-using-ternary, +; trailing-comma-tuple, +; stop-iteration-return, +; simplify-boolean-expression, +; inconsistent-return-statements, +; useless-return, +; consider-swap-variables, +; consider-using-join, +; consider-using-in, +; consider-using-get, +; chained-comparison, +; consider-using-dict-comprehension, +; consider-using-set-comprehension, +; simplifiable-if-expression, +; no-else-raise, +; unnecessary-comprehension, +; consider-using-sys-exit, +; no-else-break, +; no-else-continue, +; super-with-arguments, +; simplifiable-condition, +; condition-evals-to-constant, +; consider-using-generator, +; use-a-generator, +; consider-using-min-builtin, +; consider-using-max-builtin, +; consider-using-with, +; unnecessary-dict-index-lookup, +; use-list-literal, +; use-dict-literal, +; pointless-statement, +; pointless-string-statement, +; expression-not-assigned, +; unnecessary-pass, +; unnecessary-lambda, +; assign-to-new-keyword, +; useless-else-on-loop, +; exec-used, +; eval-used, +; confusing-with-statement, +; using-constant-test, +; missing-parentheses-for-call-in-test, +; self-assigning-variable, +; redeclared-assigned-name, +; assert-on-string-literal, +; comparison-with-callable, +; lost-exception, +; nan-comparison, +; assert-on-tuple, +; attribute-defined-outside-init, +; bad-staticmethod-argument, +; protected-access, +; arguments-differ, +; signature-differs, +; abstract-method, +; super-init-not-called, +; no-init, +; non-parent-init-called, +; useless-super-delegation, +; invalid-overridden-method, +; arguments-renamed, +; unused-private-member, +; overridden-final-method, +; subclassed-final-class, +; bad-indentation, +; wildcard-import, +; deprecated-module, +; reimported, +; import-self, +; preferred-module, +; misplaced-future, +; fixme, +; global-variable-undefined, +; global-statement, +; global-at-module-level, +; unused-argument, +; unused-wildcard-import, +; redefined-outer-name, +; redefined-builtin, +; undefined-loop-variable, +; unbalanced-tuple-unpacking, +; cell-var-from-loop, +; possibly-unused-variable, +; self-cls-assignment, +; bare-except, +; broad-except, +; duplicate-except, +; try-except-raise, +; raise-missing-from, +; raising-format-tuple, +; wrong-exception-operation, +; keyword-arg-before-vararg, +; arguments-out-of-order, +; non-str-assignment-to-dunder-name, +; isinstance-second-argument-not-valid-type, +; logging-not-lazy, +; logging-format-interpolation, +; logging-fstring-interpolation, +; bad-format-string-key, +; unused-format-string-key, +; missing-format-argument-key, +; unused-format-string-argument, +; format-combined-specification, +; missing-format-attribute, +; invalid-format-index, +; duplicate-string-formatting-argument, +; f-string-without-interpolation, +; useless-with-lock + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +; enable=unneeded-not, +; format-string-without-interpolation, +; anomalous-unicode-escape-in-string, +; implicit-str-concat, +; inconsistent-quotes, +; redundant-u-string-prefix, +; boolean-datetime, +; redundant-unittest-assert, +; deprecated-method, +; bad-thread-instantiation, +; shallow-copy-environ, +; invalid-envvar-default, +; subprocess-popen-preexec-fn, +; subprocess-run-check, +; deprecated-argument, +; deprecated-class, +; deprecated-decorator, +; unspecified-encoding, +; forgotten-debug-statement, +; using-f-string-in-unsupported-version, +; using-final-decorator-in-unsupported-version, +; singleton-comparison, +; consider-using-f-string, +; line-too-long, +; trailing-whitespace, +; multiple-statements, +; syntax-error, +; unrecognized-inline-option, +; bad-option-value, +; bad-plugin-value, +; bad-configuration-section, +; init-is-generator, +; return-in-init, +; function-redefined, +; not-in-loop, +; return-outside-function, +; yield-outside-function, +; return-arg-in-generator, +; nonexistent-operator, +; duplicate-argument-name, +; abstract-class-instantiated, +; bad-reversed-sequence, +; too-many-star-expressions, +; invalid-star-assignment-target, +; star-needs-assignment-target, +; nonlocal-and-global, +; continue-in-finally, +; nonlocal-without-binding, +; used-prior-global-declaration, +; misplaced-format-function, +; method-hidden, +; access-member-before-definition, +; no-method-argument, +; no-self-argument, +; invalid-slots-object, +; assigning-non-slot, +; invalid-slots, +; inherit-non-class, +; inconsistent-mro, +; duplicate-bases, +; class-variable-slots-conflict, +; invalid-class-object, +; non-iterator-returned, +; unexpected-special-method-signature, +; invalid-length-returned, +; invalid-bool-returned, +; invalid-index-returned, +; invalid-repr-returned, +; invalid-str-returned, +; invalid-bytes-returned, +; invalid-hash-returned, +; invalid-length-hint-returned, +; invalid-format-returned, +; invalid-getnewargs-returned, +; invalid-getnewargs-ex-returned, +; import-error, +; relative-beyond-top-level, +; used-before-assignment, +; undefined-variable, +; undefined-all-variable, +; invalid-all-object, +; invalid-all-format, +; no-name-in-module, +; unpacking-non-sequence, +; bad-except-order, +; raising-bad-type, +; bad-exception-context, +; misplaced-bare-raise, +; raising-non-exception, +; notimplemented-raised, +; catching-non-exception, +; bad-super-call, +; no-member, +; not-callable, +; assignment-from-no-return, +; no-value-for-parameter, +; too-many-function-args, +; unexpected-keyword-arg, +; redundant-keyword-arg, +; missing-kwoa, +; invalid-sequence-index, +; invalid-slice-index, +; assignment-from-none, +; not-context-manager, +; invalid-unary-operand-type, +; unsupported-binary-operation, +; repeated-keyword, +; not-an-iterable, +; not-a-mapping, +; unsupported-membership-test, +; unsubscriptable-object, +; unsupported-assignment-operation, +; unsupported-delete-operation, +; invalid-metaclass, +; unhashable-dict-key, +; dict-iter-missing-items, +; await-outside-async, +; logging-unsupported-format, +; logging-format-truncated, +; logging-too-many-args, +; logging-too-few-args, +; bad-format-character, +; truncated-format-string, +; mixed-format-string, +; format-needs-mapping, +; missing-format-string-key, +; too-many-format-args, +; too-few-format-args, +; bad-string-format-type, +; bad-str-strip-call, +; invalid-envvar-value, +; yield-inside-async-function, +; not-async-context-manager, +; fatal, +; astroid-error, +; parse-error, +; config-parse-error, +; method-check-failed, +; unreachable, +; dangerous-default-value, +; duplicate-key, +; unnecessary-semicolon, +; global-variable-not-assigned, +; unused-import, +; unused-variable, +; binary-op-exception, +; bad-format-string, +; anomalous-backslash-in-string, +; bad-open-mode + +enable = F,E,unreachable,duplicate-key,unnecessary-semicolon,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,dangerous-default-value,trailing-whitespace,unneeded-not,singleton-comparison,unused-import,line-too-long,multiple-statements,consider-using-f-string,global-variable-not-assigned +disable = all + +[REPORTS] + +# Python expression which should return a score less than or equal to 10. You +# have access to the variables 'error', 'warning', 'refactor', and 'convention' +# which contain the number of messages in each category, as well as 'statement' +# which is the total number of statements analyzed. This score is used by the +# global evaluation report (RP0004). +evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +#msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +output-format=text + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit,argparse.parse_error + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format=LF + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=200 + +# Maximum number of lines in a module. +max-module-lines=15000 + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. Available dictionaries: none. To make it work, +# install the 'python-enchant' package. +spelling-dict= + +# List of comma separated words that should be considered directives if they +# appear and the beginning of a comment and should not be checked. +spelling-ignore-comment-directives=fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy: + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains the private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to the private dictionary (see the +# --spelling-private-dict-file option) instead of raising a message. +spelling-store-unknown-words=no + + +[LOGGING] + +# The type of string formatting that logging methods do. `old` means using % +# formatting, `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of names allowed to shadow builtins +allowed-redefined-builtins= + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io + + +[SIMILARITIES] + +# Comments are removed from the similarity computation +ignore-comments=yes + +# Docstrings are removed from the similarity computation +ignore-docstrings=yes + +# Imports are removed from the similarity computation +ignore-imports=no + +# Signatures are removed from the similarity computation +ignore-signatures=no + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + +# Regular expression of note tags to take in consideration. +#notes-rgx= + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Bad variable names regexes, separated by a comma. If names match any regex, +# they will always be refused +bad-names-rgxs= + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. +#class-attribute-rgx= + +# Naming style matching correct class constant names. +class-const-naming-style=UPPER_CASE + +# Regular expression matching correct class constant names. Overrides class- +# const-naming-style. +#class-const-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _ + +# Good variable names regexes, separated by a comma. If names match any regex, +# they will always be accepted +good-names-rgxs= + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. +#variable-rgx= + + +[STRING] + +# This flag controls whether inconsistent-quotes generates a warning when the +# character used as a quote delimiter is used inconsistently within a module. +check-quote-consistency=no + +# This flag controls whether the implicit-str-concat should generate a warning +# on implicit string concatenation in sequences defined over several lines. +check-str-concat-over-line-jumps=no + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether missing members accessed in mixin class should be ignored. A +# class is considered mixin if its name matches the mixin-class-rgx option. +ignore-mixin-members=yes + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis). It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + +# Regex pattern to define which classes are considered mixins ignore-mixin- +# members is set to 'yes' +mixin-class-rgx=.*[Mm]ixin + +# List of decorators that change the signature of a decorated function. +signature-mutators= + + +[IMPORTS] + +# List of modules that can be imported at any level, not just the top level +# one. +allow-any-import-level= + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules= + +# Output a graph (.gv or any supported image format) of external dependencies +# to the given file (report RP0402 must not be disabled). +ext-import-graph= + +# Output a graph (.gv or any supported image format) of all (i.e. internal and +# external) dependencies to the given file (report RP0402 must not be +# disabled). +import-graph= + +# Output a graph (.gv or any supported image format) of internal dependencies +# to the given file (report RP0402 must not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + +# Couples of modules and preferred modules, separated by a comma. +preferred-modules= + + +[DESIGN] + +# List of regular expressions of class ancestor names to ignore when counting +# public methods (see R0903) +exclude-too-few-public-methods= + +# List of qualified class names to ignore when counting class parents (see +# R0901) +ignored-parents= + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement (see R0916). +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=15 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[CLASSES] + +# Warn about protected attribute access inside special methods +check-protected-access-in-special-methods=no + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp, + __post_init__ + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict, + _fields, + _replace, + _source, + _make + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=cls + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. Defaults to +# "BaseException, Exception". +overgeneral-exceptions=BaseException, + Exception diff --git a/Makefile b/Makefile index 4d9f4be2f..20840cbb8 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,11 @@ +ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) NB_CORES := $(shell grep --count '^processor' /proc/cpuinfo) +PYLINT_RC := $(ROOT_DIR)/.pylintrc PYLINT_DISABLE:= all -PYLINT_ENABLE := F,E,unreachable,duplicate-key,unnecessary-semicolon,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,dangerous-default-value,trailing-whitespace,unneeded-not,singleton-comparison,unused-import -PYLINT_TEST_ENABLE := $(PYLINT_ENABLE),line-too-long,multiple-statements,consider-using-f-string,global-variable-not-assigned PYLINT_JOBS := $(NB_CORES) PYLINT_SUGGEST_FIX := y -PYLINT_COMMON_PARAMETERS := --jobs=$(PYLINT_JOBS) --suggestion-mode=$(PYLINT_SUGGEST_FIX) -PYLINT_GEF_PARAMETERS := --disable=$(PYLINT_DISABLE) --enable=$(PYLINT_ENABLE) $(PYLINT_COMMON_PARAMETERS) -PYLINT_TEST_PARAMETERS := --disable=$(PYLINT_DISABLE) --enable=$(PYLINT_TEST_ENABLE) $(PYLINT_COMMON_PARAMETERS) +PYLINT_PY_VERSION := 3.6 +PYLINT_PARAMETERS := --jobs=$(PYLINT_JOBS) --suggestion-mode=$(PYLINT_SUGGEST_FIX) --py-version=$(PYLINT_PY_VERSION) --rcfile=$(PYLINT_RC) TARGET := $(shell lscpu | head -1 | sed -e 's/Architecture:\s*//g') COVERAGE_DIR ?= /tmp/cov GEF_PATH ?= $(shell readlink -f gef.py) @@ -29,8 +28,8 @@ clean: @rm -rf $(TMPDIR)/gef-* $(TMPDIR)/gef.py || true lint: - python3 -m pylint $(PYLINT_GEF_PARAMETERS) $(GEF_PATH) - python3 -m pylint $(PYLINT_TEST_PARAMETERS) $(wildcard tests/*.py tests/*/*.py) + python3 -m pylint $(PYLINT_PARAMETERS) $(GEF_PATH) + python3 -m pylint $(PYLINT_PARAMETERS) $(wildcard tests/*/*.py) coverage: @! ( [ -d $(COVERAGE_DIR) ] && echo "COVERAGE_DIR=$(COVERAGE_DIR) exists already") diff --git a/gef.py b/gef.py index 57b691b86..63704bfb2 100644 --- a/gef.py +++ b/gef.py @@ -113,7 +113,7 @@ def update_gef(argv: List[str]) -> int: try: - import gdb # pylint: disable= + import gdb # type:ignore except ImportError: # if out of gdb, the only action allowed is to update gef.py if len(sys.argv) >= 2 and sys.argv[1].lower() in ("--update", "--upgrade"): @@ -130,7 +130,7 @@ def update_gef(argv: List[str]) -> int: DEFAULT_PAGE_ALIGN_SHIFT = 12 DEFAULT_PAGE_SIZE = 1 << DEFAULT_PAGE_ALIGN_SHIFT -GEF_RC = (pathlib.Path(os.getenv("GEF_RC")).absolute() +GEF_RC = (pathlib.Path(os.getenv("GEF_RC", "")).absolute() if os.getenv("GEF_RC") else pathlib.Path().home() / ".gef.rc") GEF_TEMP_DIR = os.path.join(tempfile.gettempdir(), "gef") @@ -154,7 +154,7 @@ def update_gef(argv: List[str]) -> int: PATTERN_LIBC_VERSION = re.compile(rb"glibc (\d+)\.(\d+)") -gef : "Gef" = None +gef : "Gef" __registered_commands__ : List[Type["GenericCommand"]] = [] __registered_functions__ : List[Type["GenericFunction"]] = [] __registered_architectures__ : Dict[Union["Elf.Abi", str], Type["Architecture"]] = {} @@ -376,8 +376,10 @@ def deprecated(solution: str = "") -> Callable: def decorator(f: Callable) -> Callable: @functools.wraps(f) def wrapper(*args: Any, **kwargs: Any) -> Any: - if gef.config["gef.show_deprecation_warnings"] is True: - msg = f"'{f.__name__}' is deprecated and will be removed in a feature release. " + msg = f"'{f.__name__}' is deprecated and will be removed in a feature release. " + if not gef: + print(msg) + elif gef.config["gef.show_deprecation_warnings"] is True: if solution: msg += solution warn(msg) @@ -426,13 +428,13 @@ def FakeExit(*args: Any, **kwargs: Any) -> NoReturn: def parse_arguments(required_arguments: Dict[Union[str, Tuple[str, str]], Any], - optional_arguments: Dict[Union[str, Tuple[str, str]], Any]) -> Optional[Callable]: + optional_arguments: Dict[Union[str, Tuple[str, str]], Any]) -> Callable: """Argument parsing decorator.""" def int_wrapper(x: str) -> int: return int(x, 0) def decorator(f: Callable) -> Optional[Callable]: - def wrapper(*args: Any, **kwargs: Any) -> Optional[Callable]: + def wrapper(*args: Any, **kwargs: Any) -> Callable: parser = argparse.ArgumentParser(prog=args[0]._cmdline_, add_help=True) for argname in required_arguments: argvalue = required_arguments[argname] @@ -440,7 +442,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Optional[Callable]: if argtype is int: argtype = int_wrapper - argname_is_list = isinstance(argname, list) or isinstance(argname, tuple) + argname_is_list = not isinstance(argname, str) if not argname_is_list and argname.startswith("-"): # optional args if argtype is bool: @@ -457,13 +459,12 @@ def wrapper(*args: Any, **kwargs: Any) -> Optional[Callable]: parser.add_argument(argname, type=argtype, default=argvalue, nargs=nargs) for argname in optional_arguments: - argname_is_list = isinstance(argname, list) or isinstance(argname, tuple) - if not argname_is_list and not argname.startswith("-"): + if isinstance(argname, str) and not argname.startswith("-"): # refuse positional arguments continue argvalue = optional_arguments[argname] argtype = type(argvalue) - if not argname_is_list: + if isinstance(argname, str): argname = [argname,] if argtype is int: argtype = int_wrapper @@ -472,10 +473,7 @@ def wrapper(*args: Any, **kwargs: Any) -> Optional[Callable]: else: parser.add_argument(*argname, type=argtype, default=argvalue) - try: - parsed_args = parser.parse_args(*(args[1:])) - except RuntimeWarning: - return + parsed_args = parser.parse_args(*(args[1:])) kwargs["arguments"] = parsed_args return f(*args, **kwargs) return wrapper @@ -1220,7 +1218,7 @@ class GlibcHeapInfo: See https://github.com/bminor/glibc/blob/glibc-2.34/malloc/arena.c#L64""" def __init__(self, addr: Union[int, str]) -> None: - self.__addr = addr if type(addr) is int else parse_address(addr) + self.__addr = parse_address(addr) if isinstance(addr, str) else addr self.size_t = cached_lookup_type("size_t") if not self.size_t: ptr_type = "unsigned long" if gef.arch.ptrsize == 8 else "unsigned int" @@ -1699,7 +1697,7 @@ def gef_pybytes(x: str) -> bytes: @lru_cache() -def which(program: str) -> Optional[pathlib.Path]: +def which(program: str) -> pathlib.Path: """Locate a command on the filesystem.""" for path in os.environ["PATH"].split(os.pathsep): dirname = pathlib.Path(path) @@ -2130,17 +2128,52 @@ def get_zone_base_address(name: str) -> Optional[int]: # # Architecture classes # +@deprecated("Using the decorator `register_architecture` is unecessary") def register_architecture(cls: Type["Architecture"]) -> Type["Architecture"]: - """Class decorator for declaring an architecture to GEF.""" - global __registered_architectures__ - for key in cls.aliases: - __registered_architectures__[key] = cls return cls +class ArchitectureBase: + """Class decorator for declaring an architecture to GEF.""" + aliases: Union[Tuple[()], Tuple[Union[str, Elf.Abi], ...]] = () + + def __init_subclass__(cls: Type["ArchitectureBase"], **kwargs): + global __registered_architectures__ + super().__init_subclass__(**kwargs) + for key in getattr(cls, "aliases"): + if issubclass(cls, Architecture): + __registered_architectures__[key] = cls + return + -class Architecture(metaclass=abc.ABCMeta): +class Architecture(ArchitectureBase): """Generic metaclass for the architecture supported by GEF.""" + # Mandatory defined attributes by inheriting classes + arch: str + mode: str + all_registers: Union[Tuple[()], Tuple[str, ...]] + nop_insn: bytes + return_register: str + flag_register: Optional[str] + instruction_length: Optional[int] + flags_table: Dict[int, str] + syscall_register: Optional[str] + syscall_instructions: Union[Tuple[()], Tuple[str, ...]] + function_parameters: Union[Tuple[()], Tuple[str, ...]] + + # Optionally defined attributes + _ptrsize: Optional[int] = None + _endianness: Optional[Endianness] = None + special_registers: Union[Tuple[()], Tuple[str, ...]] = () + + def __init_subclass__(cls, **kwargs): + super().__init_subclass__(**kwargs) + attributes = ("arch", "mode", "aliases", "all_registers", "nop_insn", + "return_register", "flag_register", "instruction_length", "flags_table", + "function_parameters",) + if not all(map(lambda x: hasattr(cls, x), attributes)): + raise NotImplementedError + @staticmethod def supports_gdb_arch(gdb_arch: str) -> Optional[bool]: """If implemented by a child `Architecture`, this function dictates if the current class @@ -2148,69 +2181,54 @@ def supports_gdb_arch(gdb_arch: str) -> Optional[bool]: function will override any assumption made by GEF to determine the architecture.""" return None - @abc.abstractproperty - def all_registers(self) -> List[str]: pass - @abc.abstractproperty - def instruction_length(self) -> Optional[int]: pass - @abc.abstractproperty - def nop_insn(self) -> bytes: pass - @abc.abstractproperty - def return_register(self) -> str: pass - @abc.abstractproperty - def flag_register(self) -> Optional[str]: pass - @abc.abstractproperty - def flags_table(self) -> Optional[Dict[int, str]]: pass - @abc.abstractproperty - def function_parameters(self) -> List[str]: pass - @abc.abstractmethod - def flag_register_to_human(self, val: Optional[int] = None) -> str: pass - @abc.abstractmethod - def is_call(self, insn: Instruction) -> bool: pass - @abc.abstractmethod - def is_ret(self, insn: Instruction) -> bool: pass - @abc.abstractmethod - def is_conditional_branch(self, insn: Instruction) -> bool: pass - @abc.abstractmethod - def is_branch_taken(self, insn: Instruction) -> Tuple[bool, str]: pass - @abc.abstractmethod - def get_ra(self, insn: Instruction, frame: "gdb.Frame") -> Optional[int]: pass - @classmethod - @abc.abstractmethod - def mprotect_asm(cls, addr: int, size: int, perm: Permission) -> str: pass + def flag_register_to_human(self, val: Optional[int] = None) -> str: + raise NotImplementedError - arch = "" - mode = "" - aliases: Tuple[Union[str, int], ...] = [] - special_registers: List[str] = [] + def is_call(self, insn: Instruction) -> bool: + raise NotImplementedError + + def is_ret(self, insn: Instruction) -> bool: + raise NotImplementedError + + def is_conditional_branch(self, insn: Instruction) -> bool: + raise NotImplementedError + + def is_branch_taken(self, insn: Instruction) -> Tuple[bool, str]: + raise NotImplementedError + + def get_ra(self, insn: Instruction, frame: "gdb.Frame") -> Optional[int]: + raise NotImplementedError + + @classmethod + def mprotect_asm(cls, addr: int, size: int, perm: Permission) -> str: + raise NotImplementedError def reset_caches(self) -> None: self.__get_register_for_selected_frame.cache_clear() return - def __get_register(self, regname: str) -> Optional[int]: + def __get_register(self, regname: str) -> int: """Return a register's value.""" curframe = gdb.selected_frame() key = curframe.pc() ^ int(curframe.read_register('sp')) # todo: check when/if gdb.Frame implements `level()` return self.__get_register_for_selected_frame(regname, key) @lru_cache() - def __get_register_for_selected_frame(self, regname: str, hash_key: int) -> Optional[int]: + def __get_register_for_selected_frame(self, regname: str, hash_key: int) -> int: # 1st chance try: return parse_address(regname) except gdb.error: pass - # 2nd chance - try: - regname = regname.lstrip("$") - value = gdb.selected_frame().read_register(regname) - return int(value) - except (ValueError, gdb.error): - pass - return None + # 2nd chance - if an exception, propagate it + regname = regname.lstrip("$") + value = gdb.selected_frame().read_register(regname) + return int(value) - def register(self, name: str) -> Optional[int]: + def register(self, name: str) -> int: + if not is_alive(): + raise gdb.error("No debugging session active") return self.__get_register(name) @property @@ -2218,18 +2236,17 @@ def registers(self) -> Generator[str, None, None]: yield from self.all_registers @property - def pc(self) -> Optional[int]: + def pc(self) -> int: return self.register("$pc") @property - def sp(self) -> Optional[int]: + def sp(self) -> int: return self.register("$sp") @property - def fp(self) -> Optional[int]: + def fp(self) -> int: return self.register("$fp") - _ptrsize = None @property def ptrsize(self) -> int: if not self._ptrsize: @@ -2240,7 +2257,6 @@ def ptrsize(self) -> int: self._ptrsize = gdb.parse_and_eval("$pc").type.sizeof return self._ptrsize - _endianness = None @property def endianness(self) -> Endianness: if not self._endianness: @@ -2262,51 +2278,37 @@ def get_ith_parameter(self, i: int, in_func: bool = True) -> Tuple[str, Optional class GenericArchitecture(Architecture): - arch = "Generic" mode = "" + aliases = ("GenericArchitecture",) all_registers = () instruction_length = 0 - ptrsize = 0 return_register = "" function_parameters = () syscall_register = "" syscall_instructions = () nop_insn = b"" flag_register = None - flags_table = None - def flag_register_to_human(self, val: Optional[int] = None) -> str: return "" - def is_call(self, insn: Instruction) -> bool: return False - def is_ret(self, insn: Instruction) -> bool: return False - def is_conditional_branch(self, insn: Instruction) -> bool: return False - def is_branch_taken(self, insn: Instruction) -> Tuple[bool, str]: return False, "" - def get_ra(self, insn: Instruction, frame: "gdb.Frame") -> Optional[int]: return 0 - - @classmethod - def mprotect_asm(cls, addr: int, size: int, perm: Permission) -> str: - raise OSError(f"Architecture {cls.arch} not supported") + flags_table = {} -@register_architecture class RISCV(Architecture): arch = "RISCV" mode = "RISCV" aliases = ("RISCV", Elf.Abi.RISCV) - - all_registers = ["$zero", "$ra", "$sp", "$gp", "$tp", "$t0", "$t1", + all_registers = ("$zero", "$ra", "$sp", "$gp", "$tp", "$t0", "$t1", "$t2", "$fp", "$s1", "$a0", "$a1", "$a2", "$a3", "$a4", "$a5", "$a6", "$a7", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7", "$s8", "$s9", "$s10", "$s11", - "$t3", "$t4", "$t5", "$t6",] + "$t3", "$t4", "$t5", "$t6",) return_register = "$a0" - function_parameters = ["$a0", "$a1", "$a2", "$a3", "$a4", "$a5", "$a6", "$a7"] + function_parameters = ("$a0", "$a1", "$a2", "$a3", "$a4", "$a5", "$a6", "$a7") syscall_register = "$a7" - syscall_instructions = ["ecall"] + syscall_instructions = ("ecall",) nop_insn = b"\x00\x00\x00\x13" # RISC-V has no flags registers flag_register = None - flag_register_to_human = None - flags_table = None + flags_table = {} @property def instruction_length(self) -> int: @@ -2395,7 +2397,7 @@ def long_to_twos_complement(v: int) -> int: return taken, reason - def get_ra(self, insn: Instruction, frame: "gdb.Frame") -> int: + def get_ra(self, insn: Instruction, frame: "gdb.Frame") -> Optional[int]: ra = None if self.is_ret(insn): ra = gef.arch.register("$ra") @@ -2404,18 +2406,17 @@ def get_ra(self, insn: Instruction, frame: "gdb.Frame") -> int: return ra -@register_architecture class ARM(Architecture): aliases = ("ARM", Elf.Abi.ARM) arch = "ARM" - all_registers = ["$r0", "$r1", "$r2", "$r3", "$r4", "$r5", "$r6", + all_registers = ("$r0", "$r1", "$r2", "$r3", "$r4", "$r5", "$r6", "$r7", "$r8", "$r9", "$r10", "$r11", "$r12", "$sp", - "$lr", "$pc", "$cpsr",] + "$lr", "$pc", "$cpsr",) # https://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0041c/Caccegih.html nop_insn = b"\x01\x10\xa0\xe1" # mov r1, r1 return_register = "$r0" - flag_register = "$cpsr" + flag_register: str = "$cpsr" flags_table = { 31: "negative", 30: "zero", @@ -2425,10 +2426,10 @@ class ARM(Architecture): 6: "fast", 5: "thumb", } - function_parameters = ["$r0", "$r1", "$r2", "$r3"] + function_parameters = ("$r0", "$r1", "$r2", "$r3") syscall_register = "$r7" - syscall_instructions = ["swi 0x0", "swi NR"] - endianness = Endianness.LITTLE_ENDIAN + syscall_instructions = ("swi 0x0", "swi NR") + _endianness = Endianness.LITTLE_ENDIAN def is_thumb(self) -> bool: """Determine if the machine is currently in THUMB mode.""" @@ -2490,14 +2491,14 @@ def is_branch_taken(self, insn: Instruction) -> Tuple[bool, str]: taken, reason = False, "" if mnemo.endswith("eq"): taken, reason = bool(val&(1< Tuple[bool, str]: elif mnemo.endswith("pl"): taken, reason = not val&(1< str: return "; ".join(insns) -@register_architecture class AARCH64(ARM): aliases = ("ARM64", "AARCH64", Elf.Abi.AARCH64) arch = "ARM64" - mode = "" + mode: str = "" - all_registers = [ + all_registers = ( "$x0", "$x1", "$x2", "$x3", "$x4", "$x5", "$x6", "$x7", "$x8", "$x9", "$x10", "$x11", "$x12", "$x13", "$x14","$x15", "$x16", "$x17", "$x18", "$x19", "$x20", "$x21", "$x22", "$x23", "$x24", "$x25", "$x26", "$x27", "$x28", "$x29", "$x30", "$sp", - "$pc", "$cpsr", "$fpsr", "$fpcr",] + "$pc", "$cpsr", "$fpsr", "$fpcr",) return_register = "$x0" flag_register = "$cpsr" flags_table = { @@ -2570,10 +2570,11 @@ class AARCH64(ARM): 7: "interrupt", 6: "fast", } - function_parameters = ["$x0", "$x1", "$x2", "$x3", "$x4", "$x5", "$x6", "$x7"] + function_parameters = ("$x0", "$x1", "$x2", "$x3", "$x4", "$x5", "$x6", "$x7",) syscall_register = "$x8" - syscall_instructions = ["svc $x0"] - ptrsize = 8 + syscall_instructions = ("svc $x0",) + + _ptrsize = 8 def is_call(self, insn: Instruction) -> bool: mnemo = insn.mnemonic @@ -2649,20 +2650,19 @@ def is_branch_taken(self, insn: Instruction) -> Tuple[bool, str]: return taken, reason -@register_architecture class X86(Architecture): aliases: Tuple[Union[str, Elf.Abi], ...] = ("X86", Elf.Abi.X86_32) arch = "X86" mode = "32" nop_insn = b"\x90" - flag_register = "$eflags" - special_registers = ["$cs", "$ss", "$ds", "$es", "$fs", "$gs", ] - gpr_registers = ["$eax", "$ebx", "$ecx", "$edx", "$esp", "$ebp", "$esi", "$edi", "$eip", ] - all_registers = gpr_registers + [ flag_register, ] + special_registers + flag_register: str = "$eflags" + special_registers = ("$cs", "$ss", "$ds", "$es", "$fs", "$gs", ) + gpr_registers = ("$eax", "$ebx", "$ecx", "$edx", "$esp", "$ebp", "$esi", "$edi", "$eip", ) + all_registers = gpr_registers + ( flag_register,) + special_registers instruction_length = None return_register = "$eax" - function_parameters = ["$esp", ] + function_parameters = ("$esp", ) flags_table = { 6: "zero", 0: "carry", @@ -2678,9 +2678,9 @@ class X86(Architecture): 21: "identification", } syscall_register = "$eax" - syscall_instructions = ["sysenter", "int 0x80"] - ptrsize = 4 - endianness = Endianness.LITTLE_ENDIAN + syscall_instructions = ("sysenter", "int 0x80") + _ptrsize = 4 + _endianness = Endianness.LITTLE_ENDIAN def flag_register_to_human(self, val: Optional[int] = None) -> str: reg = self.flag_register @@ -2715,38 +2715,38 @@ def is_branch_taken(self, insn: Instruction) -> Tuple[bool, str]: taken, reason = False, "" if mnemo in ("ja", "jnbe"): - taken, reason = not val&(1< Tuple[str, Optional return key, val -@register_architecture class X86_64(X86): aliases = ("X86_64", Elf.Abi.X86_64, "i386:x86-64") arch = "X86" mode = "64" - gpr_registers = [ + gpr_registers = ( "$rax", "$rbx", "$rcx", "$rdx", "$rsp", "$rbp", "$rsi", "$rdi", "$rip", - "$r8", "$r9", "$r10", "$r11", "$r12", "$r13", "$r14", "$r15", ] - all_registers = gpr_registers + [ X86.flag_register, ] + X86.special_registers + "$r8", "$r9", "$r10", "$r11", "$r12", "$r13", "$r14", "$r15", ) + all_registers = gpr_registers + ( X86.flag_register, ) + X86.special_registers return_register = "$rax" function_parameters = ["$rdi", "$rsi", "$rdx", "$rcx", "$r8", "$r9"] syscall_register = "$rax" syscall_instructions = ["syscall"] # We don't want to inherit x86's stack based param getter get_ith_parameter = Architecture.get_ith_parameter - ptrsize = 8 + _ptrsize = 8 @classmethod def mprotect_asm(cls, addr: int, size: int, perm: Permission) -> str: @@ -2828,22 +2827,21 @@ def mprotect_asm(cls, addr: int, size: int, perm: Permission) -> str: return "; ".join(insns) -@register_architecture class PowerPC(Architecture): aliases = ("PowerPC", Elf.Abi.POWERPC, "PPC") arch = "PPC" mode = "PPC32" - all_registers = [ + all_registers = ( "$r0", "$r1", "$r2", "$r3", "$r4", "$r5", "$r6", "$r7", "$r8", "$r9", "$r10", "$r11", "$r12", "$r13", "$r14", "$r15", "$r16", "$r17", "$r18", "$r19", "$r20", "$r21", "$r22", "$r23", "$r24", "$r25", "$r26", "$r27", "$r28", "$r29", "$r30", "$r31", - "$pc", "$msr", "$cr", "$lr", "$ctr", "$xer", "$trap",] + "$pc", "$msr", "$cr", "$lr", "$ctr", "$xer", "$trap",) instruction_length = 4 nop_insn = b"\x60\x00\x00\x00" # https://developer.ibm.com/articles/l-ppc/ return_register = "$r0" - flag_register = "$cr" + flag_register: str = "$cr" flags_table = { 3: "negative[0]", 2: "positive[0]", @@ -2855,11 +2853,12 @@ class PowerPC(Architecture): 29: "equal[7]", 28: "overflow[7]", } - function_parameters = ["$i0", "$i1", "$i2", "$i3", "$i4", "$i5"] + function_parameters = ("$i0", "$i1", "$i2", "$i3", "$i4", "$i5") syscall_register = "$r0" - syscall_instructions = ["sc"] + syscall_instructions = ("sc",) ptrsize = 4 + def flag_register_to_human(self, val: Optional[int] = None) -> str: # https://www.cebix.net/downloads/bebox/pem32b.pdf (% 2.1.3) if not val: @@ -2883,12 +2882,12 @@ def is_branch_taken(self, insn: Instruction) -> Tuple[bool, str]: flags = dict((self.flags_table[k], k) for k in self.flags_table) val = gef.arch.register(self.flag_register) taken, reason = False, "" - if mnemo == "beq": taken, reason = val&(1< Optional[int]: @@ -2925,7 +2924,6 @@ def mprotect_asm(cls, addr: int, size: int, perm: Permission) -> str: return ";".join(insns) -@register_architecture class PowerPC64(PowerPC): aliases = ("PowerPC64", Elf.Abi.POWERPC64, "PPC64") arch = "PPC" @@ -2933,7 +2931,6 @@ class PowerPC64(PowerPC): ptrsize = 8 -@register_architecture class SPARC(Architecture): """ Refs: - https://www.cse.scu.edu/~atkinson/teaching/sp05/259/sparc.pdf @@ -2942,16 +2939,16 @@ class SPARC(Architecture): arch = "SPARC" mode = "" - all_registers = [ + all_registers = ( "$g0", "$g1", "$g2", "$g3", "$g4", "$g5", "$g6", "$g7", "$o0", "$o1", "$o2", "$o3", "$o4", "$o5", "$o7", "$l0", "$l1", "$l2", "$l3", "$l4", "$l5", "$l6", "$l7", "$i0", "$i1", "$i2", "$i3", "$i4", "$i5", "$i7", - "$pc", "$npc", "$sp ", "$fp ", "$psr",] + "$pc", "$npc", "$sp ", "$fp ", "$psr",) instruction_length = 4 nop_insn = b"\x00\x00\x00\x00" # sethi 0, %g0 return_register = "$i0" - flag_register = "$psr" + flag_register: str = "$psr" flags_table = { 23: "negative", 22: "zero", @@ -2960,9 +2957,9 @@ class SPARC(Architecture): 7: "supervisor", 5: "trap", } - function_parameters = ["$o0 ", "$o1 ", "$o2 ", "$o3 ", "$o4 ", "$o5 ", "$o7 ",] + function_parameters = ("$o0 ", "$o1 ", "$o2 ", "$o3 ", "$o4 ", "$o5 ", "$o7 ",) syscall_register = "%g1" - syscall_instructions = ["t 0x10"] + syscall_instructions = ("t 0x10",) def flag_register_to_human(self, val: Optional[int] = None) -> str: # https://www.gaisler.com/doc/sparcv8.pdf @@ -2992,21 +2989,21 @@ def is_branch_taken(self, insn: Instruction) -> Tuple[bool, str]: val = gef.arch.register(self.flag_register) taken, reason = False, "" - if mnemo == "be": taken, reason = val&(1< str: return "; ".join(insns) -@register_architecture class SPARC64(SPARC): """Refs: - http://math-atlas.sourceforge.net/devel/assembly/abi_sysV_sparc.pdf @@ -3085,28 +3081,27 @@ def mprotect_asm(cls, addr: int, size: int, perm: Permission) -> str: return "; ".join(insns) -@register_architecture class MIPS(Architecture): aliases: Tuple[Union[str, Elf.Abi], ...] = ("MIPS", Elf.Abi.MIPS) arch = "MIPS" mode = "MIPS32" # https://vhouten.home.xs4all.nl/mipsel/r3000-isa.html - all_registers = [ + all_registers = ( "$zero", "$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3", "$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7", "$t8", "$t9", "$k0", "$k1", "$s8", "$pc", "$sp", "$hi", - "$lo", "$fir", "$ra", "$gp", ] + "$lo", "$fir", "$ra", "$gp", ) instruction_length = 4 - ptrsize = 4 + _ptrsize = 4 nop_insn = b"\x00\x00\x00\x00" # sll $0,$0,0 return_register = "$v0" flag_register = "$fcsr" flags_table = {} - function_parameters = ["$a0", "$a1", "$a2", "$a3"] + function_parameters = ("$a0", "$a1", "$a2", "$a3") syscall_register = "$v0" - syscall_instructions = ["syscall"] + syscall_instructions = ("syscall",) def flag_register_to_human(self, val: Optional[int] = None) -> str: return Color.colorify("No flag register", "yellow underline") @@ -3169,18 +3164,17 @@ def mprotect_asm(cls, addr: int, size: int, perm: Permission) -> str: return "; ".join(insns) -@register_architecture class MIPS64(MIPS): aliases = ("MIPS64",) arch = "MIPS" mode = "MIPS64" - ptrsize = 8 + _ptrsize = 8 @staticmethod def supports_gdb_arch(gdb_arch: str) -> Optional[bool]: return gdb_arch.startswith("mips") and gef.binary.e_class == Elf.Class.ELF_64_BITS -def copy_to_clipboard(data: str) -> None: +def copy_to_clipboard(data: bytes) -> None: """Helper function to submit data to the clipboard""" if sys.platform == "linux": xclip = which("xclip") @@ -3316,11 +3310,12 @@ def download_file(remote_path: str, use_cache: bool = False, local_name: Optiona local_path = str(local_path.absolute()) except gdb.error: # fallback memory view - with open(local_path, "w") as f: + with local_path.open("w") as f: if is_32bit(): f.write(f"00000000-ffffffff rwxp 00000000 00:00 0 {get_filepath()}\n") else: f.write(f"0000000000000000-ffffffffffffffff rwxp 00000000 00:00 0 {get_filepath()}\n") + local_path = str(local_path.absolute()) except Exception as e: err(f"download_file() failed: {e}") @@ -3512,11 +3507,10 @@ def load_libc_args() -> bool: gef.ui.libc_args_table[_arch_mode] = json.load(_libc_args) return True except FileNotFoundError: - del gef.ui.libc_args_table[_arch_mode] warn(f"Config context.libc_args is set but definition cannot be loaded: file {_libc_args_file} not found") except json.decoder.JSONDecodeError as e: - del gef.ui.libc_args_table[_arch_mode] warn(f"Config context.libc_args is set but definition cannot be loaded from file {_libc_args_file}: {e}") + gef.ui.libc_args_table[_arch_mode] = {} return False @@ -3542,7 +3536,7 @@ def get_terminal_size() -> Tuple[int, int]: import fcntl import termios try: - tty_rows, tty_columns = struct.unpack("hh", fcntl.ioctl(1, termios.TIOCGWINSZ, "1234")) + tty_rows, tty_columns = struct.unpack("hh", fcntl.ioctl(1, termios.TIOCGWINSZ, "1234")) # type: ignore return tty_rows, tty_columns except OSError: return 600, 100 @@ -3677,10 +3671,10 @@ def get_unicorn_registers(to_string: bool = False) -> Union[Dict[str, int], Dict return regs -def keystone_assemble(code: str, arch: int, mode: int, **kwargs: Any) -> Optional[Union[str, bytearray]]: +def keystone_assemble(code_str: str, arch: int, mode: int, **kwargs: Any) -> Optional[Union[str, bytearray]]: """Assembly encoding function based on keystone.""" keystone = sys.modules["keystone"] - code = gef_pybytes(code) + code = gef_pybytes(code_str) addr = kwargs.get("addr", 0x1000) try: @@ -3703,17 +3697,17 @@ def keystone_assemble(code: str, arch: int, mode: int, **kwargs: Any) -> Optiona @lru_cache() -def get_elf_headers(filename: Optional[str] = None) -> Optional[Elf]: - """Return an Elf object with info from `filename`. If not provided, will return - the currently debugged file.""" +def get_elf_headers(filename: Optional[str] = None) -> Elf: + """Return an `Elf` object of the currently debugged target. If no `filename` provided, it will + be determined from the current GDB session.""" if not filename: filename = get_filepath() - if not filename: - raise Exception("No file provided") + + if not filename: + raise FileNotFoundError("No file provided") if filename.startswith("target:"): - warn("Your file is remote, you should try using `gef-remote` instead") - return + raise RuntimeError("Your file is remote, you should try using `gef-remote` instead") return Elf(filename) @@ -3752,45 +3746,39 @@ def is_arch(arch: Elf.Abi) -> bool: return arch in gef.arch.aliases -def reset_architecture(arch: Optional[str] = None, default: Optional[str] = None) -> None: +def reset_architecture(arch: Optional[str] = None) -> None: """Sets the current architecture. - If an arch is explicitly specified, use that one, otherwise try to parse it - out of the current target. If that fails, and default is specified, select and - set that arch. - Raise an exception if the architecture cannot be set. - Does not return a value. + If an architecture is explicitly specified by parameter, try to use that one. If this fails, an `OSError` + exception will occur. + If no architecture is specified, then GEF will attempt to determine automatically based on the current + ELF target. If this fails, an `OSError` exception will occur. """ global gef arches = __registered_architectures__ + # check if the architecture is forced by parameter if arch: try: - gef.arch = arches[arch.upper()]() - return + gef.arch = arches[arch]() except KeyError: - raise OSError(f"Specified arch {arch.upper()} is not supported") - - if not gef.binary: - gef.binary = get_elf_headers() + valid_arch_names = (x.arch for x in arches.values()) + raise OSError(f"No class '{arch}' in the list of supported architectures ('{ ', '.join(valid_arch_names) }'") + return + # otherwise, check if an known architecture matches exactly the current binary gdb_arch = get_arch() - arch_name = gef.binary.e_machine if gef.binary else gdb_arch preciser_arch = next((a for a in arches.values() if a.supports_gdb_arch(gdb_arch)), None) if preciser_arch: gef.arch = preciser_arch() return + # last resort, use the info from elf header to find it from the known architectures try: + arch_name = gef.binary.e_machine if gef.binary else gdb_arch gef.arch = arches[arch_name]() except KeyError: - if default: - try: - gef.arch = arches[default.upper()]() - except KeyError: - raise OSError(f"CPU not supported, neither is default {default.upper()}") - else: - raise OSError(f"CPU type is currently not supported: {get_arch()}") + raise OSError(f"CPU type is currently not supported: {get_arch()}") return @@ -4042,8 +4030,8 @@ def get_process_maps() -> List[Section]: @deprecated("Use `reset_architecture`") -def set_arch(arch: Optional[str] = None, default: Optional[str] = None) -> None: - return reset_architecture(arch, default) +def set_arch(arch: Optional[str] = None, _: Optional[str] = None) -> None: + return reset_architecture(arch) # # GDB event hooking @@ -4533,38 +4521,53 @@ def pane_title(): return "example:pane" # # Commands # - +@deprecated("") def register_external_command(cls: Type["GenericCommand"]) -> Type["GenericCommand"]: """Registering function for new GEF (sub-)command to GDB.""" - __registered_commands__.append(cls) - gef.gdb.load(initial=False) - gef.gdb.doc.add_command_to_doc((cls._cmdline_, cls, None)) - gef.gdb.doc.refresh() return cls - +@deprecated("") def register_command(cls: Type["GenericCommand"]) -> Type["GenericCommand"]: """Decorator for registering new GEF (sub-)command to GDB.""" - __registered_commands__.append(cls) return cls - +@deprecated("") def register_priority_command(cls: Type["GenericCommand"]) -> Type["GenericCommand"]: """Decorator for registering new command with priority, meaning that it must loaded before the other generic commands.""" - __registered_commands__.insert(0, cls) return cls -def register_function(cls: Type["GenericFunction"]) -> Type["GenericFunction"]: - """Decorator for registering a new convenience function to GDB.""" - __registered_functions__.append(cls) - return cls +class GenericCommandBase: + def __init_subclass__(cls: Type["GenericCommandBase"], **kwargs): + global __registered_commands__ + super().__init_subclass__(**kwargs) + if hasattr(cls, "_cmdline_") and issubclass(cls, GenericCommand): + __registered_commands__.append(cls) + return +class GenericExternalCommandBase: + def __init_subclass__(cls: Type["GenericExternalCommandBase"], **kwargs): + super().__init_subclass__(**kwargs) + if hasattr(cls, "_cmdline_") and issubclass(cls, GenericCommand): + gef.gdb.load(initial=False) + gef.gdb.doc.add_command_to_doc((cls._cmdline_, cls, None)) + gef.gdb.doc.refresh() + return -class GenericCommand(gdb.Command, metaclass=abc.ABCMeta): +class GenericCommand(gdb.Command, GenericCommandBase): """This is an abstract class for invoking commands, should not be instantiated.""" + _cmdline_: str + _syntax_: str + _example_: str = "" + + def __init_subclass__(cls, **kwargs): + super().__init_subclass__(**kwargs) + attributes = ("_cmdline_", "_syntax_", ) + if not all(map(lambda x: hasattr(cls, x), attributes)): + raise NotImplementedError + def __init__(self, *args: Any, **kwargs: Any) -> None: self.pre_load() syntax = Color.yellowify("\nSyntax: ") + self._syntax_ @@ -4598,21 +4601,14 @@ def usage(self) -> None: err(f"Syntax\n{self._syntax_}") return - @abc.abstractproperty - def _cmdline_(self) -> Optional[str]: pass - - @abc.abstractproperty - def _syntax_(self) -> Optional[str]: pass - - @abc.abstractproperty - def _example_(self) -> str: return "" - - @abc.abstractmethod - def do_invoke(self, argv: List[str]) -> None: pass + def do_invoke(self, argv: List[str]) -> None: + raise NotImplementedError - def pre_load(self) -> None: pass + def pre_load(self) -> None: + return - def post_load(self) -> None: pass + def post_load(self) -> None: + return def __get_setting_name(self, name: str) -> str: clsname = self.__class__._cmdline_.replace(" ", "-") @@ -4683,7 +4679,6 @@ def __set_repeat_count(self, argv: List[str], from_tty: bool) -> None: return -@register_command class VersionCommand(GenericCommand): """Display GEF version info.""" @@ -4715,7 +4710,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class PrintFormatCommand(GenericCommand): """Print bytes format in commonly used formats, such as literals in high level languages.""" @@ -4750,7 +4744,7 @@ def format_matrix(self) -> Dict[int, Tuple[str, str, str]]: @parse_arguments({"location": "$pc", }, {("--length", "-l"): 256, "--bitlen": 0, "--lang": "py", "--clip": True,}) def do_invoke(self, _: List[str], **kwargs: Any) -> None: """Default value for print-format command.""" - args = kwargs["arguments"] + args: argparse.Namespace = kwargs["arguments"] args.bitlen = args.bitlen or gef.arch.ptrsize * 2 valid_bitlens = self.format_matrix.keys() @@ -4785,6 +4779,8 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: out = "buf {0} {1}".format(asm_type, sdata) elif args.lang == "hex": out = binascii.hexlify(gef.memory.read(start_addr, end_addr-start_addr)).decode() + else: + raise ValueError(f"Invalid format: {args.lang}") if args.clip: if copy_to_clipboard(gef_pybytes(out)): @@ -4796,7 +4792,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class PieCommand(GenericCommand): """PIE breakpoint support.""" @@ -4813,7 +4808,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class PieBreakpointCommand(GenericCommand): """Set a PIE breakpoint at an offset from the target binaries base address.""" @@ -4845,7 +4839,6 @@ def set_pie_breakpoint(set_func: Callable[[int], str], addr: int) -> None: return -@register_command class PieInfoCommand(GenericCommand): """Display breakpoint info.""" @@ -4871,7 +4864,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class PieDeleteCommand(GenericCommand): """Delete a PIE breakpoint.""" @@ -4905,7 +4897,6 @@ def delete_bp(breakpoints: List[PieVirtualBreakpoint]) -> None: return -@register_command class PieRunCommand(GenericCommand): """Run process with PIE breakpoint support.""" @@ -4948,7 +4939,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class PieAttachCommand(GenericCommand): """Do attach with PIE breakpoint support.""" @@ -4972,7 +4962,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class PieRemoteCommand(GenericCommand): """Attach to a remote connection with PIE breakpoint support.""" @@ -4996,7 +4985,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class SmartEvalCommand(GenericCommand): """SmartEval: Smart eval (vague approach to mimic WinDBG `?`).""" @@ -5062,7 +5050,6 @@ def distance(self, args: Tuple[str, str]) -> None: return -@register_command class CanaryCommand(GenericCommand): """Shows the canary value of the current process.""" @@ -5088,7 +5075,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class ProcessStatusCommand(GenericCommand): """Extends the info given by GDB `info proc`, by giving an exhaustive description of the process status (file descriptors, ancestor, descendants, etc.).""" @@ -5244,7 +5230,6 @@ def show_connections(self) -> None: return -@register_priority_command class GefThemeCommand(GenericCommand): """Customize GEF appearance.""" @@ -5502,7 +5487,6 @@ def find(self, structure_name: str) -> Optional[Tuple["ExternalStructureManager. return None -@register_command class PCustomCommand(GenericCommand): """Dump user defined structure. This command attempts to reproduce WinDBG awesome `dt` command for GDB and allows @@ -5557,7 +5541,6 @@ def explode_type(self, arg: str) -> Tuple[str, str]: return modname, structname -@register_command class PCustomListCommand(PCustomCommand): """PCustom: list available structures""" @@ -5581,7 +5564,6 @@ def do_invoke(self, _: List) -> None: return -@register_command class PCustomShowCommand(PCustomCommand): """PCustom: show the content of a given structure""" @@ -5609,7 +5591,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class PCustomEditCommand(PCustomCommand): """PCustom: edit the content of a given structure""" @@ -5655,7 +5636,6 @@ class {structname}(Structure): return -@register_command class ChangeFdCommand(GenericCommand): """ChangeFdCommand: redirect file descriptor during runtime.""" @@ -5727,7 +5707,6 @@ def get_fd_from_result(self, res: str) -> int: res = int(res.split()[2], 0) return res -@register_command class IdaInteractCommand(GenericCommand): """**REMOVED** a better version of `ida-interact` is now hosted on `gef-extras`""" _cmdline_ = "ida-interact" @@ -5738,7 +5717,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class ScanSectionCommand(GenericCommand): """Search for addresses that are located in a memory mapping (haystack) that belonging to another (needle).""" @@ -5806,7 +5784,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class SearchPatternCommand(GenericCommand): """SearchPatternCommand: search a pattern in memory. If given an hex value (starting with 0x) the command will also try to look for upwards cross-references to this address.""" @@ -5864,7 +5841,7 @@ def search_pattern_by_address(self, pattern: str, start_address: int, end_addres for match in re.finditer(_pattern, mem): start = chunk_addr + match.start() if is_ascii_string(start): - ustr = gef.memory.read_ascii_string(start) + ustr = gef.memory.read_ascii_string(start) or "" end = start + len(ustr) else: ustr = gef_pystring(_pattern) + "[...]" @@ -5931,7 +5908,7 @@ def do_invoke(self, argv: List[str]) -> None: else: section_name = argv[2] if section_name == "binary": - section_name = get_filepath() + section_name = get_filepath() or "" self.search_pattern(pattern, section_name) else: @@ -5940,7 +5917,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class FlagsCommand(GenericCommand): """Edit flags in a human friendly way.""" @@ -5951,6 +5927,10 @@ class FlagsCommand(GenericCommand): f"\n{_cmdline_} +zero # sets ZERO flag") def do_invoke(self, argv: List[str]) -> None: + if not gef.arch.flag_register: + warn(f"The architecture {gef.arch.arch}:{gef.arch.mode} doesn't have flag register.") + return + for flag in argv: if len(flag) < 2: continue @@ -5982,7 +5962,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class ChangePermissionCommand(GenericCommand): """Change a page permission. By default, it will change it to 7 (RWX).""" @@ -6059,7 +6038,6 @@ def get_stub_by_arch(self, addr: int, size: int, perm: Permission) -> Union[str, return raw_insns -@register_command class UnicornEmulateCommand(GenericCommand): """Use Unicorn-Engine to emulate the behavior of the binary, without affecting the GDB runtime. By default the command will emulate only the next instruction, but location and number of @@ -6312,7 +6290,6 @@ def emulate(emu, start_addr, end_addr): return -@register_command class RemoteCommand(GenericCommand): """gef wrapper for the `target remote` command. This command will automatically download the target binary in the local temporary directory (defaut /tmp) and then @@ -6440,7 +6417,7 @@ def setup_remote_environment(self, pid: int, update_solib: bool = False) -> None exepath = get_path_from_info_proc() infos["exe"] = download_file(f"/proc/{pid:d}/exe", use_cache=False, local_name=exepath) - if not os.access(infos["exe"], os.R_OK): + if not infos["exe"] or not os.access(infos["exe"], os.R_OK): err("Source binary is not readable") return @@ -6534,7 +6511,6 @@ def prepare_qemu_stub(self, target: str) -> None: return -@register_command class NopCommand(GenericCommand): """Patch the instruction(s) pointed by parameters with NOP. Note: this command is architecture aware.""" @@ -6572,7 +6548,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class StubCommand(GenericCommand): """Stub out the specified function. This function is useful when needing to skip one function to be called and disrupt your runtime flow (ex. fork).""" @@ -6596,7 +6571,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class CapstoneDisassembleCommand(GenericCommand): """Use capstone disassembly framework to disassemble code.""" @@ -6641,8 +6615,8 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: if insn.address == gef.arch.pc: msg = Color.colorify(f"{RIGHT_ARROW} {text_insn}", "bold red") - reason = self.capstone_analyze_pc(insn, length)[0] - if reason: + valid, reason = self.capstone_analyze_pc(insn, length) + if valid: gef_print(msg) gef_print(reason) break @@ -6673,7 +6647,6 @@ def capstone_analyze_pc(self, insn: Instruction, nb_insn: int) -> Tuple[bool, st return (False, "") -@register_command class GlibcHeapCommand(GenericCommand): """Base command to get information about the Glibc heap structure.""" @@ -6690,7 +6663,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class GlibcHeapSetArenaCommand(GenericCommand): """Display information on a heap chunk.""" @@ -6728,7 +6700,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class GlibcHeapArenaCommand(GenericCommand): """Display information on a heap chunk.""" @@ -6742,7 +6713,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class GlibcHeapChunkCommand(GenericCommand): """Display information on a heap chunk. See https://github.com/sploitfun/lsploits/blob/master/glibc/malloc/malloc.c#L1123.""" @@ -6786,7 +6756,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class GlibcHeapChunksCommand(GenericCommand): """Display all heap chunks for the current arena. As an optional argument the base address of a different arena can be passed""" @@ -6852,7 +6821,6 @@ def dump_chunks_heap(self, start: int, until: Optional[int] = None, top: Optiona return -@register_command class GlibcHeapBinsCommand(GenericCommand): """Display information on the bins on an arena (default: main_arena). See https://github.com/sploitfun/lsploits/blob/master/glibc/malloc/malloc.c#L1123.""" @@ -6908,7 +6876,6 @@ def pprint_bin(arena_addr: str, index: int, _type: str = "") -> int: return nb_chunk -@register_command class GlibcHeapTcachebinsCommand(GenericCommand): """Display information on the Tcachebins on an arena (default: main_arena). See https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=d5c3fafc4307c9b7a4c7d5cb381fcdbfad340bcc.""" @@ -7073,7 +7040,6 @@ def tcachebin(tcache_base: int, i: int) -> Tuple[Optional[GlibcChunk], int]: return chunk, count -@register_command class GlibcHeapFastbinsYCommand(GenericCommand): """Display information on the fastbinsY on an arena (default: main_arena). See https://github.com/sploitfun/lsploits/blob/master/glibc/malloc/malloc.c#L1123.""" @@ -7139,7 +7105,6 @@ def fastbin_index(sz: int) -> int: return -@register_command class GlibcHeapUnsortedBinsCommand(GenericCommand): """Display information on the Unsorted Bins of an arena (default: main_arena). See: https://github.com/sploitfun/lsploits/blob/master/glibc/malloc/malloc.c#L1689.""" @@ -7166,7 +7131,6 @@ def do_invoke(self, *_: Any, **kwargs: Any) -> None: return -@register_command class GlibcHeapSmallBinsCommand(GenericCommand): """Convenience command for viewing small bins.""" @@ -7198,7 +7162,6 @@ def do_invoke(self, *_: Any, **kwargs: Any) -> None: return -@register_command class GlibcHeapLargeBinsCommand(GenericCommand): """Convenience command for viewing large bins.""" @@ -7230,7 +7193,6 @@ def do_invoke(self, *_: Any, **kwargs: Any) -> None: return -@register_command class SolveKernelSymbolCommand(GenericCommand): """Solve kernel symbols from kallsyms table.""" @@ -7265,7 +7227,6 @@ def hex_to_int(num): return -@register_command class DetailRegistersCommand(GenericCommand): """Display full details on one, many or all registers value from current architecture.""" @@ -7359,7 +7320,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class ShellcodeCommand(GenericCommand): """ShellcodeCommand uses @JonathanSalwan simple-yet-awesome shellcode API to download shellcodes.""" @@ -7377,7 +7337,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class ShellcodeSearchCommand(GenericCommand): """Search pattern in shell-storm's shellcode database.""" @@ -7426,7 +7385,6 @@ def search_shellcode(self, search_options: List) -> None: return -@register_command class ShellcodeGetCommand(GenericCommand): """Download shellcode from shell-storm's shellcode database.""" @@ -7469,7 +7427,6 @@ def get_shellcode(self, sid: int) -> None: return -@register_command class RopperCommand(GenericCommand): """Ropper (https://scoding.de/ropper/) plugin.""" @@ -7493,13 +7450,16 @@ def do_invoke(self, argv: List[str]) -> None: ropper = sys.modules["ropper"] if "--file" not in argv: path = get_filepath() + if not path: + err("No file provided") + return sect = next(filter(lambda x: x.path == path, gef.memory.maps)) argv.append("--file") argv.append(path) argv.append("-I") argv.append(f"{sect.page_start:#x}") - import readline + readline = __import__("readline") # ropper set up own autocompleter after which gdb/gef autocomplete don't work old_completer_delims = readline.get_completer_delims() old_completer = readline.get_completer() @@ -7512,7 +7472,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class AssembleCommand(GenericCommand): """Inline code assemble. Architecture can be set in GEF runtime config. """ @@ -7647,7 +7606,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class ProcessListingCommand(GenericCommand): """List and filter process. If a PATTERN is given as argument, results shown will be grepped by this pattern.""" @@ -7712,7 +7670,6 @@ def get_processes(self) -> Generator[Dict[str, str], None, None]: return -@register_command class ElfInfoCommand(GenericCommand): """Display a limited subset of ELF header information. If no argument is provided, the command will show information about the current ELF being debugged.""" @@ -7788,7 +7745,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class EntryPointBreakCommand(GenericCommand): """Tries to find best entry point and sets a temporary breakpoint on it. The command will test for well-known symbols for entry points, such as `main`, `_main`, `__libc_start_main`, etc. defined by @@ -7868,7 +7824,6 @@ def set_init_tbreak_pie(self, addr: int, argv: List[str]) -> EntryBreakBreakpoin return self.set_init_tbreak(base_address + addr) -@register_command class NamedBreakpointCommand(GenericCommand): """Sets a breakpoint and assigns a name to it, which will be shown, when it's hit.""" @@ -7893,7 +7848,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class ContextCommand(GenericCommand): """Displays a comprehensive and modular summary of runtime context. Unless setting `enable` is set to False, this command will be spawned automatically every time GDB hits a breakpoint, a @@ -8599,7 +8553,6 @@ def empty_extra_messages(self, _) -> None: return -@register_command class MemoryCommand(GenericCommand): """Add or remove address ranges to the memory view.""" _cmdline_ = "memory" @@ -8615,7 +8568,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class MemoryWatchCommand(GenericCommand): """Adds address ranges to the memory view.""" _cmdline_ = "memory watch" @@ -8654,7 +8606,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class MemoryUnwatchCommand(GenericCommand): """Removes address ranges to the memory view.""" _cmdline_ = "memory unwatch" @@ -8681,7 +8632,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class MemoryWatchResetCommand(GenericCommand): """Removes all watchpoints.""" _cmdline_ = "memory reset" @@ -8694,7 +8644,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class MemoryWatchListCommand(GenericCommand): """Lists all watchpoints to display in context layout.""" _cmdline_ = "memory list" @@ -8712,7 +8661,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class HexdumpCommand(GenericCommand): """Display SIZE lines of hexdump from the memory location pointed by LOCATION.""" @@ -8790,7 +8738,6 @@ def _hexdump(self, start_addr: int, length: int, arrange_as: str, offset: int = return lines -@register_command class HexdumpQwordCommand(HexdumpCommand): """Display SIZE lines of hexdump as QWORD from the memory location pointed by ADDRESS.""" @@ -8804,7 +8751,6 @@ def __init__(self) -> None: return -@register_command class HexdumpDwordCommand(HexdumpCommand): """Display SIZE lines of hexdump as DWORD from the memory location pointed by ADDRESS.""" @@ -8818,7 +8764,6 @@ def __init__(self) -> None: return -@register_command class HexdumpWordCommand(HexdumpCommand): """Display SIZE lines of hexdump as WORD from the memory location pointed by ADDRESS.""" @@ -8832,7 +8777,6 @@ def __init__(self) -> None: return -@register_command class HexdumpByteCommand(HexdumpCommand): """Display SIZE lines of hexdump as BYTE from the memory location pointed by ADDRESS.""" @@ -8846,7 +8790,6 @@ def __init__(self) -> None: return -@register_command class PatchCommand(GenericCommand): """Write specified values to the specified address.""" @@ -8889,7 +8832,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class PatchQwordCommand(PatchCommand): """Write specified QWORD to the specified address.""" @@ -8903,7 +8845,6 @@ def __init__(self) -> None: return -@register_command class PatchDwordCommand(PatchCommand): """Write specified DWORD to the specified address.""" @@ -8917,7 +8858,6 @@ def __init__(self) -> None: return -@register_command class PatchWordCommand(PatchCommand): """Write specified WORD to the specified address.""" @@ -8931,7 +8871,6 @@ def __init__(self) -> None: return -@register_command class PatchByteCommand(PatchCommand): """Write specified WORD to the specified address.""" @@ -8945,7 +8884,6 @@ def __init__(self) -> None: return -@register_command class PatchStringCommand(GenericCommand): """Write specified string to the specified memory location pointed by ADDRESS.""" @@ -9036,7 +8974,6 @@ def dereference_from(addr: int) -> List[str]: return msg -@register_command class DereferenceCommand(GenericCommand): """Dereference recursively from an address and display information. This acts like WinDBG `dps` command.""" @@ -9120,7 +9057,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class ASLRCommand(GenericCommand): """View/modify the ASLR setting of GDB. By default, GDB will disable ASLR when it starts the process. (i.e. not attached). This command allows to change that setting.""" @@ -9162,7 +9098,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class ResetCacheCommand(GenericCommand): """Reset cache of all stored data. This command is here for debugging and test purposes, GEF handles properly the cache reset under "normal" scenario.""" @@ -9175,7 +9110,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class VMMapCommand(GenericCommand): """Display a comprehensive layout of the virtual memory mapping. If a filter argument, GEF will filter out the mapping whose pathname do not match that filter.""" @@ -9255,7 +9189,6 @@ def is_integer(self, n: str) -> bool: return True -@register_command class XFilesCommand(GenericCommand): """Shows all libraries (and sections) loaded by binary. This command extends the GDB command `info files`, by retrieving more information from extra sources, and providing a better @@ -9292,7 +9225,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class XAddressInfoCommand(GenericCommand): """Retrieve and display runtime information for the location(s) given as parameter.""" @@ -9354,7 +9286,6 @@ def infos(self, address: int) -> None: return -@register_command class XorMemoryCommand(GenericCommand): """XOR a block of memory. The command allows to simply display the result, or patch it runtime at runtime.""" @@ -9371,7 +9302,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class XorMemoryDisplayCommand(GenericCommand): """Display a block of memory pointed by ADDRESS by xor-ing each byte with KEY. The key must be provided in hexadecimal format.""" @@ -9400,7 +9330,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class XorMemoryPatchCommand(GenericCommand): """Patch a block of memory pointed by ADDRESS by xor-ing each byte with KEY. The key must be provided in hexadecimal format.""" @@ -9425,7 +9354,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class TraceRunCommand(GenericCommand): """Create a runtime trace of all instructions executed from $pc to LOCATION specified. The trace is stored in a text file that can be next imported in IDA Pro to visualize the runtime @@ -9515,7 +9443,6 @@ def start_tracing(self, loc_start: int, loc_end: int, depth: int) -> None: return -@register_command class PatternCommand(GenericCommand): """Generate or Search a De Bruijn Sequence of unique substrings of length N and a total length of LENGTH. The default value of N is set to match the @@ -9534,7 +9461,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class PatternCreateCommand(GenericCommand): """Generate a De Bruijn Sequence of unique substrings of length N and a total length of LENGTH. The default value of N is set to match the currently @@ -9556,7 +9482,6 @@ def do_invoke(self, _: List[str], **kwargs: Any) -> None: return -@register_command class PatternSearchCommand(GenericCommand): """Search a De Bruijn Sequence of unique substrings of length N and a maximum total length of MAX_LENGTH. The default value of N is set to match @@ -9622,7 +9547,6 @@ def search(self, pattern: str, size: int, period: int) -> None: return -@register_command class ChecksecCommand(GenericCommand): """Checksec the security properties of the current executable or passed as argument. The command checks for the following protections: @@ -9682,7 +9606,6 @@ def print_security_properties(self, filename: str) -> None: return -@register_command class GotCommand(GenericCommand): """Display current status of the got inside the process.""" @@ -9770,7 +9693,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class HighlightCommand(GenericCommand): """Highlight user-defined text matches in GEF output universally.""" _cmdline_ = "highlight" @@ -9785,7 +9707,6 @@ def do_invoke(self, _: List[str]) -> None: return self.usage() -@register_command class HighlightListCommand(GenericCommand): """Show the current highlight table with matches to colors.""" _cmdline_ = "highlight list" @@ -9807,7 +9728,6 @@ def do_invoke(self, _: List[str]) -> None: return self.print_highlight_table() -@register_command class HighlightClearCommand(GenericCommand): """Clear the highlight table, remove all matches.""" _cmdline_ = "highlight clear" @@ -9818,7 +9738,6 @@ def do_invoke(self, _: List[str]) -> None: return gef.ui.highlight_table.clear() -@register_command class HighlightAddCommand(GenericCommand): """Add a match to the highlight table.""" _cmdline_ = "highlight add" @@ -9835,7 +9754,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class HighlightRemoveCommand(GenericCommand): """Remove a match in the highlight table.""" _cmdline_ = "highlight remove" @@ -9857,7 +9775,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class FormatStringSearchCommand(GenericCommand): """Exploitable format-string helper: this command will set up specific breakpoints at well-known dangerous functions (printf, snprintf, etc.), and check if the pointer @@ -9889,7 +9806,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class HeapAnalysisCommand(GenericCommand): """Heap vulnerability analysis helper: this command aims to track dynamic heap allocation done through malloc()/free() to provide some insights on possible heap vulnerabilities. The @@ -9994,7 +9910,6 @@ def clean(self, _: "gdb.Event") -> None: return -@register_command class IsSyscallCommand(GenericCommand): """Tells whether the next instruction is a system call.""" _cmdline_ = "is-syscall" @@ -10006,7 +9921,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class SyscallArgsCommand(GenericCommand): """Gets the syscall name and arguments based on the register values in the current state.""" _cmdline_ = "syscall-args" @@ -10084,17 +9998,27 @@ def load_module(modname: str) -> Any: # # GDB Function declaration # -class GenericFunction(gdb.Function, metaclass=abc.ABCMeta): - """This is an abstract class for invoking convenience functions, should not be instantiated.""" +@deprecated("") +def register_function(cls: Type["GenericFunction"]) -> Type["GenericFunction"]: + """Decorator for registering a new convenience function to GDB.""" + return cls - _example_ = "" - @abc.abstractproperty - def _function_(self) -> str: pass +class GenericFunctionBase: + def __init_subclass__(cls: Type["GenericFunctionBase"], **kwargs): + global __registered_functions__ + super().__init_subclass__(**kwargs) + if hasattr(cls, "_function_") and issubclass(cls, GenericFunction): + __registered_functions__.append(cls) + return - @property - def _syntax_(self) -> str: - return f"${self._function_}([offset])" + +class GenericFunction(gdb.Function, GenericFunctionBase): + """This is an abstract class for invoking convenience functions, should not be instantiated.""" + + _function_ : str + _syntax_: str = "" + _example_ : str = "" def __init__(self) -> None: super().__init__(self._function_) @@ -10111,14 +10035,14 @@ def arg_to_long(self, args: List, index: int, default: int = 0) -> int: except IndexError: return default - @abc.abstractmethod - def do_invoke(self, args: List) -> int: pass + def do_invoke(self, args: Any) -> int: + raise NotImplementedError -@register_function class StackOffsetFunction(GenericFunction): """Return the current stack base address plus an optional offset.""" _function_ = "_stack" + _syntax_ = f"${_function_}()" def do_invoke(self, args: List) -> int: base = get_section_base_address("[stack]") @@ -10128,10 +10052,10 @@ def do_invoke(self, args: List) -> int: return self.arg_to_long(args, 0) + base -@register_function class HeapBaseFunction(GenericFunction): """Return the current heap base address plus an optional offset.""" _function_ = "_heap" + _syntax_ = f"${_function_}()" def do_invoke(self, args: List) -> int: base = gef.heap.base_address @@ -10142,7 +10066,6 @@ def do_invoke(self, args: List) -> int: return self.arg_to_long(args, 0) + base -@register_function class SectionBaseFunction(GenericFunction): """Return the matching file's base address plus an optional offset. Defaults to current file. Note that quotes need to be escaped""" @@ -10151,6 +10074,7 @@ class SectionBaseFunction(GenericFunction): _example_ = "p $_base(\\\"/usr/lib/ld-2.33.so\\\")" def do_invoke(self, args: List) -> int: + addr = 0 try: name = args[0].string() except IndexError: @@ -10160,17 +10084,19 @@ def do_invoke(self, args: List) -> int: return 0 try: - addr = int(get_section_base_address(name)) + base = get_section_base_address(name) + if base: + addr = int(base) except TypeError: err(f"Cannot find section {name}") return 0 return addr -@register_function class BssBaseFunction(GenericFunction): """Return the current bss base address plus the given offset.""" _function_ = "_bss" + _syntax_ = f"${_function_}([OFFSET])" _example_ = "deref $_bss(0x20)" def do_invoke(self, args: List) -> int: @@ -10180,10 +10106,10 @@ def do_invoke(self, args: List) -> int: return self.arg_to_long(args, 0) + base -@register_function class GotBaseFunction(GenericFunction): """Return the current GOT base address plus the given offset.""" _function_ = "_got" + _syntax_ = f"${_function_}([OFFSET])" _example_ = "deref $_got(0x20)" def do_invoke(self, args: List) -> int: @@ -10193,7 +10119,6 @@ def do_invoke(self, args: List) -> int: return base + self.arg_to_long(args, 0) -@register_command class GefFunctionsCommand(GenericCommand): """List the convenience functions provided by GEF.""" _cmdline_ = "functions" @@ -10757,7 +10682,6 @@ def lookup_command(self, cmd: str) -> Optional[Tuple[str, Type, Any]]: return None -@register_command class AliasesCommand(GenericCommand): """Base command to add, remove, or list aliases.""" @@ -10773,7 +10697,6 @@ def do_invoke(self, _: List[str]) -> None: return -@register_command class AliasesAddCommand(AliasesCommand): """Command to add aliases.""" @@ -10793,7 +10716,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class AliasesRmCommand(AliasesCommand): """Command to remove aliases.""" @@ -10819,7 +10741,6 @@ def do_invoke(self, argv: List[str]) -> None: return -@register_command class AliasesListCommand(AliasesCommand): """Command to list aliases.""" @@ -11481,6 +11402,7 @@ def reset_caches(self) -> None: pass # load GEF + gef = None reset() # gdb events configuration