forked from Gallopsled/pwntools
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pwnlib.config module and documentation (Gallopsled#893)
This adds functionality for user configuration files at ~/.pwn.conf and /etc/pwn.conf. Previously this was only used by the pwnlib.log module, and was entirely undocumented. This is now documented, and offers an easy mechanism for other parts of the code to have extension points.
- Loading branch information
1 parent
71855eb
commit 6585897
Showing
6 changed files
with
102 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
:mod:`pwnlib.config` --- Pwntools Configuration File | ||
==================================================== | ||
|
||
.. automodule:: pwnlib.config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Allows per-user and per-host configuration of Pwntools settings. | ||
The list of configurable options includes all of the logging symbols | ||
and colors, as well as all of the default values on the global context | ||
object. | ||
The configuration file is read from ``~/.pwn.conf`` and ``/etc/pwn.conf``. | ||
The configuration file is only read in ``from pwn import *`` mode, and not | ||
when used in library mode (``import pwnlib``). To read the configuration | ||
file in library mode, invoke :func:`.config.initialize`. | ||
The ``context`` section supports complex types, at least as far as is | ||
supported by ``pwnlib.util.safeeval.expr``. | ||
:: | ||
[log] | ||
success.symbol=😎 | ||
error.symbol=☠ | ||
info.color=blue | ||
[context] | ||
adb_port=4141 | ||
randomize=1 | ||
timeout=60 | ||
terminal=['x-terminal-emulator', '-e'] | ||
""" | ||
from __future__ import absolute_import | ||
|
||
import ConfigParser | ||
import os | ||
|
||
registered_configs = {} | ||
|
||
def register_config(section, function): | ||
"""Registers a configuration section. | ||
Arguments: | ||
section(str): Named configuration section | ||
function(callable): Function invoked with a dictionary of | ||
``{option: value}`` for the entries in the section. | ||
""" | ||
registered_configs[section] = function | ||
|
||
def initialize(): | ||
"""Read the configuration files.""" | ||
from pwnlib.log import getLogger | ||
log = getLogger(__name__) | ||
|
||
c = ConfigParser.ConfigParser() | ||
c.read(['/etc/pwn.conf', os.path.expanduser('~/.pwn.conf')]) | ||
|
||
for section in c.sections(): | ||
if section not in registered_configs: | ||
log.warn("Unknown configuration section %r" % section) | ||
continue | ||
settings = dict(c.items(section)) | ||
registered_configs[section](settings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters