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

Gallopsled / pwntools / 10549534825

12 Aug 2024 04:39PM UTC coverage: 74.334% (-0.04%) from 74.374%
10549534825

push

github

peace-maker
Pin colored_traceback < 0.4 for Python 2

4451 of 7185 branches covered (61.95%)

12984 of 17467 relevant lines covered (74.33%)

0.74 hits per line

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

73.08
/pwnlib/commandline/asm.py
1
from __future__ import absolute_import
1✔
2
from __future__ import division
1✔
3

4
import argparse
1✔
5
import sys
1✔
6

7
import pwnlib.args
1✔
8
pwnlib.args.free_form = False
1✔
9

10
from pwn import *
1✔
11
from pwnlib.commandline import common
1✔
12

13
parser = common.parser_commands.add_parser(
1✔
14
    'asm',
15
    help = 'Assemble shellcode into bytes',
16
    description = 'Assemble shellcode into bytes',
17
)
18

19
parser.add_argument(
1✔
20
    'lines',
21
    metavar='line',
22
    nargs='*',
23
    help='Lines to assemble. If none are supplied, use stdin'
24
)
25

26
parser.add_argument(
1✔
27
    "-f", "--format",
28
    help="Output format (defaults to hex for ttys, otherwise raw)",
29
    choices=['raw', 'hex', 'string', 'elf']
30
)
31

32
parser.add_argument(
1✔
33
    "-o","--output",
34
    metavar='file',
35
    help="Output file (defaults to stdout)",
36
    type=argparse.FileType('wb'),
37
    default=getattr(sys.stdout, 'buffer', sys.stdout)
38
)
39

40
parser.add_argument(
1✔
41
    '-c', '--context',
42
    metavar = 'context',
43
    action = 'append',
44
    type   = common.context_arg,
45
    choices = common.choices,
46
    help = 'The os/architecture/endianness/bits the shellcode will run in (default: linux/i386), choose from: %s' % common.choices,
47
)
48

49
parser.add_argument(
1✔
50
    '-v', '--avoid',
51
    action='append',
52
    help = 'Encode the shellcode to avoid the listed bytes (provided as hex)'
53
)
54

55
parser.add_argument(
1✔
56
    '-n', '--newline',
57
    dest='avoid',
58
    action='append_const',
59
    const='0a',
60
    help = 'Encode the shellcode to avoid newlines'
61
)
62

63
parser.add_argument(
1✔
64
    '-z', '--zero',
65
    dest='avoid',
66
    action='append_const',
67
    const='00',
68
    help = 'Encode the shellcode to avoid NULL bytes'
69
)
70

71

72
parser.add_argument(
1✔
73
    '-d',
74
    '--debug',
75
    help='Debug the shellcode with GDB',
76
    action='store_true'
77
)
78

79
parser.add_argument(
1✔
80
    '-e',
81
    '--encoder',
82
    help="Specific encoder to use"
83
)
84

85
parser.add_argument(
1✔
86
    '-i',
87
    '--infile',
88
    help="Specify input file",
89
    default=sys.stdin,
90
    type=argparse.FileType('r')
91
)
92

93
parser.add_argument(
1✔
94
    '-r',
95
    '--run',
96
    help="Run output",
97
    action='store_true'
98
)
99

100
def main(args):
1✔
101
    tty    = args.output.isatty()
1✔
102

103
    if args.infile.isatty() and not args.lines:
1!
104
        parser.print_usage()
×
105
        sys.exit(1)
×
106

107
    data   = '\n'.join(args.lines) or args.infile.read()
1✔
108
    output = asm(data.replace(';', '\n'))
1✔
109
    fmt    = args.format or ('hex' if tty else 'raw')
1✔
110
    formatters = {'r':bytes, 'h':enhex, 's':repr}
1✔
111

112
    if args.avoid:
1!
113
        avoid = unhex(''.join(args.avoid))
×
114
        output = encode(output, avoid)
×
115

116
    if args.debug:
1!
117
        proc = gdb.debug_shellcode(output, arch=context.arch)
×
118
        proc.interactive()
×
119
        sys.exit(0)
×
120

121
    if args.run:
1!
122
        proc = run_shellcode(output)
×
123
        proc.interactive()
×
124
        sys.exit(0)
×
125

126
    if fmt[0] == 'e':
1!
127
        args.output.write(make_elf(output))
×
128
        try: os.fchmod(args.output.fileno(), 0o700)
×
129
        except OSError: pass
×
130
    else:
131
        output = formatters[fmt[0]](output)
1✔
132
        if not hasattr(output, 'decode'):
1✔
133
            output = output.encode('ascii')
1✔
134
        args.output.write(output)
1✔
135

136
    if tty and fmt != 'raw':
1!
137
        args.output.write(b'\n')
×
138

139
if __name__ == '__main__':
1✔
140
    pwnlib.commandline.common.main(__file__)
1✔
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

© 2025 Coveralls, Inc