
    CjRJ                         S r SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	J
r
JrJrJrJrJrJr  SSKJrJrJrJr  S	 r " S
 S\5      rg)z
Implements class for CVSS3 specification as defined at
https://www.first.org/cvss/specification-document .

The library is compatible with both Python 2 and Python 3.
    )unicode_literalsN)ROUND_CEILING)Decimal   )ENVIRONMENTAL_METRICSMETRICS_ABBREVIATIONSMETRICS_ABBREVIATIONS_JSONMETRICS_MANDATORYMETRICS_VALUE_NAMESMETRICS_VALUESTEMPORAL_METRICSOrderedDict)CVSS3MalformedErrorCVSS3MandatoryErrorCVSS3RHMalformedErrorCVSS3RHScoreDoesNotMatchc                 <    U R                  [        S5      [        S9$ )z
Round up is defined as the smallest number, specified to one decimal place, that is equal to
or higher than its input. For example, Round up (4.02) is 4.1; and Round up (4.00) is 4.0.
z0.1)rounding)quantizeDr   )values    \/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/cvss/cvss3.pyround_upr   $   s    
 >>!E(]>;;    c                       \ rS rSrSr\S 5       rS rS rS r	S r
S rS	 rS
 rS rS rS rS rS rS rS rS rS rS rS rS S jrS rS rS rS rS!S jrS rS r Sr!g)"CVSS3,   z<
Class to hold CVSS3 vector, parsed values, and all scores.
c                 ~    UR                  SS5      u  p# [	        U5      nU " U5      nUR                  5       S   U:X  a  U$ [        SR                  X5R                  5       S   U5      5      e! [         a    [        SR                  U5      5      ef = f! [         a    [        SR                  U5      5      ef = f)a  
Creates a CVSS3 object from CVSS vector in Red Hat notation, e.g. containing base score.
Also checks if the score matches the vector.

Args:
    vector (str): string specifying CVSS3 vector in Red Hat notation, fields may be out of
                  order, fields which are not mandatory may be missing

Returns:
    CVSS3: the generated CVSS3 object created from the vector string

Raises:
    CVSS3RHMalformedError: if vector is not in expected format for Red Hat notation
    CVSS3RHScoreDoesNotMatch: if vector and score do not match
/r   z0Malformed CVSS3 vector in Red Hat notation "{0}"r   zgCVSS3 vector in Red Hat notation "{0}" has score of "{1}" which does not match specified score of "{2}")split
ValueErrorr   formatfloatscoresr   )clsvectorscorebase_vectorscore_valuecvss_objects         r   from_rh_vectorCVSS3.from_rh_vector1   s    "	!'c1!5E
	,K
 +&"k1*FFLf!3!3!5a!8%G   	'BII&Q 	  	'BII&Q 	s   A/ B /%B%B<c                    Xl         SU l        0 U l        SU l        / U l        SU l        SU l        SU l        SU l        SU l	        SU l
        SU l        SU l        SU l        SU l        SU l        U R!                  5         U R#                  5         U R%                  5         U R'                  5         U R)                  5         U R+                  5         U R-                  5         g)z
Args:
    vector (str): string specifying CVSS3 vector, fields may be out of order, fields which
                  are not mandatory may be missing
N)r&   minor_versionmetricsoriginal_metricsmissing_metricsscopemodified_scope
base_scoretemporal_scoreenvironmental_scoreisc_baseiscescmodified_isc_basemodified_iscmodified_escparse_vectorcheck_mandatoryhandle_scopeadd_missing_optionalcompute_base_scorecompute_temporal_scorecompute_environmental_score)selfr&   s     r   __init__CVSS3.__init__Y   s     ! $!
""#' !%  !!#!##%((*r   c                    U R                   S:X  a  [        S5      eU R                   R                  S5      (       a  [        S5      eU R                   R                  S5      (       a  SU l        OLU R                   R                  S5      (       a  SU l        O$[        S	R                  U R                   5      5      e U R                   R                  S5      SS
 nU H  nUS:X  a$  [        SR                  U R                   5      5      e UR                  S5      u  p4U[        ;   a`  U[        U   ;   a9  X0R                  ;   a  [        SR                  U5      5      eX@R                  U'   M  [        SR                  XB5      5      e[        SR                  X25      5      e   g
! [         a%    [        SR                  U R                   5      5      ef = f! [         a    [        SR                  U5      5      ef = f)zm
Parses metrics from the CVSS3 vector.

Raises:
    CVSS3MalformedError: if vector is not in expected format
 z'Malformed CVSS3 vector, vector is emptyr   z$Malformed CVSS3 vector, trailing "/"z	CVSS:3.0/r   z	CVSS:3.1/r   zYMalformed CVSS3 vector "{0}" is missing mandatory prefix or uses unsupported CVSS versionNzMalformed CVSS3 vector "{0}"z!Empty field in CVSS3 vector "{0}":zMalformed CVSS3 field "{0}"zDuplicate metric "{0}"z"Unknown value "{0}" in field "{1}"z#Unknown metric "{0}" in field "{1}")r&   r   endswith
startswithr.   r"   r    
IndexErrorr!   r   r   r/   )rD   fieldsfieldmetricr   s        r   r=   CVSS3.parse_vectorz   s    ;;"%&OPP;;$$%&LMM ;;!!+..!"D[[##K00!"D%3396$++3F 
	Z[[&&s+AB/F
 E{)*M*T*TUYU`U`*abbW %C 0 ..N622-12J2Q2QRX2YZZ+0LL(-<CCEQ  *9@@O ' 	  	Z%&D&K&KDKK&XYY	Z  W)*G*N*Nu*UVVWs   <F$ 
G$/G%G;c                     U R                   S   U l        U R                   R                  SS5      U l        U R                  S;   a  U R                  U l        gg)zC
Sets scope and modified scope variables based on S and MS metrics
SMSN)NX)r/   r2   getr3   rD   s    r   r?   CVSS3.handle_scope   sL     \\#&
"ll..tT:+-"&**D .r   c                     / n[          H%  nX R                  ;  d  M  UR                  U5        M'     U(       a)  [        SR	                  SR                  U5      5      5      eg)z
Checks if mandatory fields are in CVSS3 vector.

Raises:
    CVSS3MandatoryError: if mandatory metric is missing in the vector
zMissing mandatory metrics "{0}"z, N)r
   r/   appendr   r"   join)rD   missingmandatory_metrics      r   r>   CVSS3.check_mandatory   sV      1||3/0 !2 %&G&N&NtyyY`Oa&bcc r   c                     [         R                   " U R                  5      U l        S HF  nXR                  ;  d  U R                  U   S:X  d  M'  U R                  USS    U R                  U'   MH     g)z
Adds missing optional parameters, so they match the mandatory ones. Original metrics are
also stored, as they may be used for printing back the minimal vector.
)MAVMACMPRMUIMCrS   MIMArT   r   N)copyr/   r0   )rD   abbreviations     r   r@   CVSS3.add_missing_optional   s]    
 !%		$,, 7PL<</4<<3MQT3T-1\\,qr:J-K\* Qr   c                     U R                   R                  US5      nUS:X  a  U R                  S:X  d  US:X  a7  U R                  S:X  a'  S[	        S5      [	        S5      [	        S5      S	.U   nU$ [
        U   U   nU$ )
z
Gets value of specific metric specified by its abbreviation. Handles exception for
Privileges Required based on Scope or Modified scope.
rT   PRCra   Nz0.85z0.68z0.50)rT   NLH)r/   rU   r2   r3   r   r   rD   rg   string_valueresults       r   	get_valueCVSS3.get_value   s    
 ||''c:D TZZ3%6E!d&9&9S&@aiaiaiPQ]^F  $L1,?Fr   c                 V    U R                   R                  US5      n[        U   U   nU$ )zL
Gets textual description of specific metric specified by its abbreviation.
rT   )r/   rU   r   ro   s       r   get_value_descriptionCVSS3.get_value_description   s.     ||''c:$\2<@r   c                     [        S5      [        S5      U R                  S5      -
  [        S5      U R                  S5      -
  -  [        S5      U R                  S5      -
  -  -
  U l        g)zD
ISCBase = 1 - [(1-ImpactConf) x (1-ImpactInteg) x (1-ImpactAvail)]
1rk   IAN)r   rr   r7   rV   s    r   compute_isc_baseCVSS3.compute_isc_base   s]     #sVdnnS))vs++-vs++-
r   c                 |   U R                   S:X  a  [        S5      U R                  -  U l        g
U R                   S:X  a[  [        S5      U R                  [        S5      -
  -  [        S5      U R                  [        S5      -
  [        S5      -  -  -
  U l        g
[	        S	R                  U R                   5      5      e)z`
Scope Unchanged 6.42 x ISCBase
Scope Changed 7.52 x [ISCBase-0.029] - 3.25 x [ISCBase-0.02]^15
U6.42rk   7.520.0293.250.0215zInvalid Scope: "{0}"N)r2   r   r7   r8   RuntimeErrorr"   rV   s    r   compute_iscCVSS3.compute_isc   s    
 ::y4==0DHZZ3yDMMAgJ$>?!F)&	)4O C DH 5<<TZZHIIr   c                     [        S5      U R                  S5      -  U R                  S5      -  U R                  S5      -  U R                  S5      -  U l        g)zN
8.22 x AttackVector x AttackComplexity x PrivilegeRequired x UserInteraction
8.22AVACrj   UIN)r   rr   r9   rV   s    r   compute_escCVSS3.compute_esc   sZ    
 fInnT"#nnT"# nnT"# nnT"	# 	r   c                     U R                  5         U R                  5         U R                  5         U R                  [	        S5      ::  a  [	        S5      U l        gU R                  S;   d   eU R                  S:X  a;  [        [        U R                  U R                  -   [	        S5      5      5      U l        gU R                  S:X  aG  [        [        [	        S5      U R                  U R                  -   -  [	        S5      5      5      U l        gg)z
If (Impact sub score =< 0) 0 else,
Scope Unchanged Round up (Minimum [(Impact + Exploitability),10])
Scope Changed Round up (Minimum [1.08 x (Impact + Exploitability),10])
0.0)r~   rk   r~   10rk   1.08N)
r{   r   r   r8   r   r4   r2   r   minr9   rV   s    r   rA   CVSS3.compute_base_score
  s     	88qxhDO::+++zzS "*34880Cag+N"Os""*3qyDHHtxx<O/PRSTXRY+Z"[ #r   c                     [        U R                  U R                  S5      -  U R                  S5      -  U R                  S5      -  5      U l        g)zQ
Round up(BaseScore x ExploitCodeMaturity x RemediationLevel x ReportConfidence)
ERLRCN)r   r4   rr   r5   rV   s    r   rB   CVSS3.compute_temporal_score  sC     'OOdnnS11DNN44HH4>>Z^K__
r   c           	      j   [        [        S5      [        S5      U R                  S5      U R                  S5      -  -
  [        S5      U R                  S5      U R                  S5      -  -
  -  [        S5      U R                  S5      U R                  S5      -  -
  -  -
  [        S5      5      U l        g	)
zW
ISCModified = Minimum[[1-(1-M.IConf x CR)x(1-M.IInteg x IR)x(1-M.IAvail x AR)],0.915]
rx   rc   CRrd   IRre   ARz0.915N)r   r   rr   r:   rV   s    r   compute_modified_isc_baseCVSS3.compute_modified_isc_base%  s    
 "%#S6DNN404>>$3GGGS6DNN404>>$3GGGIS6DNN404>>$3GGGII
 gJ"
r   c                    U R                   S:X  a  [        S5      U R                  -  U l        g[        S5      U R                  [        S5      -
  -  [        S5      U R                  [        S5      -
  [        S5      -  -  -
  U l        g)	z
This is CVSS:3.0 version

If Modified Scope Unchanged    6.42 x [ISCModified]
If Modified Scope Changed      7.52 x [ISCModified-0.029] - 3.25 x [ISCModified-0.02]^15
r~   r   r   r   r   r   r   Nr3   r   r:   r;   rV   s    r   compute_modified_isc_30CVSS3.compute_modified_isc_304  s|     #% !&	D,B,B BD !&	T-C-Caj-P QTUV\T]&&624a U !Dr   c                 ,   U R                   S:X  a  [        S5      U R                  -  U l        g	[        S5      U R                  [        S5      -
  -  [        S5      U R                  [        S5      -  [        S5      -
  [        S5      -  -  -
  U l        g	)
z
This is CVSS:3.1 version

If Modified Scope Unchanged    6.42 x [ISCModified]
If Modified Scope Changed      7.52 x (ISCModified - 0.029) - 3.25 x
                               (ISCModified x 0.9731 - 0.02)^13
r~   r   r   r   r   z0.9731r   13Nr   rV   s    r   compute_modified_iscCVSS3.compute_modified_iscB  s     #% !&	D,B,B BD !&	T-C-Caj-P QTUV\T]&&84qy@4a U !Dr   c                     [        S5      U R                  S5      -  U R                  S5      -  U R                  S5      -  U R                  S5      -  U l        g)zV
8.22 x M.AttackVector x M.AttackComplexity x M.PrivilegeRequired x M.UserInteraction
r   r_   r`   ra   rb   N)r   rr   r<   rV   s    r   compute_modified_escCVSS3.compute_modified_escQ  s[    
 fInnU#$nnU#$ nnU#$ nnU#	$ 	r   c                    U R                  5         U R                  S:X  a  U R                  5         OU R                  5         U R	                  5         U R
                  [        S5      ::  a  [        S5      U l        g	U R                  S:X  a6  [        [        U R
                  U R                  -   [        S5      5      5      nOA[        [        [        S5      U R
                  U R                  -   -  [        S5      5      5      n[        XR                  S5      -  U R                  S5      -  U R                  S5      -  5      U l        g	)
a  
If (Modified Impact Sub score =< 0)    0 else,
If Modified Scope Unchanged    Round up(Round up (Minimum [
                  (M.Impact + M.Exploitability),10])
                  x Exploit Code Maturity
                  x Remediation Level
                  x Report Confidence)

If Modified Scope Changed    Round up(Round up (Minimum [1.08
                  x (M.Impact + M.Exploitability),10])
                  x Exploit Code Maturity
                  x Remediation Level
                  x Report Confidence))
r   r   r~   r   r   r   r   r   N)r   r.   r   r   r   r;   r   r6   r3   r   r   r<   rr   )rD   modifieds     r   rC   !CVSS3.compute_environmental_score]  s    	&&("((*%%'!!#%('(xD$""c)#C):):T=N=N)NQRSWQX$YZ#&	T%6%69J9J%JKQtWU (0>>#..1EEW[H\\(D$r   c                 ~    [        U R                  5      [        U R                  5      [        U R                  5      4$ )zo
Returns all computed scores.

Returns:
    (tuple of floats): Base Score, Temporal Score, Environmental Score
)r#   r4   r5   r6   rV   s    r   r$   CVSS3.scores  s0     T__%uT-@-@'A5IaIaCbbbr   c                 "   / n[          HK  nX0R                  ;   d  M  U R                  U   nUS:w  d  M+  UR                  SR                  X45      5        MM     U(       a  SR                  U R                  5      nOSnUSR                  U5      -   $ )z
Returns vector without optional metrics marked as X and in preferred order.

Args:
    output_prefix (bool): defines if CVSS vector should be printed with prefix

Returns:
    (str): cleaned CVSS3 with metrics in correct order
rT   z{0}:{1}zCVSS:3.{0}/rH   r   )r   r0   rY   r"   r.   rZ   )rD   output_prefixr&   rO   r   prefixs         r   clean_vectorCVSS3.clean_vector  s     +F...--f5C<MM)"2"26"AB	 ,
 "))$*<*<=FF(((r   c                    / nU R                   U R                  U R                  4 H  nU[        S5      :X  a  UR	                  S5        M%  U[        S5      ::  a  UR	                  S5        MG  U[        S5      ::  a  UR	                  S5        Mi  U[        S5      ::  a  UR	                  S5        M  UR	                  S	5        M     [        U5      $ )
z
Returns severities based on scores.

Returns:
    (tuple): Base Severity, Temporal Severity, Environmental Severity as strings
r   Nonez3.9Lowz6.9Mediumz8.9HighCritical)r4   r5   r6   r   rY   tuple)rD   
severitiesr'   s      r   r   CVSS3.severities  s     
oot':':D<T<TUE% !!&)!E("!!%(!E("!!(+!E("!!&)!!*- V Z  r   c                 b    [        U R                  5       S   5      S-   U R                  5       -   $ )z
Returns cleaned vector with score in Red Hat notation, e.g. score/vector.

Example: 6.5/CVSS:3.0/AV:P/AC:H/PR:H/UI:R/S:C/C:H/I:H/A:N/E:H/RL:O/RC:R/CR:H/MAC:H/MC:L
r   r   )strr$   r   rV   s    r   	rh_vectorCVSS3.rh_vector  s-     4;;=#$s*T->->-@@@r   c           
          SR                  [         Vs/ s H$  oS-   U R                  R                  US5      -   PM&     sn5      $ s  snf )zc
Returns the temporal vector from the full CVSS vector.

Returns:
    (str): Temporal CVSS vector.
r   rI   rT   )rZ   r   r/   rU   rD   rO   s     r   temporal_vectorCVSS3.temporal_vector  sD     xxHXYHXfc\DLL,,VS99HXY
 	
Y   +Ac           
          SR                  [         Vs/ s H$  oS-   U R                  R                  US5      -   PM&     sn5      $ s  snf )zm
Returns the environmental vector from the full CVSS vector.

Returns:
    (str): Environmental CVSS vector.
r   rI   rT   )rZ   r   r/   rU   r   s     r   environmental_vectorCVSS3.environmental_vector  sD     xxH]^H]fc\DLL,,VS99H]^
 	
^r   c                   ^ ^^	 S m	UU U	4S jnT R                  5       u  pEnS[        T R                  5      -   T R                  S.m[         H  nU" U5        M     [        T R                  5      TS'   T	" U5      TS'   U(       a  [        U 4S j[         5       5      (       a8  [         H  nU" U5        M     [        T R                  5      TS'   T	" U5      TS	'   U(       a  [        U 4S
 j[         5       5      (       a8  [         H  nU" U5        M     [        T R                  5      TS'   T	" U5      TS'   U(       a"  [        [        TR                  5       5      5      mT$ )a  
Returns a dictionary formatted with attribute names and values defined by the official
CVSS JSON schema:

CVSS v3.0: https://www.first.org/cvss/cvss-v3.0.json?20170531
CVSS v3.1: https://www.first.org/cvss/cvss-v3.1.json?20190610

Serialize a `cvss` instance to JSON with:

json.dumps(cvss.as_json())

Or get sorted JSON in an OrderedDict with:

json.dumps(cvss.as_json(sort=True))

Returns:
    (dict): JSON schema-compatible CVSS representation
c                 p    U S:X  a  gU R                  5       R                  SS5      R                  SS5      $ )NAdjacentADJACENT_NETWORK-_ )upperreplace)texts    r   usCVSS3.as_json.<locals>.us  s5     z!)::<''S199#sCCr   c                 L   > [         U    nT" TR                  U 5      5      TU'   g N)r	   ru   )rO   kdatarD   r   s     r   add_metric_to_data)CVSS3.as_json.<locals>.add_metric_to_data  s&    *62A33F;<DGr   z3.)versionvectorString	baseScorebaseSeverityc              3   @   >#    U  H  oTR                   ;   v   M     g 7fr   r0   .0rO   rD   s     r   	<genexpr> CVSS3.as_json.<locals>.<genexpr>  s     ]L\&(=(==L\   temporalScoretemporalSeverityc              3   @   >#    U  H  oTR                   ;   v   M     g 7fr   r   r   s     r   r   r     s     bLa&(=(==Lar   environmentalScoreenvironmentalSeverity)r   r   r.   r&   r
   r#   r4   anyr   r5   r   r6   r   sorteditems)
rD   sortminimalr   base_severitytemporal_severityenvironmental_severityrO   r   r   s
   `       @@r   as_jsonCVSS3.as_json  s3   (	D	= DH??CT@*@ c$"4"455 KK

 (Fv& (!$//2[!-0^#]L\]]]*"6* +$)$*=*=$>D!')*;'<D#$#bLabbb/"6* 0).t/G/G)HD%&,./E,FD()vdjjl34Dr   c                 4    [        U R                  5       5      $ r   )hashr   rV   s    r   __hash__CVSS3.__hash__  s    D%%'((r   c                 p    [        U[        5      (       a!  U R                  5       UR                  5       :H  $ g)NF)
isinstancer   r   )rD   os     r   __eq__CVSS3.__eq__  s-    a$$&!..*:::r   )r4   r6   r9   r8   r7   r/   r.   r1   r<   r;   r:   r3   r0   r2   r5   r&   N)T)FF)"__name__
__module____qualname____firstlineno____doc__classmethodr+   rE   r=   r?   r>   r@   rr   ru   r{   r   r   rA   rB   r   r   r   r   rC   r$   r   r   r   r   r   r   r   r   __static_attributes__ r   r   r   r   ,   s     % %N+B3j-dL
J

\&



!Fc),!*A	
	
:x)r   r   )r  
__future__r   rf   decimalr   r   r   
constants3r   r   r	   r
   r   r   r   r   
exceptionsr   r   r   r   r   objectr   r  r   r   <module>r     sD   
 (  !  	 	 	 <hF hr   