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

Gallopsled / pwntools / 1

28 Jan 2022 10:58AM UTC coverage: 0.0% (-73.8%) from 73.823%
1

push

github

web-flow
Fix CI after Groovy Gorilla went away for libc unstrip test (#2025)

* Fix CI after Groovy Gorilla went away for libc unstrip test

Build elfutils 0.181 from source since we can't use builds
from a newer ubuntu version anymore.

* Install python wheels in CI

0 of 1 new or added line in 1 file covered. (0.0%)

13713 existing lines in 142 files now uncovered.

0 of 16559 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
UNCOV
2
from __future__ import absolute_import
×
UNCOV
3
from __future__ import division
×
4

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

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

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

UNCOV
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

UNCOV
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

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

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

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

UNCOV
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

UNCOV
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

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

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

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

UNCOV
84
        constant = args.constant
×
85

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

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

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

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

109
        # Output all matching constants
UNCOV
110
        for _, k in sorted(out):
×
UNCOV
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.
UNCOV
116
        if constant and args.mask_mode:
×
UNCOV
117
            mask = constant
×
UNCOV
118
            good = []
×
UNCOV
119
            out = [(v, k) for v, k in out if v != 0]
×
120

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

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

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

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