Skip to content

Instantly share code, notes, and snippets.

@averagesecurityguy
Created March 10, 2015 16:00
Show Gist options
  • Save averagesecurityguy/1fb34475ff0c89f937f4 to your computer and use it in GitHub Desktop.
Save averagesecurityguy/1fb34475ff0c89f937f4 to your computer and use it in GitHub Desktop.
SecurityCenter File Upload Example
#!/usr/bin/env python
import requests
import random
import json
import os
token = ''
cookie = ''
server = ''
username = ''
password = ''
def sc_connect(module, action, input={}):
'''
Connect to the SC server using the module, action and input. Return the
JSON response or return None if there is an error.
'''
data = {
'module': module,
'action': action,
'input': json.dumps(input),
'token': token,
'request_id': random.randrange(10000, 99999)
}
cookies = {'TNS_SESSIONID': cookie}
url = '{0}/request.php'.format(server)
try:
resp = requests.post(url, data=data, cookies=cookies, verify=False)
json_resp = resp.json()
if json_resp['error_msg'] != '':
return json_resp['error_msg']
else:
return json_resp['response']
except requests.ConnectionError, e:
print 'Error: {0}'.format(str(e))
return None
def sc_upload(filename):
'''
Upload a file to the SC server.
'''
params = {
'module': 'file',
'action': 'upload',
'input': None,
'token': token,
'request_id': random.randrange(10000, 99999)
}
cookies = {'TNS_SESSIONID': cookie}
files = {'Filedata': (os.path.basename(filename), open(filename, 'rb'))}
url = '{0}/request.php'.format(server)
try:
resp = requests.post(url, params=params, files=files, cookies=cookies, verify=False)
json_resp = resp.json()
if json_resp['error_msg'] != '':
return json_resp['error_msg']
else:
return json_resp['response']
except requests.ConnectionError, e:
print 'Error: {0}'.format(str(e))
return None
def sc_login(username, password):
'''
Login to the server using the username and password. Store the auth token
and cookie for subsequent logins.
'''
input = {'username': username, 'password': password}
resp = sc_connect('auth', 'login', input)
return resp['token'], resp['sessionID']
def sc_logout():
'''
Send a logout request to SecurityCenter and delete the token.
'''
sc_connect('auth', 'logout')
return '', ''
if __name__ == '__main__':
token, cookie = sc_login(username, password)
resp = sc_upload('../files/nessus_minimal.nessus')
fname = resp['filename']
print fname
token, cookie = sc_logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment