-
Notifications
You must be signed in to change notification settings - Fork 44
/
cmdr.py
executable file
·335 lines (284 loc) · 8.75 KB
/
cmdr.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
import argparse
import plistlib
import uuid
import sys
import random
# closure which generates a function that returns a simple command dictionary
def simple_command(request_type):
def simple_command_body(args):
nonlocal request_type
if request_type is None and hasattr(args, "request_type"):
request_type = args.request_type
return {"RequestType": request_type}
return simple_command_body
def install_profile(args):
return {
"RequestType": "InstallProfile",
"Payload": args.mobileconfig.read(),
}
def remove_profile(args):
return {
"RequestType": "RemoveProfile",
"Identifier": args.identifier,
}
def dev_info(args):
c = {
"RequestType": "DeviceInformation",
}
if hasattr(args, "query") and args.query:
c["Queries"] = args.query
return c
def account_config(args):
c = {
"RequestType": "AccountConfiguration",
}
if hasattr(args, "fullname") and args.fullname:
c["PrimaryAccountFullName"] = args.fullname
if hasattr(args, "username") and args.username:
c["PrimaryAccountUserName"] = args.username
if hasattr(args, "lock") and args.lock:
c["LockPrimaryAccountInfo"] = True
return c
def sched_update(args):
c = {
"RequestType": "ScheduleOSUpdate",
"Updates": [{"InstallAction": args.action}],
}
if hasattr(args, "version") and args.version:
c["Updates"][0]["ProductVersion"] = args.version
if hasattr(args, "key") and args.key:
c["Updates"][0]["ProductKey"] = args.key
if hasattr(args, "deferrals") and args.deferrals:
c["Updates"][0]["MaxUserDeferrals"] = args.deferrals
if hasattr(args, "priority") and args.priority:
c["Updates"][0]["Priority"] = args.priority
return c
def settings(args):
c = {"RequestType": "Settings", "Settings": []}
if hasattr(args, "allowbst") and args.allowbst:
c["Settings"] = [
{
"Item": "MDMOptions",
"MDMOptions": {
"BootstrapTokenAllowed": True,
},
}
]
return c
def make_erase_device_command(args):
return {"RequestType": "EraseDevice", "PIN": args.pin}
def make_device_lock_command(args):
return {"RequestType": "DeviceLock", "PIN": args.pin}
def sched_update_subparser(parser):
sched_update_parser = parser.add_parser(
"ScheduleOSUpdate", help="ScheduleOSUpdate MDM command"
)
sched_update_parser.add_argument(
"action",
type=str,
help="InstallAction (ex. InstallASAP, InstallLater, etc.)",
)
sched_update_parser.add_argument(
"--version",
type=str,
help="ProductVersion (ex. 12.1, 12.2.1, etc.)",
)
sched_update_parser.add_argument(
"--key",
type=str,
help="ProductKey (ex. MSU_UPDATE_21E5227a_patch_12.3, etc.)",
)
sched_update_parser.add_argument(
"--deferrals",
type=int,
help="MaxUserDeferrals (ex. 3, 30, etc.)",
)
sched_update_parser.add_argument(
"--priority",
type=str,
help="Priority (ex. Low, High.)",
)
sched_update_parser.set_defaults(func=sched_update)
return sched_update_parser
def dev_info_subparser(parser):
dev_info_parser = parser.add_parser(
"DeviceInformation",
help="DeviceInformation MDM command",
aliases=["DeviceInfo", "DevInfo"],
)
dev_info_parser.add_argument(
"query",
nargs="*",
type=str,
help="optional DeviceInformation queries (ex. Model, OSVersion, etc.)",
)
dev_info_parser.set_defaults(func=dev_info)
return dev_info_parser
def inst_prof_subparser(parser):
inst_prof_parser = parser.add_parser(
"InstallProfile", help="InstallProfile MDM command"
)
inst_prof_parser.add_argument(
"mobileconfig",
type=argparse.FileType("rb"),
help="Path to mobileconfig file (profile) to install",
)
inst_prof_parser.set_defaults(func=install_profile)
return inst_prof_parser
def rem_prof_subparser(parser):
rem_prof_parser = parser.add_parser(
"RemoveProfile", help="RemoveProfile MDM command"
)
rem_prof_parser.add_argument(
"identifier",
type=str,
help="Identifier of profile to remove (ex. com.example.profile)",
)
rem_prof_parser.set_defaults(func=remove_profile)
return rem_prof_parser
def account_config_subparser(parser):
p = parser.add_parser(
"AccountConfig", help="AccountConfiguration MDM command"
)
p.add_argument(
"-f",
"--fullname",
type=str,
help="PrimaryAccountFullName field",
)
p.add_argument(
"-u",
"--username",
type=str,
help="PrimaryAccountUserName field",
)
p.add_argument(
"-l",
"--lock",
action="store_true",
help="LockPrimaryAccountInfo",
)
p.set_defaults(func=account_config)
return p
def settings_subparser(parser):
settings_parser = parser.add_parser(
"Settings", help="Settings MDM command"
)
settings_parser.add_argument(
"--allowbst",
action="store_true",
help="BootstrapTokenAllowed",
)
settings_parser.set_defaults(func=settings)
return settings_parser
def make_erase_device_subparser(parser):
p = parser.add_parser("EraseDevice", help="EraseDevice MDM command")
p.add_argument(
"pin",
type=str,
help="The six-character PIN for Find My.",
)
p.set_defaults(func=make_erase_device_command)
def make_device_lock_subparser(parser):
p = parser.add_parser("DeviceLock", help="DeviceLock MDM command")
p.add_argument(
"pin",
type=str,
help="The six-character PIN for Find My.",
)
p.set_defaults(func=make_device_lock_command)
def simple_command_subparser(request_type, parser):
new_parser = parser.add_parser(
request_type,
help=request_type + " MDM command",
)
new_parser.set_defaults(func=simple_command(request_type))
return new_parser
def command_subparser(parser):
command_parser = parser.add_parser(
"command", help="arbitrary MDM command (simple non-argument command)"
)
command_parser.add_argument(
"request_type",
type=str,
help='Command RequestType (i.e. "SecurityInfo", "ProfileList", etc.)',
)
command_parser.set_defaults(func=simple_command(None))
return command_parser
def main():
parser = argparse.ArgumentParser(description="MDM command generator")
parser.add_argument(
"-u",
"--uuid",
type=str,
default=str(uuid.uuid4()),
help="command UUID (auto-generated if not specified)",
)
parser.add_argument(
"-r",
"--random",
action="store_true",
help="Select a random simple command (only non-argument commands)",
)
subparsers = parser.add_subparsers(
title="MDM commands",
help="supported MDM commands",
)
for c in [
"ProfileList",
"ProvisioningProfileList",
"CertificateList",
"SecurityInfo",
"RestartDevice",
"ShutDownDevice",
"StopMirroring",
"ClearRestrictionsPassword",
"UserList",
"LogOutUser",
"PlayLostModeSound",
"DisableLostMode",
"DeviceLocation",
"ManagedMediaList",
"DeviceConfigured",
"AvailableOSUpdates",
"NSExtensionMappings",
"OSUpdateStatus",
"EnableRemoteDesktop",
"DisableRemoteDesktop",
"ActivationLockBypassCode",
"ScheduleOSUpdateScan",
]:
simple_command_subparser(c, subparsers)
dev_info_subparser(subparsers)
inst_prof_subparser(subparsers)
rem_prof_subparser(subparsers)
sched_update_subparser(subparsers)
account_config_subparser(subparsers)
settings_subparser(subparsers)
make_erase_device_subparser(subparsers)
make_device_lock_subparser(subparsers)
command_subparser(subparsers)
args = parser.parse_args()
# command and random are mutually exclusive
if (not hasattr(args, "func") and not args.random) or (
hasattr(args, "func") and args.random
):
parser.print_help()
sys.exit(2)
# select a random simple command and set the func
if args.random:
read_only_commands = [
"SecurityInfo",
"CertificateList",
"ProfileList",
"ProvisioningProfileList",
]
args.func = simple_command(random.choice(read_only_commands))
c = {
"CommandUUID": args.uuid,
"Command": args.func(args),
}
plistlib.dump(c, sys.stdout.buffer)
if __name__ == "__main__":
main()