Skip to content

Instantly share code, notes, and snippets.

@ChrSteinert
Created July 30, 2022 19:57
Show Gist options
  • Save ChrSteinert/3ba1e6d0ed6d5fa84e90d4b2d18748d4 to your computer and use it in GitHub Desktop.
Save ChrSteinert/3ba1e6d0ed6d5fa84e90d4b2d18748d4 to your computer and use it in GitHub Desktop.
#r "nuget:FParsec"
open FParsec
type AttackVector =
| Network
| AdjacentNetwork
| Local
| Physical
type AttackComplexity =
| Low
| High
type PrivilegesRequired =
| None
| Low
| High
type UserInteraction =
| None
| Required
type Scope =
| Unchanged
| Changed
type ConfidentialityImpact =
| None
| Low
| Hight
type IntegrityImpact =
| None
| Low
| Hight
type AvailabilityImpact =
| None
| Low
| Hight
let attackVectorParser : Parser<AttackVector, unit> =
let vectors =
choice [
skipChar 'N' >>% AttackVector.Network
skipChar 'A' >>% AttackVector.AdjacentNetwork
skipChar 'L' >>% AttackVector.Local
skipChar 'P' >>% AttackVector.Physical
]
skipString "AV:"
>>. vectors
let attackComplexityParser : Parser<AttackComplexity, unit> =
let complexities =
choice [
skipChar 'L' >>% AttackComplexity.Low
skipChar 'H' >>% AttackComplexity.High
]
skipString "AC:"
>>. complexities
let privilegesRequiredParser : Parser<PrivilegesRequired, unit> =
let privileges =
choice [
skipChar 'N' >>% PrivilegesRequired.None
skipChar 'L' >>% PrivilegesRequired.Low
skipChar 'H' >>% PrivilegesRequired.High
]
skipString "PR:"
>>. privileges
let userInteractionParser : Parser<UserInteraction, unit> =
let interactions =
choice [
skipChar 'N' >>% UserInteraction.None
skipChar 'R' >>% UserInteraction.Required
]
skipString "UI:"
>>. interactions
let scopeParser : Parser<Scope, unit> =
let scopes =
choice [
skipChar 'U' >>% Scope.Unchanged
skipChar 'C' >>% Scope.Changed
]
skipString "S:"
>>. scopes
let confidentialityImpactParser : Parser<ConfidentialityImpact, unit> =
let impacts =
choice [
skipChar 'N' >>% ConfidentialityImpact.None
skipChar 'L' >>% ConfidentialityImpact.Low
skipChar 'H' >>% ConfidentialityImpact.Hight
]
skipString "C:"
>>. impacts
let integrityImpactParser : Parser<IntegrityImpact, unit> =
let impacts =
choice [
skipChar 'N' >>% IntegrityImpact.None
skipChar 'L' >>% IntegrityImpact.Low
skipChar 'H' >>% IntegrityImpact.Hight
]
skipString "I:"
>>. impacts
let availabilityImpactParser : Parser<AvailabilityImpact, unit> =
let impacts =
choice [
skipChar 'N' >>% AvailabilityImpact.None
skipChar 'L' >>% AvailabilityImpact.Low
skipChar 'H' >>% AvailabilityImpact.Hight
]
skipString "A:"
>>. impacts
type CVSS = {
AttackVector : AttackVector
AttackComplexity : AttackComplexity
PrivilegesRequired : PrivilegesRequired
UserInteraction : UserInteraction
Scope : Scope
ConfidentialityImpact : ConfidentialityImpact
IntegrityImpact : IntegrityImpact
AvailabilityImpact : AvailabilityImpact
}
let cvssParser : Parser<CVSS, unit> =
attackVectorParser >>= fun av ->
skipChar '/' >>. attackComplexityParser >>= fun ac ->
skipChar '/' >>. privilegesRequiredParser >>= fun pr ->
skipChar '/' >>. userInteractionParser >>= fun ui ->
skipChar '/' >>. scopeParser >>= fun s ->
skipChar '/' >>. confidentialityImpactParser >>= fun ci ->
skipChar '/' >>. integrityImpactParser >>= fun ii ->
skipChar '/' >>. availabilityImpactParser >>= fun ai ->
{
AttackVector = av
AttackComplexity = ac
PrivilegesRequired = pr
UserInteraction = ui
Scope = s
ConfidentialityImpact = ci
IntegrityImpact = ii
AvailabilityImpact = ai
}
|> preturn
// Example
run cvssParser "AV:L/AC:H/PR:L/UI:R/S:C/C:L/I:L/A:N"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment