-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathput_python.py
executable file
·143 lines (114 loc) · 4.23 KB
/
put_python.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
#!/usr/bin/env python3
import tanrest, json, time, sys, base64, os
from pprint import pprint as pp
from time import sleep
import getpass
import getopt
import subprocess
import configparser
config = configparser.ConfigParser()
config.readfp(open('content.cfg'))
def usage():
print("""
Usage:
put_python.py [options]
Description:
Uploads python deployment package to the Tanium server.
Files:
- TanCD-python-windows.zip
- TanCD-python-linux.zip
- TanCD-python-install.py
Command:
$TANIUM_CLIENT_EXECUTABLE runscript InternalPython TanCD-python-install.py
Options:
-h, --help display this help and exit
--server [required] tanium server (ip address or dns name) [required]
--username user name to connect to tanium with (defaults to logged in user)
--password password to connect to tanium with (will prompt if not provided)
Example:
./put_python.py --server 139.181.111.21 --username tanium
""")
def main(argv):
global loglevel
creds = {}
try:
opts, args = getopt.getopt(argv,"d:hs:p:q:b:",["debug:","help", "server=", "username=", "password="])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
if opt in ('d', '--debug'):
loglevel = arg
if opt in ('--server'):
creds['server'] = arg
if opt in ('--username'):
creds['username'] = arg
if opt in ('--password'):
creds['password'] = arg
if 'server' not in creds:
print("--server parameter required")
usage()
sys.exit(2)
else:
if 'http' not in creds['server']:
creds['server'] = 'https://' + creds['server']
if '/api/v2' not in creds['server']:
creds['server'] = creds['server'] + '/api/v2'
if 'username' not in creds:
creds['username'] = getpass.getuser()
if 'password' not in creds:
creds['password'] = getpass.getpass()
tan = tanrest.server(creds)
#tan.debug = True
files = []
# Upload a base64 encoded file, capture the hash for future reference.
for filepath in [ 'TanCD/package_files/TanCD-python-install.py', 'TanCD-python-linux.zip', 'TanCD-python-windows.zip' ]:
#for filepath in [ 'TanCD/package_files/TanCD-python-install.py' ]:
with open(filepath, 'rb') as fd:
file_b64data = base64.b64encode(fd.read()).decode('ascii')
file_data = {
'bytes': file_b64data,
'file_size': os.stat(filepath).st_size,
'force_overwrite': 1
#'start_pos': 0,
#'part_size': os.stat(filepath).st_size
}
##
# TODO: handling chunking and sending file parts if the files get too large.
file_obj = tan.req('POST', 'upload_file', data=file_data)
pp(file_obj)
file_hash = file_obj['data']['upload_file']['hash']
sleep(1)
file_status = tan.req('GET', 'upload_file/' + str(file_obj['data']['upload_file']['id']))
pp(file_status['data']['upload_file_status']['file_cached'])
files.append({
'name': filepath.split('/')[-1],
'hash': file_hash
}
)
#all_package_files = tan.req('GET', 'package_files')
#pp(all_package_files)
#pp(results)
package = {
'name': 'TanCD Python Content',
'display-name':'TanCD Python Content',
'command': '$TANIUM_CLIENT_EXECUTABLE runscript InternalPython TanCD-python-install.py',
'files': files,
'expire_seconds': 900,
"skip_lock_flag": False,
"process_group_flag": True,
}
#pp(package)
package_id = tan.get_package_id(package['name'])
if package_id:
if tan.update_package(package_id, package):
print('updated existing package: ' + package["name"] + ' (' + str(package_id) + ')')
else:
resp = tan.create_package(package)
if resp:
print('created new package: ' + package["name"] + ' (' + str(resp['data']['id']) + ')')
if __name__ == "__main__":
main(sys.argv[1:])