-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLibc.py
214 lines (188 loc) · 6.66 KB
/
Libc.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
import random
import shutil
import argparse
import re
import subprocess
import wget
from pyunpack import Archive
from pwn import ELF
import uuid
import patoolib
pkd_url = "https://launchpad.net/ubuntu/+archive/primary/+files"
def libcVersion(path) -> tuple:
f = open(path, "rb")
_ = f.read()
f.close()
pattern = b"GLIBC (\d+\.\d+)-(\w+\d+(?:\.\d+)?)?"
res = re.search(pattern, _)
if res:
libcVersion = res.group(1).decode()
releaseNumber = res.group(2).decode()
return (libcVersion, releaseNumber)
else:
return ""
def extract(archive: str, extractPath: str, extractFiles: tuple = ()):
try:
Archive(archive).extractall(extractPath)
except:
print("err: extract()")
exit(1)
class LIBC(ELF):
# Ex: GNU C Library (Ubuntu GLIBC 2.27-3ubuntu1)
# "2.27" is libcVersion
# "3ubuntu1" is releaseNumber
def __init__(self, path):
super().__init__(path, checksec=0)
self.libcVersion, self.releaseNumber = libcVersion(path)
if (self.libcVersion == ""):
print("Ubuntu glibc not detected!")
exit(1)
self.libc6_bin_deb = "libc6_{}-{}_{}.deb".format(
self.libcVersion, self.releaseNumber, self.arch)
self.libc6_dbg_deb = "libc6-dbg_{}-{}_{}.deb".format(
self.libcVersion, self.releaseNumber, self.arch)
self.workDir = "/tmp/pwninit_{}".format(str(uuid.uuid4()))
self.dbgSym = "{}/dbgsym".format(self.workDir)
self.libcBin = "{}/libcbin".format(self.workDir)
if os.path.exists(self.workDir):
shutil.rmtree(self.workDir)
os.mkdir(self.workDir)
def __del__(self):
if os.path.exists(self.workDir):
shutil.rmtree(self.workDir)
def getLinker(self, path=".") -> ELF:
# get ld binary
_ = "{}/{}".format(pkd_url, self.libc6_bin_deb)
archive = "{}/{}".format(self.workDir, self.libc6_bin_deb)
wget.download(
_,
archive)
_ = self.libcBin
if not os.path.exists(_):
os.mkdir(_)
extract(archive, _)
try:
linkerPath = "{}/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2".format(
_)
if not os.path.exists(linkerPath):
linkerPath = "{}/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2".format(
_)
if not os.path.exists(linkerPath):
raise FileNotFoundError
ELF(linkerPath, checksec=False)
shutil.copy(linkerPath, path)
linker = ELF("{}/ld-linux-x86-64.so.2".format(path),
checksec=False)
except FileNotFoundError:
print("err: Can't find the linkerfile")
exit(1)
_ = "{}/{}".format(pkd_url, self.libc6_dbg_deb)
archive = "{}/{}".format(self.workDir, self.libc6_dbg_deb)
if not os.path.exists(archive):
wget.download(
_,
archive)
_ = self.dbgSym
if not os.path.exists(_):
os.mkdir(_)
extract(archive, _)
# try unstrip the linkerfile
try:
_ = subprocess.check_call(
[
"/usr/bin/eu-unstrip",
"-o", linker.path,
linker.path,
"{}/usr/lib/debug/lib/{}-linux-gnu/ld-{}.so".format(
self.dbgSym,
"x86_64" if self.arch == "amd64" else "i386",
self.libcVersion
)
],
stderr=open("/tmp/pwninit_log", "a+")
)
except subprocess.CalledProcessError:
_ = subprocess.check_call(
[
"/usr/bin/eu-unstrip",
"-o", linker.path,
linker.path,
"{}/usr/lib/debug/.build-id/{}/{}.debug".format(
self.dbgSym,
linker.buildid[:1].hex(),
linker.buildid[1:].hex()
)
],
stderr=open("/tmp/pwninit_log", "a+")
)
if _:
print("err {}: eu-unstrip".format(_))
exit(1)
return linker
def unstripLibc(self):
archive = "{}/{}".format(self.workDir, self.libc6_dbg_deb)
linkArchive = "{}/{}".format(pkd_url, self.libc6_dbg_deb)
if not os.path.exists(archive):
wget.download(
linkArchive,
archive)
_ = self.dbgSym
if not os.path.exists(_):
os.mkdir(_)
extract(archive, _)
try:
_ = subprocess.check_call(
[
"/usr/bin/eu-unstrip",
"-o", self.path,
self.path,
"{}/usr/lib/debug/lib/{}-linux-gnu/libc-{}.so".format(
self.dbgSym,
"x86_64" if self.arch == "amd64" else "i386",
self.libcVersion
)
],
stderr=open("/tmp/pwninit_log", "a+")
)
except subprocess.CalledProcessError:
_ = subprocess.check_call(
[
"/usr/bin/eu-unstrip",
"-o", self.path,
self.path,
"{}/usr/lib/debug/.build-id/{}/{}.debug".format(
self.dbgSym,
self.buildid[:1].hex(),
self.buildid[1:].hex()
)
],
stderr=open("/tmp/pwninit_log", "a+")
)
if _:
print("err {}: eu-unstrip".format(_))
exit(1)
def getSrc(self):
wget.download(
"http://archive.ubuntu.com/ubuntu/pool/main/g/glibc/glibc_{}.orig.tar.xz".format(self.libcVersion))
def main():
parser = argparse.ArgumentParser()
parser.add_argument("libc", metavar="<Libc file>")
parser.add_argument("-u", "--unstrip",
help="Unstrip the libc file", action="store_true")
parser.add_argument("-ld", "--get_linker",
help="Get the linker for libc", action="store_true")
parser.add_argument("-src", "--get_src",
help="Get soruce code of libc", action="store_true")
args = parser.parse_args()
if not args.libc:
return 1
libcObject = LIBC(args.libc)
if args.unstrip:
libcObject.unstripLibc()
if args.get_linker:
libcObject.getLinker()
if args.get_src:
libcObject.getSrc()
if __name__ == '__main__':
main()