The sample CSV files are a random sample of the full sized dataset. The use-case is primarily to provide ease of access for users to inspect the dataset using spreadsheet software instead of a development environment. The sample CSV dataset is also useful for developing and debugging scripts on lighter hardware like a laptop. The sample CSV files are a pseudo-random sample of the full dataset using Pyspark's pyspark.sql.DataFrame.sample() function. The pseudo-random sample was produced using the seed value 4321 for reproducibility. The sample size was selected to be 100,000 total records including benign with a 50:50 ratio of attack data to benign data. The 100,000 sample size was selected to more-or-less align with the size of previous datasets where the number of records in those published sample CSV files was up to about 100,000. Because the Mitre ATT&CK Tactic is our primary label, and due to the flattening issue related to generalizing the Mitre Technique to the corresponding Tactics (see demonstration script at the end of this readme), only 'label_tactic' is roughly a 50:50 ratio. The other labels, 'label_technique' and 'label_tactic', do not have a 50:50 ratio because some techniques correspond to multiple tactics. In the interest of ensuring there is only one label per record, we had to flatten (or explode in Spark parlance) the dataset. This is the reason for the 'Duplicate' labels. The label name 'Duplicate' was selected to ensure that users take notice and seek out the reason for the label, and also to make it easier to filter out the 'Duplicate' labels. We've included, at the end of this readme, a small sample script which demonstrates how the 'Duplicate' labels end up in the dataset. Reading CSVs in Spark or Pandas: ========================================================================================================== Due to the nature of the Syslog data collected, the 'syslog-message' column has complex data which is incompatible with standard CSV formatting for spreadsheet software. In order to allow our dataset to be easily accessed using spreadsheet software, we had to use some particular configuration options with Spark's dataframe writer that must also be set when reading the CSV data back in to Spark (or Pandas). Below, you can find an example for how to read the sample CSV files for both Pyspark and Pandas: Spark CSV Dataframe Reader Configuration Options: spark_df = spark.read.option("header", True) .option("encoding", "UTF-8") .option("multiLine", "true") .option("quote", '"') .option("escape", '"') .csv(csv_file_path) Pandas CSV Dataframe Reader Configuration Options: pd_df = pd.read_csv(csv_file_path, header=0, encoding="utf-8", quotechar='"', doublequote=True) CSV Record Counts for Each Label: ========================================================================================================== label_tactic ---------------------------------------------------------------------------------------------------- Counts of all labels including 'none' (benign) and 'Duplicate' labels: +--------------------+-----+ | label_tactic|count| +--------------------+-----+ | none|50439| | Persistence|19034| |Privilege Escalation|14262| | Defense Evasion| 8686| | Execution| 3515| | Collection| 2635| | Credential Access| 2635| +--------------------+-----+ Counts of attack, benign, total, and percentages of each after filtering 'Duplicate' labels: Benign Count: 50,439 Attack Count: 50,767 Total Count: 101,206 Percent Benign: 0.4983795427148588 Percent Attack: 0.5016204572851412 label_technique ---------------------------------------------------------------------------------------------------- Counts of all labels including 'none' (benign) and 'Duplicate' labels: +---------------+-----+ |label_technique|count| +---------------+-----+ | none|50439| | Duplicate|20420| | T1098|10469| | T1136| 5042| | T1562| 4112| | T1053| 3515| | T1056| 2635| | T1027| 2335| | T1036| 2239| +---------------+-----+ Counts of attack, benign, total, and percentages of each after filtering 'Duplicate' labels: Benign Count: 50,439 Attack Count: 30,347 Total Count: 80,786 Percent Benign: 0.6243532295199663 Percent Attack: 0.37564677048003364 label_binary ---------------------------------------------------------------------------------------------------- Counts of all labels including 'none' (benign) and 'Duplicate' labels: +------------+-----+ |label_binary|count| +------------+-----+ | False|50439| | True|30347| | Duplicate|20420| +------------+-----+ Counts of attack, benign, total, and percentages of each after filtering 'Duplicate' labels: Benign Count: 50,439 Attack Count: 30,347 Total Count: 80,786 Percent Benign: 0.6243532295199663 Percent Attack: 0.37564677048003364 Duplicate Label Demonstration Script ========================================================================================================== - Requires mitreattack-python library to be installed. There is a Jupyter magic command in the script to automatically install the mitreattack-python library, but if you aren't using a Jupyter interactive environment, you will need to install the library manually and then remove/comment the Jupyter magic command on line 7. # Spark imports import pyspark from pyspark.sql import SparkSession import pyspark.sql.functions as F # Imports for getting the enterprise attacks dataset for translating mitre attack techniques to tactics. %pip install mitreattack-python # Note: This permanently installs the official MITRE ATT&CK STIX Python library permanently. import mitreattack.attackToExcel.attackToExcel as attackToExcel import mitreattack.attackToExcel.stixToDf as stixToDf spark = SparkSession.builder.getOrCreate() # Setup Spark session on local machine. # Getting the data from 'mitreattack-python' library was significantly condensed here to save space, at the cost of readability. # Download and parse ATT&CK STIX data. Select v18.1 which has the old 'Defense Evasion' Tactic that was changed with v19. attackdata = attackToExcel.get_stix_data("enterprise-attack", "v18.1") # Get Pandas dataframe from stix mitreattack-python library, select the "enterprise-attack" dataset, and select the Excel sheet with 'techniques'. stix_techniques = spark.createDataFrame(stixToDf.techniquesToDf(attackdata, "enterprise-attack")["techniques"].astype("str")) # Setup the dataframe with an 'index' column which succinctly represents all of the data columns that will be duplicated # after flattening the dataset. The second, 'technique', column contains the Mitre Techniques used for this demonstration, # which were selected from this dataset specifically because they have 1, 2, and 3 Mitre Tactics per Technique. data = [(1, "T1036"), (2, "T1098"), (3, "T1053")] # Data for the dataframe. columns = ["index", "technique"] # Column names. Although the index is used to sort, it works without explicit data types. techniques_df = spark.createDataFrame(data, schema=columns) print("The starting dataframe for this example. Note that 'index' can be assumed to be all data columns:") techniques_df.show() # Join the example 'techniques_df' dataframe with the MITRE STIX dataframe. We only need the techniques ('ID') and tactics columns from the # STIX dataframe, so those are selected explicitly before joining the local dataframe's 'technique' column with the STIX 'ID' column. print("The dataframe after joining the technique with the mitreattack-python library's stix data containing 'ID' (technique) and 'tactics':") tactic_df = techniques_df.join(stix_techniques.select(["ID", "tactics"]), techniques_df["technique"] == stix_techniques["ID"], how="leftouter") tactic_df.show(truncate = 100) # Shows all columns in the joined dataframe. # Split the tactics column on ', ' delimiter into an array type column. For illustrative purposes, a count of the number of array # elements was added in the 'tactics_per_technique' column. print("The dataframe after splitting the tactics on ', ' delimiter to create an ArrayType column, and also adding a new column indicating the number of array elements:") tactic_split_df = tactic_df.withColumn("tactics_array", F.split(F.col("tactics"), ", ")).withColumn("tactics_per_technique", F.size(F.col("tactics_array"))).select("index", "technique", "tactics_array", "tactics_per_technique") tactic_split_df.orderBy(F.col("index")).show(truncate = 100) # Next, explode the 'tactics_array' column using posexplode so that we get one tactic label per row. Using posexplode so that the array # element's position can be used to appropriately apply the 'Duplicate' label afterwards. The tactic column was also renamed to match # the naming convention for our labeled data columns, which was borrowed from the UNSW-15 dataset's naming convention. tactic_explode_df = tactic_split_df.select("*", F.posexplode("tactics_array").alias("array_position", "tactic")) \ .withColumn("label_binary", F.when(F.col("array_position") > 0, F.lit("Duplicate")).otherwise(F.lit("True"))) \ .withColumn("label_technique", F.when(F.col("array_position") > 0, F.lit("Duplicate")).otherwise(F.col("technique"))) \ .withColumn("label_tactic", F.col("tactic")) print("Dataframe after the MITRE Tactics were split into an array and flattened to produce one tactic label per row") tactic_explode_df.orderBy(F.col("index"), F.col("array_position")).select("index", "technique", "tactics_array", "tactics_per_technique", "array_position", "tactic").show(truncate = 100) print("Dataframe after the technique and binary labels were added. Note that the 'Duplicate' label is present for rows where 'array_position' > 0.") tactic_explode_df.orderBy(F.col("index"), F.col("array_position")).select("index", "technique", "tactics_array", "tactics_per_technique", "array_position", "label_technique", "label_tactic", "label_binary").show(truncate = 100) print("Dataframe after dropping out the 'tactics_per_technique' and 'array_position' columns to make it a little easier to inspect.") tactic_explode_df.orderBy(F.col("index"), F.col("array_position")).select("index", "tactics_array", "label_technique", "label_tactic", "label_binary").show(truncate = 100) print("When we use the data in for training, we select the data columns and ONE label column. For instance, if we want to use train a dataset, we need to select a single label, ex: 'label_tactic', like below") print() tactic_explode_df.select("index", "label_tactic").show() print("But when we select one of the other labels ('label_technique' or 'label_binary'), we would end up with duplicate indexes (data rows) due to the explode operation on the 'label_tactic' column:") tactic_explode_df.select("index", "label_technique").show() print("So the 'Duplicate' label was used here to make it easier to filter. We could have chosen Null or an empty string, but this seemed to be more intuitive as a user would naturally question the presence of a 'Duplicate' label:") tactic_explode_df.select("index", "label_technique").filter(F.col("label_technique") != "Duplicate").show()