@@ -107,12 +107,20 @@ def __init__(
107
107
108
108
self .nvti = nvticache
109
109
110
+ self .errors = []
111
+
110
112
def prepare_scan_id_for_openvas (self ):
111
113
"""Create the openvas scan id and store it in the redis kb.
112
114
Return the openvas scan_id.
113
115
"""
114
116
self .kbdb .add_scan_id (self .scan_id )
115
117
118
+ def get_error_messages (self ) -> List :
119
+ """Returns the Error List and reset it"""
120
+ ret = self .errors
121
+ self .errors = []
122
+ return ret
123
+
116
124
@property
117
125
def target_options (self ) -> Dict :
118
126
"""Return target options from Scan collection"""
@@ -571,8 +579,7 @@ def prepare_scan_params_for_openvas(self, ospd_params: Dict[str, Dict]):
571
579
if prefs_val :
572
580
self .kbdb .add_scan_preferences (self .scan_id , prefs_val )
573
581
574
- @staticmethod
575
- def build_credentials_as_prefs (credentials : Dict ) -> List [str ]:
582
+ def build_credentials_as_prefs (self , credentials : Dict ) -> List [str ]:
576
583
"""Parse the credential dictionary.
577
584
Arguments:
578
585
credentials: Dictionary with the credentials.
@@ -589,23 +596,36 @@ def build_credentials_as_prefs(credentials: Dict) -> List[str]:
589
596
username = cred_params .get ('username' , '' )
590
597
password = cred_params .get ('password' , '' )
591
598
599
+ # Check service ssh
592
600
if service == 'ssh' :
593
- port = cred_params .get ('port' , '' )
594
- cred_prefs_list .append ('auth_port_ssh|||' + '{0}' .format (port ))
595
- cred_prefs_list .append (
596
- OID_SSH_AUTH
597
- + ':1:'
598
- + 'entry:SSH login '
599
- + 'name:|||{0}' .format (username )
600
- )
601
+ # For ssh check the Port
602
+ port = cred_params .get ('port' , '22' )
603
+ if not port :
604
+ port = '22'
605
+ warning = (
606
+ "Missing port number for ssh credentials."
607
+ + " Using default port 22."
608
+ )
609
+ logger .warning (warning )
610
+ elif not port .isnumeric ():
611
+ self .errors .append (
612
+ "Port for SSH '" + port + "' is not a valid number."
613
+ )
614
+ continue
615
+ elif int (port ) > 65535 or int (port ) < 1 :
616
+ self .errors .append (
617
+ "Port for SSH is out of range (1-65535): " + port
618
+ )
619
+ continue
620
+ # For ssh check the credential type
601
621
if cred_type == 'up' :
602
622
cred_prefs_list .append (
603
623
OID_SSH_AUTH
604
624
+ ':3:'
605
625
+ 'password:SSH password '
606
626
+ '(unsafe!):|||{0}' .format (password )
607
627
)
608
- else :
628
+ elif cred_type == 'usk' :
609
629
private = cred_params .get ('private' , '' )
610
630
cred_prefs_list .append (
611
631
OID_SSH_AUTH
@@ -619,7 +639,30 @@ def build_credentials_as_prefs(credentials: Dict) -> List[str]:
619
639
+ 'file:SSH private key:|||'
620
640
+ '{0}' .format (private )
621
641
)
622
- if service == 'smb' :
642
+ elif cred_type :
643
+ self .errors .append (
644
+ "Unknown Credential Type for SSH: "
645
+ + cred_type
646
+ + ". Use 'up' for Username + Password"
647
+ + " or 'usk' for Username + SSH Key."
648
+ )
649
+ continue
650
+ else :
651
+ self .errors .append (
652
+ "Missing Credential Type for SSH."
653
+ + " Use 'up' for Username + Password"
654
+ + " or 'usk' for Username + SSH Key."
655
+ )
656
+ continue
657
+ cred_prefs_list .append ('auth_port_ssh|||' + '{0}' .format (port ))
658
+ cred_prefs_list .append (
659
+ OID_SSH_AUTH
660
+ + ':1:'
661
+ + 'entry:SSH login '
662
+ + 'name:|||{0}' .format (username )
663
+ )
664
+ # Check servic smb
665
+ elif service == 'smb' :
623
666
cred_prefs_list .append (
624
667
OID_SMB_AUTH
625
668
+ ':1:entry'
@@ -631,7 +674,8 @@ def build_credentials_as_prefs(credentials: Dict) -> List[str]:
631
674
+ 'password:SMB password:|||'
632
675
+ '{0}' .format (password )
633
676
)
634
- if service == 'esxi' :
677
+ # Check service esxi
678
+ elif service == 'esxi' :
635
679
cred_prefs_list .append (
636
680
OID_ESXI_AUTH
637
681
+ ':1:entry:'
@@ -644,13 +688,47 @@ def build_credentials_as_prefs(credentials: Dict) -> List[str]:
644
688
+ 'password:ESXi login password:|||'
645
689
+ '{0}' .format (password )
646
690
)
647
-
648
- if service == 'snmp' :
691
+ # Check service snmp
692
+ elif service == 'snmp' :
649
693
community = cred_params .get ('community' , '' )
650
694
auth_algorithm = cred_params .get ('auth_algorithm' , '' )
651
695
privacy_password = cred_params .get ('privacy_password' , '' )
652
696
privacy_algorithm = cred_params .get ('privacy_algorithm' , '' )
653
697
698
+ if not privacy_algorithm :
699
+ if privacy_password :
700
+ self .errors .append (
701
+ "When no privacy algorithm is used, the privacy"
702
+ + " password also has to be empty."
703
+ )
704
+ continue
705
+ elif (
706
+ not privacy_algorithm == "aes"
707
+ and not privacy_algorithm == "des"
708
+ ):
709
+ self .errors .append (
710
+ "Unknows privacy algorithm used: "
711
+ + privacy_algorithm
712
+ + ". Use 'aes', 'des' or '' (none)."
713
+ )
714
+ continue
715
+
716
+ if not auth_algorithm :
717
+ self .errors .append (
718
+ "Missing authentification algorithm for SNMP."
719
+ + " Use 'md5' or 'sha1'."
720
+ )
721
+ continue
722
+ elif (
723
+ not auth_algorithm == "md5" and not auth_algorithm == "sha1"
724
+ ):
725
+ self .errors .append (
726
+ "Unknown authentification algorithm: "
727
+ + auth_algorithm
728
+ + ". Use 'md5' or 'sha1'."
729
+ )
730
+ continue
731
+
654
732
cred_prefs_list .append (
655
733
OID_SNMP_AUTH
656
734
+ ':1:'
@@ -685,20 +763,29 @@ def build_credentials_as_prefs(credentials: Dict) -> List[str]:
685
763
+ 'radio:SNMPv3 Privacy Algorithm:|||'
686
764
+ '{0}' .format (privacy_algorithm )
687
765
)
766
+ elif service :
767
+ self .errors .append (
768
+ "Unknown service type for credential: " + service
769
+ )
770
+ else :
771
+ self .errors .append ("Missing service type for credential." )
688
772
689
773
return cred_prefs_list
690
774
691
775
def prepare_credentials_for_openvas (self ) -> bool :
692
776
"""Get the credentials from the scan collection and store them
693
777
in the kb."""
778
+ logger .debug ("Looking for given Credentials..." )
694
779
credentials = self .scan_collection .get_credentials (self .scan_id )
695
780
if credentials :
696
781
cred_prefs = self .build_credentials_as_prefs (credentials )
697
782
if cred_prefs :
698
783
self .kbdb .add_credentials_to_scan_preferences (
699
784
self .scan_id , cred_prefs
700
785
)
701
-
786
+ logger .debug ("Credentials added to the kb." )
787
+ else :
788
+ logger .debug ("No credentials found." )
702
789
if credentials and not cred_prefs :
703
790
return False
704
791
0 commit comments