-
Notifications
You must be signed in to change notification settings - Fork 0
/
jit.py
65 lines (59 loc) · 2.75 KB
/
jit.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
import os
import uuid
import random
import requests
import argparse
def jit(vm, rg, sub, ip="0.0.0.0/0", port=22, ssl_verify=True, showmyip=["https://ipconfig.pw/ip", "https://ifconfig.me/ip", "https://ipinfo.io/ip", "https://ident.me/"]):
if ip == None:
ip = requests.get(random.choice(showmyip), verify=ssl_verify).text.strip()
vm_details = os.popen(f'az vm show -g {rg} -n {vm} --subscription {sub} -o tsv --query "[id, location]"').read().split()
vm_id = vm_details[0]
location = vm_details[1]
endpoint = f"https://management.azure.com/subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Security/locations/{location}/jitNetworkAccessPolicies/default/initiate?api-version=2015-06-01-preview"
body = f'''
{{
"requests":[
{{
"content":{{
"virtualMachines":[
{{
"id":"{vm_id}",
"ports":[
{{
"number":{port},
"duration":"PT3H",
"allowedSourceAddressPrefix":"{ip}"
}}
],
"justification": "Need Just-In-Time-Access to the VM."
}}
]
}},
"httpMethod":"POST",
"name":"{str(uuid.uuid4())}",
"requestHeaderDetails":{{"commandName":"Microsoft_Azure_Compute."}},
"url":"{endpoint}"
}}
]
}}
'''
url = "https://management.azure.com/batch?api-version=2015-11-01"
cmd = f'az rest --verbose --method post --uri "{url}" --body \'{body}\''
os.system(cmd)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='JIT access for given Azure VM')
parser.add_argument('--vm', type=str, required=True,
help='Name of the virtual machine')
parser.add_argument('--rg', type=str, required=True,
help='Name of the Resource Group')
parser.add_argument('--sub', type=str, required=True,
help='ID of the Subscription')
parser.add_argument('--port', type=str, required=False,
help='Port to open', default="22")
args = parser.parse_args()
# Login to Azure
os.system("az login --scope https://management.core.windows.net//.default")
# Set Azure Subscription
os.system(f"az account set --subscription {args.sub}")
# Request Just-In-Time-Access
jit(vm=args.vm, rg=args.rg, sub=args.sub, ip=None, port=args.port)