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

Gallopsled / pwntools / 1

18 May 2023 08:32PM UTC coverage: 19.336% (-54.2%) from 73.494%
1

push

github

web-flow
Handle `context.newline` correctly in `tube.interactive()` (#2129)

* Handle `context.newline` correctly in `tube.interactive()`

The `context.newline` or `self.newline` variable isn't obeyed when typing in interactive mode. It is used when sending and receiving lines through `tube.sendline` though, causing a mismatch.

The receiving end of the `tube.interactive()` already has handling of newlines, but the sending side does not.

Example:
```python
from pwn import *
io = process('cat')
io.newline = b'\r\n'
io.sendline(b'auto')
io.interactive()
```

```
$ python testinteractive.py DEBUG
[x] Starting local process '/usr/bin/cat' argv=[b'cat']
[+] Starting local process '/usr/bin/cat' argv=[b'cat'] : pid 19060
[DEBUG] Sent 0x6 bytes:
    b'auto\r\n'
[*] Switching to interactive mode
[DEBUG] Received 0x6 bytes:
    b'auto\r\n'
auto
$ test
[DEBUG] Sent 0x5 bytes:
    b'test\n'
[DEBUG] Received 0x5 bytes:
    b'test\n'
test
```

The expected outcome would be to send `b'test\r\n'.

The same problem arises in non-term mode (`PWNLIB_NOTERM=1`), where stdin is read in binary mode causing the OS line seperators to come through. Correctly replacing them with the `context.newline` setting allows to use the interactive input on windows hosts as well, without constantly sending `\r\n`.

* Update CHANGELOG.md

* Only replace newlines in TTYs

Don't mess with the line endings when piping data through.

* Always send bytes immediately on receive

Only replace newlines in a tty if NOTERM wasn't explicitly set or
the `newline` setting was set.

* Don't return a newline after term.readline lines

The builtin `input()` doesn't include the newline.

* Remove bogous newline from CHANGELOG

189 of 5940 branches covered (3.18%)

1 of 20 new or added lines in 2 files covered. (5.0%)

9283 existing lines in 112 files now uncovered.

3286 of 16994 relevant lines covered (19.34%)

0.39 hits per line

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

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

UNCOV
5
import shutil
×
UNCOV
6
from argparse import ArgumentParser
×
UNCOV
7
from subprocess import CalledProcessError
×
UNCOV
8
from subprocess import check_output
×
UNCOV
9
from tempfile import NamedTemporaryFile
×
10

UNCOV
11
import pwnlib.args
×
UNCOV
12
pwnlib.args.free_form = False
×
13

UNCOV
14
from pwn import *
×
UNCOV
15
from pwnlib.commandline import common
×
16

17

UNCOV
18
def dump(objdump, path):
×
19
    n = NamedTemporaryFile(delete=False)
×
20
    o = check_output([objdump,'-d','-x','-s',path])
×
21
    n.write(o)
×
22
    n.flush()
×
23
    return n.name
×
24

UNCOV
25
def diff(a,b):
×
26
    try: return check_output(['diff',a,b], universal_newlines=True)
×
27
    except CalledProcessError as e:
×
28
        return e.output
×
29

UNCOV
30
p = common.parser_commands.add_parser(
×
31
    'elfdiff',
32
    help = 'Compare two ELF files',
33
    description = 'Compare two ELF files'
34
)
35

UNCOV
36
p.add_argument('a')
×
UNCOV
37
p.add_argument('b')
×
38

UNCOV
39
def main(a):
×
40
    with context.silent:
×
41
        x = ELF(a.a)
×
42
        y = ELF(a.b)
×
43

44
    if x.arch != y.arch:
×
45
        log.error("Architectures are not the same: %s vs %s" % (x.arch, y.arch))
×
46

47
    context.arch = x.arch
×
48

49
    objdump = pwnlib.asm.which_binutils('objdump')
×
50

51
    tmp = NamedTemporaryFile()
×
52
    name = tmp.name
×
53

54
    shutil.copy(x.path, name)
×
55
    x = dump(objdump, name)
×
56

57
    shutil.copy(y.path, name)
×
58
    y = dump(objdump, name)
×
59

60
    print(diff(x, y))
×
61

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