• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Gallopsled / pwntools / 1

16 Jun 2020 04:28AM UTC coverage: 0.0% (-69.4%) from 69.405%
1

push

github

heapcrash
Re-enable UI tests in Github Actions

I suspect this will not work because of curses failing to initialize in GHA

0 of 15570 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/pwnlib/commandline/constgrep.py
1
#!/usr/bin/env python2
2
from __future__ import absolute_import
×
3
from __future__ import division
×
4

5
import argparse
×
6
import functools
×
7
import re
×
8

9
import pwnlib
×
10
pwnlib.args.free_form = False
×
11

12
from pwn import *
×
13
from pwnlib.commandline import common
×
14

15
p = common.parser_commands.add_parser(
×
16
    'constgrep',
17
    help = "Looking up constants from header files.\n\nExample: constgrep -c freebsd -m  ^PROT_ '3 + 4'",
18
    formatter_class = argparse.RawDescriptionHelpFormatter,
19
)
20

21
p.add_argument(
×
22
    '-e', '--exact',
23
    action='store_true',
24
    help='Do an exact match for a constant instead of searching for a regex',
25
)
26

27
p.add_argument(
×
28
    'regex',
29
    help='The regex matching constant you want to find',
30
)
31

32
p.add_argument(
×
33
    'constant',
34
    nargs = '?',
35
    default = None,
36
    type = safeeval.expr,
37
    help = 'The constant to find',
38
)
39

40
p.add_argument(
×
41
    '-i', '--case-insensitive',
42
    action = 'store_true',
43
    help = 'Search case insensitive',
44
)
45

46
p.add_argument(
×
47
    '-m', '--mask-mode',
48
    action = 'store_true',
49
    help = 'Instead of searching for a specific constant value, search for values not containing strictly less bits that the given value.',
50
)
51

52
p.add_argument(
×
53
    '-c', '--context',
54
    metavar = 'arch_or_os',
55
    action = 'append',
56
    type   = common.context_arg,
57
    choices = common.choices,
58
    help = 'The os/architecture/endianness/bits the shellcode will run in (default: linux/i386), choose from: %s' % common.choices,
59
)
60

61
def main(args):
×
62
    if args.exact:
×
63
        # This is the simple case
64
        print(cpp(args.regex).strip())
×
65
    else:
66
        # New we search in the right module.
67
        # But first: We find the right module
68
        if context.os == 'freebsd':
×
69
            mod = constants.freebsd
×
70
        else:
71
            mod = getattr(getattr(constants, context.os), context.arch)
×
72

73
        # Compile the given regex, for optimized lookup
74
        if args.case_insensitive:
×
75
            matcher = re.compile(args.regex, re.IGNORECASE)
×
76
        else:
77
            matcher = re.compile(args.regex)
×
78

79
        # The found matching constants and the length of the longest string
80
        out    = []
×
81
        maxlen = 0
×
82

83
        constant = args.constant
×
84

85
        for k in dir(mod):
×
86
            # No python stuff
87
            if k.endswith('__') and k.startswith('__'):
×
88
                continue
×
89

90
            # Run the regex
91
            if not matcher.search(k):
×
92
                continue
×
93

94
            # Check the constant
95
            if constant is not None:
×
96
                val = getattr(mod, k)
×
97
                if args.mask_mode:
×
98
                    if constant & val != val:
×
99
                        continue
×
100
                else:
101
                    if constant != val:
×
102
                        continue
×
103

104
            # Append it
105
            out.append((getattr(mod, k), k))
×
106
            maxlen = max(len(k), maxlen)
×
107

108
        # Output all matching constants
109
        for _, k in sorted(out):
×
110
            print('#define %s %s' % (k.ljust(maxlen), cpp(k).strip()))
×
111

112
        # If we are in match_mode, then try to find a combination of
113
        # constants that yield the exact given value
114
        # We do not want to find combinations using the value 0.
115
        if constant and args.mask_mode:
×
116
            mask = constant
×
117
            good = []
×
118
            out = [(v, k) for v, k in out if v != 0]
×
119

120
            while mask and out:
×
121
                cur = out.pop()
×
122
                mask &= ~cur[0]
×
123
                good.append(cur)
×
124

125
                out = [(v, k) for v, k in out if mask & v == v]
×
126

127
            if functools.reduce(lambda x, cur: x | cur[0], good, 0) == constant:
×
128
                print('')
×
129
                print('(%s) == %s' % (' | '.join(k for v, k in good), args.constant))
×
130

131
if __name__ == '__main__':
×
132
    pwnlib.commandline.common.main(__file__)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc