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

Support for custom contexts in ContextCommand #727

Merged
merged 8 commits into from
Sep 27, 2021
Merged

Conversation

mahaloz
Copy link
Contributor

@mahaloz mahaloz commented Sep 25, 2021

Custom Context Support

Description/Motivation/Screenshots

This patch adds a new external api: register_external_context_pane, which takes three parameters:

  1. pane_name: str. The name that will be printed as the context panes title
  2. pane_function: function. The function that will be called to display things after the context title. Should use gef_print.
  3. pane_title_function: function. The function returns a string which will be used for the context pane title.

To be concise, this new api essentially allows you to pass it an arbitrary function that will be executed after displaying your new context's title. More things could be added to create a wrapper class, but overall this is good enough to allow users to display arbitrary text on each break event while living in the context command.

This patch will make the world better because it will allow users to have more freedom when using GEFs workflow for a specific target which requires a custom environment.

In this screenshot you can see that the last pane in the context is a custom context pane. You can also see that the pane is now a part of the actual context settings config:
image

Here is an example of the new api usage (also documented in the source):

__start_time__ = int(time.time())
def wasted_time_debugging():
    gef_print("You have wasted {} seconds!".format(int(time.time()) - __start_time__))

def wasted_time_debugging_title():
    return "wasted:time:debugging:{}".format(int(time.time()) - __start_time__)

register_external_context_pane("wasted_time_debugging", wasted_time_debugging, wasted_time_debugging_title)

Take this file and simply run source on it like a normal external command in gef.

I'll wait on adding the docs until the main devs like the general format of the external api.

How Has This Been Tested?

Architecture Yes/No Comments
x86-32 ✔️
x86-64 ✔️
ARM ✖️
AARCH64 ✖️
MIPS ✖️
POWERPC ✖️
SPARC ✖️
RISC-V ✖️
make test ✖️

Checklist

  • My PR was done against the dev branch, not master.
  • My code follows the code style of this project.
  • My change includes a change to the documentation, if required.
  • My change adds tests as appropriate.
  • I have read and agree to the CONTRIBUTING document.

@mahaloz
Copy link
Contributor Author

mahaloz commented Sep 25, 2021

Created to close: #726

@hugsy hugsy linked an issue Sep 25, 2021 that may be closed by this pull request
@hugsy
Copy link
Owner

hugsy commented Sep 25, 2021

I like this a lot and I will do a complete review this weekend, because I think it could simplify some existing code we use for the ContextCommand already (or even move out to gef-extras).

Quick initial: @mahaloz can you also add some docs about it (at least in API.md and maybe a word in commands/context.md) ?

@mahaloz
Copy link
Contributor Author

mahaloz commented Sep 25, 2021

@hugsy yup I can, though there was some discussion with @Grazfather about wether the user should be able to pass some explicit pane_title_function, which we will then call inside the context_title function in ContextCommand. Lmk know how you feel about it, because it could make this api look bloated:

register_external_context_pane(pane_name, pane_function, pane_title_function)

Though, I do personally like the idea of dynamic names like in the code pane.

@Grazfather
Copy link
Collaborator

dynamic pane names makes registration cleaner as well as the layout mapping.

I suppose it makes sense to allow users to select which of their contexts they want to show, I also just figured you could register a function and only allow the option to turn off / on ALL user contexts. Obviously this is less powerful but it's much simpler.

Copy link
Owner

@hugsy hugsy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR, some minor tweaks and we can merge

gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
@mahaloz
Copy link
Contributor Author

mahaloz commented Sep 26, 2021

@hugsy thanks :). All review changes have been addressed. I should also note that I modified the api a little since you last looked, so you may want to just check out the api again. I allowed users to pass a function that returns a string now so they can have dynamic pane titles, after discussion with @Grazfather. Feel free to DM me on discord for a immediate response @hugsy.

@mahaloz mahaloz requested a review from hugsy September 26, 2021 01:45
docs/api.md Outdated Show resolved Hide resolved
docs/commands/context.md Outdated Show resolved Hide resolved
docs/commands/context.md Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Show resolved Hide resolved
gef.py Show resolved Hide resolved
gef.py Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
gef.py Outdated Show resolved Hide resolved
@mahaloz
Copy link
Contributor Author

mahaloz commented Sep 26, 2021

@Grazfather take a look at my reply for the update_setting function review, since I based it on preexisting gef code (oops pinged wrong person initially)

Copy link
Owner

@hugsy hugsy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly minor stuff in the docs, you should be able to apply all the suggestions via GH if you agree.

docs/api.md Show resolved Hide resolved
docs/api.md Outdated Show resolved Hide resolved
docs/api.md Outdated Show resolved Hide resolved
docs/api.md Outdated Show resolved Hide resolved
docs/commands/context.md Outdated Show resolved Hide resolved
docs/commands/context.md Outdated Show resolved Hide resolved
docs/commands/context.md Outdated Show resolved Hide resolved
Co-authored-by: hugsy <hugsy@users.noreply.github.com>
Copy link
Owner

@hugsy hugsy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@theguy147 theguy147 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR. LGTM except the one little remark I gave. Also it would be even more cool if you could add some test for this: e.g. add a custom pane with some generated sequences as a title and the pane's content (different ones for each) and then test if those sequences are shown in the output. Just to prevent regression in the future.

gef.py Outdated Show resolved Hide resolved
docs/commands/context.md Outdated Show resolved Hide resolved
docs/commands/context.md Outdated Show resolved Hide resolved
docs/api.md Outdated Show resolved Hide resolved
docs/api.md Show resolved Hide resolved
docs/api.md Outdated Show resolved Hide resolved
docs/api.md Outdated Show resolved Hide resolved
@theguy147
Copy link
Collaborator

Sorry, I added a few more nitpicks

Co-authored-by: theguy147 <37738506+theguy147@users.noreply.github.com>
@mahaloz
Copy link
Contributor Author

mahaloz commented Sep 27, 2021

@theguy147, I addressed all the requests.

gef.py Show resolved Hide resolved
gef.py Show resolved Hide resolved
@mahaloz mahaloz changed the title Support for custom contexts for ContextCommand Support for custom contexts in ContextCommand Sep 27, 2021
@hugsy hugsy merged commit 3b0d9da into hugsy:dev Sep 27, 2021
@mahaloz mahaloz deleted the context_api branch September 28, 2021 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for adding custom Contexts to ContextCommand
4 participants