R/exploitDB.R

# Pre: exploitdb dataframe
# Post: exploitdb dataframe
# Description: exploitdb final form
redifine <- function(exploitdb) {
  aux <- stringr::str_split_fixed(exploitdb$description, " - ",Inf)
  exploitdb$description <- aux[,1]
  exploitdb$subtype <- aux[,2]
  exploitdb$port <- NULL
  exploitdb$date <- as.Date(exploitdb$date)
  return(exploitdb)
}

# Pre: -
# Post: return all exploitdb dataframe
# Description: download and read csv
get_exploitdb_All <- function() {
  csv <- RCurl::getURL("https://raw.githubusercontent.com/offensive-security/exploit-database/master/files.csv")
  db <- read.csv(text = csv,stringsAsFactors = F)
  return(db)
}

# Pre: integer (by default 7), exploitdb dataframe (by default NULL)
# Post: exploitdb dataframe
# Description: get exploitdb dataframe and it creates a new one delimited by a time period
get_exploitdb <- function(days=7,csv=NULL) {
  if (is.null(csv))
    exploitdb <- get_exploitdb_All()
  else
    exploitdb <- csv


  if (days == Inf) {
    exploitdb <- redifine(exploitdb)
    return (exploitdb)
  }
  else {
    exploitdb_7d <-  exploitdb[exploitdb$date > Sys.Date() - days,]
    exploitdb_7d <- redifine(exploitdb_7d)
    return(exploitdb_7d)
  }
}

# Pre: integer (by default 7, Inf allowed), integer (by default 10), exploitdb dataframe (by default NULL)
# Post: platform statistics datframe
# Description: get the statistics of the platforms
get_platform_stats <- function(days=7,top=10,csv=NULL) {
  if (days == Inf) {
    exploitdb <- get_exploitdb(Inf,csv)
  }
  else {
    exploitdb <- get_exploitdb(days,csv)
  }
  db <- dplyr::count(exploitdb,platform)
  db <- dplyr::mutate(db, percent=round(n/nrow(exploitdb) * 100,digits=2))
  db <- dplyr::arrange(db, desc(n))
  db <- dplyr::slice(db, 1:top)
  return(db)
}

# Pre: integer (by default 7, Inf allowed), integer (by default 10), exploitdb dataframe (by default NULL)
# Post: platform statistics datframe
# Description: get the statistics of the exploit types
get_type_stats <- function(days=7,top=10,csv=NULL) {
  if (days == Inf) {
    exploitdb <- get_exploitdb(Inf,csv)
  }
  else {
    exploitdb <- get_exploitdb(days,csv)
  }
  db <- dplyr::count(exploitdb,type)
  db <- dplyr::mutate(db, percent=round(n/nrow(exploitdb) * 100,digits=2))
  db <- dplyr::arrange(db, desc(n))
  db <- dplyr::slice(db, 1:top)
  return(db)
}

# Pre: integer (by default 7, Inf allowed), integer (by default 10), exploitdb dataframe (by default NULL)
# Post: platform statistics datframe
# Description: get the statistics of the exploit subtypes
get_subtype_stats <- function(days=7,top=10,csv=NULL) {
  if (days == Inf) {
    exploitdb <- get_exploitdb(Inf,csv)
  }
  else {
    exploitdb <- get_exploitdb(days,csv)
  }
  db <- dplyr::count(exploitdb,subtype)
  db <- dplyr::mutate(db, percent=round(n/nrow(exploitdb) * 100,digits=2))
  db <- dplyr::arrange(db, desc(n))
  db <- dplyr::slice(db, 1:top)
  return(db)
}

# Pre: integer (by default 7, Inf allowed), integer (by default 10), exploitdb dataframe (by default NULL)
# Post: platform statistics datframe
# Description: get the statistics of the exploit authors
get_author_stats <- function(days=7,top=10,csv=NULL) {
  if (days == Inf) {
    exploitdb <- get_exploitdb(Inf,csv)
  }
  else {
    exploitdb <- get_exploitdb(days,csv)
  }
  db <- dplyr::count(exploitdb,author)
  db <- dplyr::mutate(db, percent=round(n/nrow(exploitdb) * 100,digits=2))
  db <- dplyr::arrange(db, desc(n))
  db <- dplyr::slice(db, 1:top)
  return(db)
}

# Pre: integer (by default 7, Inf allowed), integer (by default 10), exploitdb dataframe (by default NULL)
# Post: platform statistics datframe
# Description: get the statistics of the exploit platform and types
get_platform_type_stats <- function(days=7,top=10,csv=NULL) {
  if (days == Inf) {
    exploitdb <- get_exploitdb(Inf,csv)
  }
  else {
    exploitdb <- get_exploitdb(days,csv)
  }
  db <- dplyr::count(exploitdb,platform,type)
  db <- dplyr::mutate(db, percent=round(n/nrow(exploitdb) * 100,digits=2))
  db <- dplyr::arrange(db, desc(n))
  return(db[1:top,])
}
mrcllbr/SoCoTools documentation built on May 23, 2019, 7:14 a.m.