-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwntty.py
139 lines (104 loc) · 3.19 KB
/
pwntty.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
from random import randint
from termios import TIOCSTI
from time import sleep
import argparse
import fcntl
import os
import sys
VERBOSE = False
BANNER = r'''
. * . * .----. .
' ____ ______________ __ .---------. | == |
/ __ \_ " ______/_ __/_ __/\ \/ / |.-"""""-.| |----|
/ /_/ / | /| / / __ \/ / / / \ / || || | == |
/ ____/| |/ |/ / / / / / / / , / / * || || |----|
/_/ |__/|__/_/ /_/_/ /_/ /_/ |'-.....-'| |::::|
, `"")---(""` |____|
Created by @astreuzz /:::::::::::\" \'\
'''
print(BANNER)
def send_input(dev, data):
for ch in data:
fcntl.ioctl(dev, TIOCSTI, ch)
def write(dev, data):
os.write(dev, data.encode())
def bug_tty_cursor(dev, cur):
codes = lambda ch, n: f"{ch}\033[XZ".replace(
"X", str(n)).replace("Z", list("ABCD")[randint(0, 3)])
cur = cur.split()
try:
printv("+ Bug cursor started...")
while True:
for ch in cur:
write(dev, codes(ch, randint(0, 0x7f)))
except KeyboardInterrupt:
print("- Bug cursor stopped (CTL + C)", file=sys.stderr)
def lock_tty_io(dev):
cmd = "exec 2>&-\nclear\nexec >&-"
send_input(dev, cmd)
write(dev, "\033[9C")
printv(f"* The device '{dev}' was locked")
def parse_args():
parser = argparse.ArgumentParser(
prog="pwntty",
description="A toolkit to control TTY devices")
parser.add_argument(
"devices",
nargs="+",
help="Target TTY device",
metavar="DEVICES")
parser.add_argument(
"-e", "--exec",
nargs="?",
help="Run a given command line on target",
metavar="CMD")
parser.add_argument(
"-m", "--message",
nargs="?",
help="Send a message to target")
parser.add_argument(
"-b", "--bug-cursor",
nargs="?",
default="",
help="Turn on the bug cursor on target",
metavar="STR")
parser.add_argument(
"-l", "--lock-tty",
action="store_true")
parser.add_argument(
"-v", "--verbose",
action="store_true")
return parser.parse_args()
def printv(*msg):
if VERBOSE:
print(*msg)
def main():
args = parse_args()
devices = []
global VERBOSE
VERBOSE = args.verbose
if not os.getuid() == 0:
print("error: pwntty must be run as root", file=sys.stderr)
exit(1)
printv("* Checking devices...")
for dev in args.devices:
try:
fd = os.open(dev, os.O_RDWR)
devices.append(fd)
printv(f"+ The device '{dev}' is OK!")
except Exception as e:
print(f"- {e}", file=sys.stderr)
for dev in devices:
if args.exec:
send_input(dev, args.exec + "\n")
if args.message:
for ch in args.message:
write(dev, ch)
sleep(0.05)
if args.bug_cursor:
bug_tty_cursor(dev, cur=args.bug_cursor)
if args.lock_tty:
lock_tty_io(dev)
if __name__ == "__main__":
main()