2018-02-11 01:38:38 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-02-10 02:12:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Generic file checksum request."""
|
|
|
|
|
2018-02-10 02:12:24 +00:00
|
|
|
import json
|
2018-02-11 01:38:38 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from .. import credentials
|
2018-02-10 02:12:24 +00:00
|
|
|
from .tclrequest import TclRequest
|
|
|
|
from .tclresult import ChecksumResult
|
|
|
|
|
2018-02-11 01:38:38 +00:00
|
|
|
|
2018-02-10 02:12:24 +00:00
|
|
|
class ChecksumRequest(TclRequest):
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Generic file checksum request."""
|
|
|
|
|
2018-02-10 02:12:24 +00:00
|
|
|
def __init__(self, address, file_uri):
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Populate variables."""
|
2018-02-10 02:12:24 +00:00
|
|
|
super().__init__()
|
|
|
|
# NOTE: THIS HAS TO BE RUN ON AN ENCSLAVE
|
|
|
|
self.uri = "/checksum.php"
|
|
|
|
self.method = "POST"
|
|
|
|
self.address = address
|
|
|
|
self.file_uri = file_uri
|
|
|
|
|
|
|
|
def get_headers(self):
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Return request headers."""
|
2018-02-10 02:12:24 +00:00
|
|
|
return {"User-Agent": "tcl"}
|
|
|
|
|
|
|
|
def get_params(self):
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Return request parameters."""
|
2018-02-10 02:12:24 +00:00
|
|
|
params = OrderedDict()
|
|
|
|
params.update(credentials.get_creds2())
|
|
|
|
payload = {self.address: self.file_uri}
|
|
|
|
payload_json = json.dumps(payload)
|
|
|
|
params["address"] = bytes(payload_json, "utf-8")
|
|
|
|
return params
|
|
|
|
|
|
|
|
def is_done(self, http_status: int, contents: str) -> bool:
|
2018-02-11 01:38:38 +00:00
|
|
|
"""Handle request result."""
|
2018-02-10 02:12:24 +00:00
|
|
|
if http_status == 200:
|
|
|
|
# <ENCRYPT_FOOTER>2abfa6f6507044fec995efede5d818e62a0b19b5</ENCRYPT_FOOTER> means ERROR (invalid ADDRESS!)
|
|
|
|
if "<ENCRYPT_FOOTER>2abfa6f6507044fec995efede5d818e62a0b19b5</ENCRYPT_FOOTER>" in contents:
|
|
|
|
self.error = "INVALID URI: {}".format(self.file_uri)
|
|
|
|
self.success = False
|
|
|
|
return True
|
|
|
|
self.response = contents
|
|
|
|
self.result = ChecksumResult(contents)
|
|
|
|
self.success = True
|
|
|
|
return True
|
|
|
|
self.error = "HTTP {}".format(http_status)
|
|
|
|
self.success = False
|
|
|
|
return True
|