22import argparse
33import sys
44import webbrowser
5- from typing import List , Optional , Tuple
5+ from typing import Any , List , Optional , Tuple
66
77import requests
8- from rich import print
8+ from rich import box
9+ from rich .table import Table
910
11+ from censys .cli .utils import console
1012from censys .common .exceptions import CensysCLIException , CensysNotFoundException
1113from censys .search import CensysHosts
1214
@@ -50,76 +52,87 @@ def translate_risk(self, services: List[dict]) -> Tuple[List[dict], List[dict]]:
5052 medium_risk = []
5153
5254 for service in services :
53- port = service .get ("port" )
54- protocol = service .get ("service_name" )
55- string = f"{ protocol } on { port } "
56- if protocol in self .HIGH_RISK_DEFINITION :
57- high_risk .append ({"port" : port , "protocol" : protocol , "string" : string })
58- elif protocol in self .MEDIUM_RISK_DEFINITION :
59- medium_risk .append (
60- {"port" : port , "protocol" : protocol , "string" : string }
61- )
55+ service_name = service .get ("service_name" )
56+ if service_name in self .HIGH_RISK_DEFINITION :
57+ high_risk .append (service )
58+ elif service_name in self .MEDIUM_RISK_DEFINITION :
59+ medium_risk .append (service )
6260 else :
63- medium_risk .append (
64- {"port" : port , "protocol" : protocol , "string" : string }
65- )
61+ medium_risk .append (service )
6662
6763 return high_risk , medium_risk
6864
69- @staticmethod
70- def risks_to_string (high_risk : list , medium_risk : list ) -> str :
65+ def make_risks_into_table (self , title : str , risks : List [dict ]) -> Table :
66+ """Creates a table of risks.
67+
68+ Args:
69+ title (str): Title of the table.
70+ risks (list): List of risks.
71+
72+ Returns:
73+ Table: Table of risks.
74+ """
75+ table = Table ("Port" , "Service Name" , title = title , box = box .SQUARE )
76+ for risk in risks :
77+ table .add_row (str (risk .get ("port" )), risk .get ("service_name" ))
78+ return table
79+
80+ def risks_to_string (self , high_risks : list , medium_risks : list ) -> List [Any ]:
7181 """Risks to printable string.
7282
7383 Args:
74- high_risk (list): Lists of high risks.
75- medium_risk (list): Lists of medium risks.
84+ high_risks (list): Lists of high risks.
85+ medium_risks (list): Lists of medium risks.
7686
7787 Raises:
7888 CensysCLIException: No information/risks found.
7989
8090 Returns:
81- str : Printable string for CLI.
91+ list : Printable objects for CLI.
8292 """
83- len_high_risk = len (high_risk )
84- len_medium_risk = len (medium_risk )
93+ len_high_risk = len (high_risks )
94+ len_medium_risk = len (medium_risks )
8595
8696 if len_high_risk + len_medium_risk == 0 :
8797 raise CensysCLIException
8898
89- response = ""
99+ response : List [ Any ] = []
90100 if len_high_risk > 0 :
91- response = (
92- response
93- + "[bold red]:exclamation: High Risks Found:[/bold red] \n "
94- + "\n " .join ([risk .get ("string" ) for risk in high_risk ])
101+ response .append (
102+ self .make_risks_into_table (
103+ ":exclamation: High Risks Found" ,
104+ high_risks ,
105+ )
95106 )
96107 else :
97- response = response + "You don't have any High Risks in your network\n "
108+ response . append ( "You don't have any High Risks in your network\n " )
98109 if len_medium_risk > 0 :
99- response = (
100- response
101- + "[bold orange]:grey_exclamation: Medium Risks Found:[/bold orange] \n "
102- + "\n " .join ([risk .get ("string" ) for risk in medium_risk ])
110+ response .append (
111+ self .make_risks_into_table (
112+ ":grey_exclamation: Medium Risks Found" ,
113+ medium_risks ,
114+ )
103115 )
104116 else :
105- response = response + "You don't have any Medium Risks in your network\n "
117+ response . append ( "You don't have any Medium Risks in your network\n " )
106118 return response
107119
108- def view_current_ip_risks (self ) -> str :
109- """Gets protocol information for the current IP and returns any risks.
110-
111- Returns:
112- str: Printable
113- """
120+ def view_current_ip_risks (self ):
121+ """Gets protocol information for the current IP and returns any risks."""
114122 current_ip = self .get_current_ip ()
115123
116124 try :
125+ console .print (f"Searching for information on { current_ip } ..." )
117126 results = self .index .view (current_ip )
118127 services = results .get ("services" , [])
119128 high_risk , medium_risk = self .translate_risk (services )
120- return self .risks_to_string (high_risk , medium_risk )
129+ for res in self .risks_to_string (high_risk , medium_risk ):
130+ console .print (res )
131+ console .print (
132+ f"\n For more information, please visit: https://search.censys.io/hosts/{ current_ip } "
133+ )
121134 except (CensysNotFoundException , CensysCLIException ):
122- return (
135+ console . print (
123136 "[green]:white_check_mark: No Risks were found on your network[/green]"
124137 )
125138
@@ -136,9 +149,7 @@ def cli_hnri(args: argparse.Namespace):
136149
137150 client = CensysHNRI (args .api_id , args .api_secret )
138151
139- risks = client .view_current_ip_risks ()
140-
141- print (risks )
152+ client .view_current_ip_risks ()
142153
143154
144155def include (parent_parser : argparse ._SubParsersAction , parents : dict ):
0 commit comments