Exemplo n.º 1
0
    def print_probabilities(self):
        """
        Prints a copy of the current probabilities.
        Used for convenient checking in a command line environment.
        For dictionaries containing the raw values, use the `p_*` attributes.
        :return:
        """
        # create copies to avoid editing
        p_initial = copy.deepcopy(self.p_initial)
        p_emission = copy.deepcopy(self.p_emission)
        p_transition = copy.deepcopy(self.p_transition)

        # convert to nested lists for clean printing
        p_initial = [[str(s)] + [str(round(p_initial[s], 3))]
                     for s in self.states]
        p_emission = [
            [str(s)] +
            [str(round(p_emission[s][e], 3)) for e in self.emissions]
            for s in self.states
        ]
        p_transition = [
            [str(s1)] +
            [str(round(p_transition[s1][s2], 3)) for s2 in self.states]
            for s1 in self.states
        ]
        p_initial.insert(0, ["S_i", "Y_0"])
        p_emission.insert(0, ["S_i \ E_j"] + [str(e) for e in self.emissions])
        p_transition.insert(0, ["S_i \ E_j"] + [str(s) for s in self.states])

        # format tables
        ti = DoubleTable(p_initial, "Starting state probabilities")
        te = DoubleTable(p_emission, "Emission probabilities")
        tt = DoubleTable(p_transition, "Transition probabilities")
        te.padding_left = 1
        te.padding_right = 1
        tt.padding_left = 1
        tt.padding_right = 1
        te.justify_columns[0] = "right"
        tt.justify_columns[0] = "right"

        # print tables
        print("\n")
        print(ti.table)
        print("\n")
        print(te.table)
        print("\n")
        print(tt.table)
        print("\n")

        #
        return None
Exemplo n.º 2
0
    def print_fit_parameters(self):
        """
        Prints a copy of the current state counts.
        Used for convenient checking in a command line environment.
        For dictionaries containing the raw values, use the `n_*` attributes.
        :return:
        """
        # create copies to avoid editing
        n_initial = copy.deepcopy(self.n_initial)
        n_emission = copy.deepcopy(self.n_emission)
        n_transition = copy.deepcopy(self.n_transition)

        # make nested lists for clean printing
        initial = [[str(s)] + [str(n_initial[s])] for s in self.states]
        initial.insert(0, ["S_i", "Y_0"])
        emissions = [[str(s)] +
                     [str(n_emission[s][e]) for e in self.emissions]
                     for s in self.states]
        emissions.insert(0, ["S_i \ E_i"] + list(map(str, self.emissions)))
        transitions = [[str(s1)] +
                       [str(n_transition[s1][s2]) for s2 in self.states]
                       for s1 in self.states]
        transitions.insert(0, ["S_i \ S_j"] +
                           list(map(lambda x: str(x), self.states)))

        # format tables
        ti = DoubleTable(initial, "Starting state counts")
        te = DoubleTable(emissions, "Emission counts")
        tt = DoubleTable(transitions, "Transition counts")
        ti.padding_left = 1
        ti.padding_right = 1
        te.padding_left = 1
        te.padding_right = 1
        tt.padding_left = 1
        tt.padding_right = 1
        ti.justify_columns[0] = "right"
        te.justify_columns[0] = "right"
        tt.justify_columns[0] = "right"

        # print tables
        print("\n")
        print(ti.table)
        print("\n")
        print(te.table)
        print("\n")
        print(tt.table)
        print("\n")

        #
        return None
def get_host_reputation_table(response_list):
    data_list = []
    header = [
        'Verdict',
        'Threat Status',
        'Threat Name',
        'Threat Type',
        'First Seen',
        'Last Seen',
    ]
    data_list.append(header)
    response = response_list[0]
    threat_data = response.get('threatData')
    data = [
        threat_data.get('verdict'),
        threat_data.get('threatStatus'),
        threat_data.get('threatName'),
        threat_data.get('threatType'),
        threat_data.get('firstSeen'),
        threat_data.get('lastSeen'),
    ]
    data_list.append(data)

    host_reputation = DoubleTable(data_list)
    host_reputation.padding_left = 1
    host_reputation.padding_right = 1
    host_reputation.inner_column_border = True
    host_reputation.inner_row_border = True

    return host_reputation.table
def get_api_quota_table(response_list):
    data_list = []
    header = [
        'Licenced Quota',
        'Remaining Quota',
        'Expiration Date',
    ]
    data_list.append(header)
    response = response_list[0]
    quota_data = response.get('quotaDetails')
    data = [
        quota_data.get('licensedQuota'),
        quota_data.get('remainingQuota'),
        quota_data.get('expiryDate'),
    ]
    data_list.append(data)

    api_quota = DoubleTable(data_list)
    api_quota.padding_left = 1
    api_quota.padding_right = 1
    api_quota.inner_column_border = True
    api_quota.inner_row_border = True

    return api_quota.table + '\n\nNote: ' + quota_data.get('note')
def get_scan_report_table(response_list, source=0):
    data_list = []
    header = [
        'URL',
        'Type',
        'Verdict',
        'Threat Status',
        'Scan ID',
        'Threat Name',
        'Threat Type',
        'First Seen',
        'Last Seen',
    ]
    data_list.append(header)
    response = response_list[0]

    normalize_msg = ''
    if response.get('errorNo') != 1:
        if response.get('normalizeData').get('normalizeStatus') == 1:
            normalize_msg = response.get('normalizeData').get(
                'normalizeMessage') + '\n'

        url = response.get('urlData')
        threat_data = url.get('threatData')
        name = url.get('scanId')
        data = [
            url.get('url'),
            'Scanned URL',
            threat_data.get('verdict'),
            threat_data.get('threatStatus'),
            name,
            threat_data.get('threatName'),
            threat_data.get('threatType'),
            threat_data.get('firstSeen'),
            threat_data.get('lastSeen'),
        ]
        data_list.append(data)

        if url.get('finalUrl') is not None:
            data = [
                url.get('finalUrl'),
                'Final URL',
                threat_data.get('verdict'),
                threat_data.get('threatStatus'),
                '-',
                '-',
                '-',
                '-',
                '-',
            ]
            data_list.append(data)

        if url.get('landingUrl') is not None:
            landing_url = url.get('landingUrl')
            threat_data = landing_url.get('threatData')
            data = [
                landing_url.get('url'),
                'Redirected URL',
                threat_data.get('verdict'),
                threat_data.get('threatStatus'),
                landing_url.get('scanId'),
                threat_data.get('threatName'),
                threat_data.get('threatType'),
                threat_data.get('firstSeen'),
                threat_data.get('lastSeen'),
            ]
            data_list.append(data)

        scan_report = DoubleTable(data_list)
        scan_report.padding_left = 1
        scan_report.padding_right = 1
        scan_report.inner_column_border = True
        scan_report.inner_row_border = True

        for i, data in enumerate(data_list):
            if i > 0:
                wrapped_url = '\n'.join(wrap(data[0], 35))
                wrapped_t = '\n'.join(wrap(data[1], 10))
                wrapped_sid = '\n'.join(wrap(data[4], 18))
                wrapped_tn = '\n'.join(wrap(data[5], 12))
                wrapped_tt = '\n'.join(wrap(data[6], 12))
                wrapped_fs = '\n'.join(wrap(data[7], 12))
                wrapped_ls = '\n'.join(wrap(data[8], 12))

                scan_report.table_data[i][0] = wrapped_url
                scan_report.table_data[i][1] = wrapped_t
                scan_report.table_data[i][4] = wrapped_sid
                scan_report.table_data[i][5] = wrapped_tn
                scan_report.table_data[i][6] = wrapped_tt
                scan_report.table_data[i][7] = wrapped_fs
                scan_report.table_data[i][8] = wrapped_ls
    else:
        if source == 1:
            return 'Your Url Scan request is submitted to the cloud and may take up-to 60 seconds to complete.\n'\
                   'Please check back later using "slashnext-scan-report" action with Scan ID = {0} or running the ' \
                   'same "slashnext-url-scan" action one more time'.format(response.get('urlData').get('scanId'))
        elif source == 2:
            return 'Your Url Scan request is submitted to the cloud and is taking longer than expected to complete.\n' \
                   'Please check back later using "slashnext-scan-report" action with Scan ID = {0} or running the ' \
                   'same "slashnext-url-scan-sync" action one more time'.format(response.get('urlData').get('scanId'))
        else:
            return 'Your Url Scan request is submitted to the cloud and is taking longer than expected to complete.\n' \
                   'Please check back later using "slashnext-scan-report" action with Scan ID = {0} one more ' \
                   'time'.format(response.get('urlData').get('scanId'))

    if len(response_list) == 4:
        download_sc = get_download_sc_file([response_list[1]], name)
        download_html = get_download_html_file([response_list[2]], name)
        download_text = get_download_text_file([response_list[3]], name)

        return normalize_msg + scan_report.table + '\n\nWebpage Forensics\n\n' + \
            download_sc + '\n' + download_html + '\n' + download_text
    else:
        return normalize_msg + scan_report.table
def get_host_urls_table(response_list):
    data_list = []
    header = [
        'URL',
        'Type',
        'Verdict',
        'Threat Status',
        'Scan ID',
        'Threat Name',
        'Threat Type',
        'First Seen',
        'Last Seen',
    ]
    data_list.append(header)
    response = response_list[0]
    url_list = response.get('urlDataList')
    for url in url_list:
        threat_data = url.get('threatData')
        data = [
            url.get('url'),
            'Scanned URL',
            threat_data.get('verdict'),
            threat_data.get('threatStatus'),
            url.get('scanId'),
            threat_data.get('threatName'),
            threat_data.get('threatType'),
            threat_data.get('firstSeen'),
            threat_data.get('lastSeen'),
        ]
        data_list.append(data)

        if url.get('finalUrl') is not None:
            data = [
                url.get('finalUrl'),
                'Final URL',
                threat_data.get('verdict'),
                threat_data.get('threatStatus'),
                '-',
                '-',
                '-',
                '-',
                '-',
            ]
            data_list.append(data)

        if url.get('landingUrl') is not None:
            landing_url = url.get('landingUrl')
            threat_data = landing_url.get('threatData')
            data = [
                landing_url.get('url'),
                'Redirected URL',
                threat_data.get('verdict'),
                threat_data.get('threatStatus'),
                landing_url.get('scanId'),
                threat_data.get('threatName'),
                threat_data.get('threatType'),
                threat_data.get('firstSeen'),
                threat_data.get('lastSeen'),
            ]
            data_list.append(data)

    host_urls = DoubleTable(data_list)
    host_urls.padding_left = 1
    host_urls.padding_right = 1
    host_urls.inner_column_border = True
    host_urls.inner_row_border = True

    for i, data in enumerate(data_list):
        if i > 0:
            wrapped_url = '\n'.join(wrap(data[0], 35))
            wrapped_t = '\n'.join(wrap(data[1], 10))
            wrapped_sid = '\n'.join(wrap(data[4], 18))
            wrapped_tn = '\n'.join(wrap(data[5], 12))
            wrapped_tt = '\n'.join(wrap(data[6], 12))
            wrapped_fs = '\n'.join(wrap(data[7], 12))
            wrapped_ls = '\n'.join(wrap(data[8], 12))

            host_urls.table_data[i][0] = wrapped_url
            host_urls.table_data[i][1] = wrapped_t
            host_urls.table_data[i][4] = wrapped_sid
            host_urls.table_data[i][5] = wrapped_tn
            host_urls.table_data[i][6] = wrapped_tt
            host_urls.table_data[i][7] = wrapped_fs
            host_urls.table_data[i][8] = wrapped_ls

    return host_urls.table