File: config.py

package info (click to toggle)
pwntools 4.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 18,436 kB
  • sloc: python: 59,156; ansic: 48,063; asm: 45,030; sh: 396; makefile: 256
file content (71 lines) | stat: -rw-r--r-- 2,127 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- 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``, ``$XDG_CONFIG_HOME/pwn.conf``
(``$XDG_CONFIG_HOME`` defaults to ``~/.config``, per XDG Base Directory Specification),
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']

    [update]
    interval=7
"""
from __future__ import absolute_import
from __future__ import division

from six.moves 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__)

    xdg_config_home = os.environ.get('XDG_CONFIG_HOME',
                                     os.path.expanduser("~/.config"))

    c = configparser.ConfigParser()
    c.read(['/etc/pwn.conf',
            os.path.join(xdg_config_home, '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)