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

Gallopsled / pwntools / 1

20 Aug 2023 06:49PM UTC coverage: 1.309% (-70.3%) from 71.623%
1

push

github

web-flow
plt: remove stale MIPS workaround (#2256)

Fixes #2042

2 of 5878 branches covered (0.03%)

221 of 16886 relevant lines covered (1.31%)

0.03 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.args
×
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
    description = "Looking up constants from header files.\n\nExample: constgrep -c freebsd -m  ^PROT_ '3 + 4'",
19
    formatter_class = argparse.RawDescriptionHelpFormatter,
20
)
21

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

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

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

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

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

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

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

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

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

84
        constant = args.constant
×
85

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

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

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

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

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

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

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

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

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

132
if __name__ == '__main__':
×
133
    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