pwnlib.log and — Logging stuff

Logging module for printing status during an exploit, and internally within pwntools.

Exploit Developers

By using the standard from pwn import *, an object named log will be inserted into the global namespace. You can use this to print out status messages during exploitation.

For example,:

log.info('Hello, world!')

prints:

[*] Hello, world!

Additionally, there are some nifty mechanisms for performing status updates on a running job (e.g. when brute-forcing).:

p = log.progress('Working')
p.status('Reticulating splines')
time.sleep(1)
p.success('Got a shell!')

The verbosity of logging can be most easily controlled by setting context.log_level on the global context object.:

log.info("No you see me")
context.log_level = 'error'
log.info("Now you don't")

Pwnlib Developers

A module-specific logger can be imported into the module via:

log = logging.getLogger(__name__)

This provides an easy way to filter logging programmatically or via a configuration file for debugging.

There’s no need to expressly import this log module.

When using progress, you should use the with keyword to manage scoping, to ensure the spinner stops if an exception is thrown.

class pwnlib.log.Logger(*args, **kwargs)[source]

Specialization of logging.Logger which uses pwnlib.context.context.log_level to infer verbosity.

Also adds some pwnlib flavor via:

Adds pwnlib-specific information for coloring and indentation to the log records passed to the logging.Formatter.

Internal:

Permits prepending a string to each message, by means of msg_prefix. This is leveraged for progress messages.
debug(message)[source]

Logs a debug message.

error(message)[source]

To be called outside an exception handler.

Logs an error message, then raises a PwnlibException.

failure(message)[source]

Logs a failure message. If the Logger is animated, the animation is stopped.

indented(message, level=logging.INFO)[source]

Log an info message without the line prefix.

Parameters:level (int) – Alternate log level at which to set the indented message.
info(message)[source]

Logs an info message.

info_once(message)[source]

Logs an info message. The same message is never printed again.

progress(self) → Logger[source]

Creates a Logger with a progress animation, which can be stopped via success(), and failure().

The Logger returned is also a scope manager. Using scope managers ensures that the animation is stopped, even if an exception is thrown.

::
with log.progress(‘Trying something...’) as p:
for i in range(10):
p.status(“At %i” % i) time.sleep(0.5)

x = 1/0

success(message)[source]

Logs a success message. If the Logger is animated, the animation is stopped.

warn(message)[source]

Logs a warning message.

warn_once(message)[source]

Logs a warning message. The same message is never printed again.