Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. from bs4 import BeautifulSoup
  2. import requests
  3.  
  4. class TimesketchApiClient():
  5. def __init__(self, host, username, password):
  6. self.host_url = host
  7. self.username = username
  8. self.password = password
  9. self.session = self._CreateSession()
  10.  
  11. def _CreateSession(self):
  12. session = requests.Session()
  13. session.verify = False # Depending on SSL cert is verifiable
  14. response = session.get(self.host_url)
  15. # Get the CSRF token from the response
  16. soup = BeautifulSoup(response.text, "html.parser")
  17. csrf_token = soup.find('input', dict(name='csrf_token'))['value']
  18. login_data = dict(username=self.username, password=self.password, csrf_token=csrf_token)
  19. session.headers.update({'x-csrftoken': csrf_token, 'referer': self.host_url})
  20. response = session.post(u'{0:s}/login/'.format(self.host_url), data=login_data)
  21. return session
  22.  
  23. def CreateSketch(self, name, description):
  24. resource_url = u'{0:s}/api/v1/sketches/'.format(self.host_url)
  25. form_data = {u'name': name, u'description': description}
  26. response = self.session.post(resource_url, json=form_data)
  27. response_dict = response.json()
  28. sketch_id = r_dict[u'objects'][0]['id']
  29. return sketch_id
  30.  
  31. def UploadTimeline(self, timeline_name, plaso_storage_path):
  32. resource_url = u'{0:s}/api/v1/upload/'.format(self.host_url)
  33. files = {'file': open(plaso_storage_path, 'rb')}
  34. data = {u'name': timeline_name}
  35. response = self.session.post(resource_url, files=files, data=data)
  36. print response.text
  37. response_dict = response.json()
  38. index_id = response_dict[u'objects'][0]['id']
  39. return index_id
  40.  
  41. def AddTimelineToSketch(self, sketch_id, index_id):
  42. resource_url = u'{0:s}/api/v1/sketches/{0:d}/'.format(self.host_url,
  43. sketch_id)
  44. form_data = {u'timelines': [index_id]}
  45. response = self.session.post(resource_url, json=form_data)
  46.  
  47. def GetSketch(self, sketch_id):
  48. resource_url = u'{0:s}/api/v1/sketches/{1:d}/'.format(self.host_url,
  49. sketch_id)
  50. response = self.session.get(resource_url)
  51. response_dict = response.json()
  52. try:
  53. response_dict[u'objects']
  54. except KeyError:
  55. raise ValueError(u'Sketch does not exist or you have no access')
  56. return response_dict
  57.  
  58. def GetSketchURL(self, sketch_id):
  59. resource_url = u'{0:s}/api/v1/sketches/{1:d}/'.format(self.host_url,
  60. sketch_id)
  61. return resource_url
  62.  
  63.  
  64. if __name__ == '__main__':
  65. timesketch = TimesketchApiClient(host=u'127.0.0.1', username=u'foo', password=u'bar')
  66. # Create new sketch
  67. sketch_id = timesketch.CreateSketch(name=u'foo', description=u'bar')
  68. # Upload and index a new timeline
  69. index_id = timesketch.UploadTimeline(name=u'foobar timeline', plaso_storage_path=u'/tmp/foo.plaso')
  70. # Add the newly created timeline to the sketch
  71. timesketch.AddTimelineToSketch(sketch_id, index_id)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement