forked from salopensource/sal-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_python_framework.py
executable file
·244 lines (206 loc) · 6.84 KB
/
sign_python_framework.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python3
# encoding: utf-8
"""
Based on https://github.com/ox-it/munki-rebrand
Original license is below
Copyright (C) University of Oxford 2016-21
Ben Goodstein <ben.goodstein at it.ox.ac.uk>
Based on an original script by Arjen van Bochoven
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import subprocess
import os
import stat
import shutil
from tempfile import mkdtemp
import plistlib
import argparse
import sys
import atexit
PYTHON_VERSION = "3.9.7"
SHORT_PYTHON_VERSION = "3.9"
PRODUCTSIGN = "/usr/bin/productsign"
CODESIGN = "/usr/bin/codesign"
global verbose
verbose = False
tmp_dir = mkdtemp()
@atexit.register
def cleanup():
print("Cleaning up...")
try:
shutil.rmtree(tmp_dir)
# In case subprocess cleans up before we do
except OSError:
pass
print("Done.")
def run_cmd(cmd, ret=None):
"""Runs a command passed in as a list. Can also be provided with a regex
to search for in the output, returning the result"""
if verbose:
print(f"Running command {cmd}")
proc = subprocess.run(cmd, capture_output=True)
if verbose and proc.stdout != b"" and not ret:
print(proc.stdout.rstrip().decode())
if proc.returncode != 0:
print(proc.stderr.rstrip().decode())
sys.exit(1)
if ret:
return proc.stdout.rstrip().decode()
def sign_package(signing_id, pkg):
"""Signs a pkg with a signing id"""
cmd = [PRODUCTSIGN, "--sign", signing_id, pkg, f"{pkg}-signed"]
print("Signing pkg...")
run_cmd(cmd)
print(f"Moving {pkg}-signed to {pkg}...")
os.rename(f"{pkg}-signed", pkg)
def sign_binary(
signing_id,
binary,
verbose=False,
deep=False,
options=[],
entitlements="",
force=False,
):
"""Signs a binary with a signing id, with optional arguments for command line
args"""
cmd = [CODESIGN, "--timestamp", "--sign", signing_id]
if force:
cmd.append("--force")
if deep:
cmd.append("--deep")
if verbose:
cmd.append("--verbose")
if entitlements:
cmd.append("--entitlements")
cmd.append(entitlements)
if options:
cmd.append("--options")
cmd.append(",".join([option for option in options]))
cmd.append(binary)
run_cmd(cmd)
def is_signable_bin(path):
"""Checks if a path is a file and is executable"""
if os.path.isfile(path) and (os.stat(path).st_mode & stat.S_IXUSR > 0):
return True
return False
def is_signable_lib(path):
"""Checks if a path is a file and ends with .so or .dylib"""
if os.path.isfile(path) and (path.endswith(".so") or path.endswith(".dylib")):
return True
return False
def main():
p = argparse.ArgumentParser(description="Builds and signs Python")
p.add_argument(
"-S",
"--sign-binaries",
action="store",
dest="sign_binaries",
default=None,
help="A Developer ID Application certificate from keychain. "
"Provide the certificate's Common Name. e.g.: "
"'Developer ID Application Munki (U8PN57A5N2)'",
),
p.add_argument(
"-L",
"--location",
action="store",
dest="location",
default=None,
help="Path to python framework to sign.",
),
p.add_argument("-v", "--verbose", action="store_true", help="Be more verbose"),
args = p.parse_args()
if os.geteuid() != 0:
print(
"You must run this script as root in order to build your new "
"Python installer pkg!"
)
sys.exit(1)
if not args.sign_binaries:
print("You must specify a signing identity")
sys.exit(1)
global verbose
verbose = args.verbose
if not args.location:
print("Path to Python.framework must be provided.")
sys.exit(1)
root_dir = args.location
PY_CUR = os.path.join(root_dir, "Versions/Current")
# Set root:admin throughout payload
for root, dirs, files in os.walk(root_dir):
for dir_ in dirs:
os.chown(os.path.join(root, dir_), 0, 80)
for file_ in files:
os.chown(os.path.join(root, file_), 0, 80)
# Generate entitlements file for later
entitlements = {
"com.apple.security.cs.allow-unsigned-executable-memory": True,
"com.apple.security.cs.allow-jit": True,
"com.apple.security.cs.allow-dyld-environment-variables": True,
"com.apple.security.cs.disable-library-validation": True,
}
ent_file = os.path.join(tmp_dir, "entitlements.plist")
with open(ent_file, "wb") as f:
plistlib.dump(entitlements, f)
binaries = []
# Add the executable libs and bins in python pkg
pylib = os.path.join(root_dir, PY_CUR, "lib")
pybin = os.path.join(root_dir, PY_CUR, "bin")
for pydir in pylib, pybin:
binaries.extend(
[
os.path.join(pydir, f)
for f in os.listdir(pydir)
if is_signable_bin(os.path.join(pydir, f))
]
)
for root, dirs, files in os.walk(pydir):
for file_ in files:
if is_signable_lib(os.path.join(root, file_)):
binaries.append(os.path.join(root, file_))
# Add binaries which need entitlements
entitled_binaries = [
os.path.join(root_dir, PY_CUR, "Resources/Python.app"),
os.path.join(pybin, "python3"),
]
# Sign all the binaries. The order is important. Which is why this is a bit
# gross
print("Signing binaries (this may take a while)...")
for binary in binaries:
if verbose:
print(f"Signing {binary}...")
sign_binary(
args.sign_binaries,
binary,
deep=True,
force=True,
options=["runtime"],
)
for binary in entitled_binaries:
if verbose:
print(f"Signing {binary} with entitlements from {ent_file}...")
sign_binary(
args.sign_binaries,
binary,
deep=True,
force=True,
options=["runtime"],
entitlements=ent_file,
)
# Finally sign python framework
py_fwkpath = os.path.join(root_dir, root_dir)
if verbose:
print(f"Signing {py_fwkpath}...")
sign_binary(args.sign_binaries, py_fwkpath, deep=True, force=True)
if __name__ == "__main__":
main()