{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from pymongo import MongoClient\n", "import pandas as pd\n", "import json\n", "from bson.json_util import dumps\n", "from pandas.io.json import json_normalize" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ip = '127.0.0.1'\n", "port = 27017\n", "client = MongoClient(ip, port)\n", "db = client['Vulnerabilities']\n", "joinedVulnerabilitiesDateFormat = db['Vulnerabilities']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def getSeverityAndImpact(component):\n", " return joinedVulnerabilitiesDateFormat.aggregate([\n", " { \"$project\" : \n", " { \n", " \"_id\" : 0,\n", " \"severity\" : \"$webScrapingInformation.cvssScoreNVD2.severity\",\n", " \"impact\" : \"$webScrapingInformation.cvssScoreNVD2.\"+component+\"Impact\",\n", " \"impactLabel\": {\"$literal\": component.title()}\n", " }\n", " }])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def getDataFrameSeverityAndImpact():\n", " dataFrame = pd.DataFrame([])\n", " array = [\"confidentiality\",\"availability\",\"integrity\"]\n", " for element in array: \n", " MongoResponse = getSeverityAndImpact(element)\n", " dataFromMongoResponse = json.loads(dumps(MongoResponse)) \n", " dataFrameTemp = json_normalize(dataFromMongoResponse)\n", " dataFrame = dataFrame.append(pd.DataFrame(dataFrameTemp))\n", " return dataFrame.reset_index()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "dfResult = getDataFrameSeverityAndImpact()\n", "del dfResult['index']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def returnImpact(imp):\n", " if \"None\" in imp:\n", " return 1\n", " if \"Partial\" in imp:\n", " return 2\n", " if \"Complete\" in imp:\n", " return 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def returnSeverity(sev):\n", " if \"LOW\" in sev:\n", " return 1\n", " if \"MEDIUM\" in sev:\n", " return 2\n", " if \"HIGH\" in sev:\n", " return 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "dfResult['impact'] = dfResult['impact'].apply(returnImpact)\n", "dfResult['severity'] = dfResult['severity'].apply(returnSeverity)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "dfResult = dfResult[['severity','impact','impactLabel']]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Dataframe created and appended due visualizations reasons\n", "dataTemp = {'severity': [0, 4], 'impact': [0, 4], 'impactLabel': ['Integrity', 'Integrity']}\n", "dfTemp = pd.DataFrame(data=dataTemp)\n", "dfTemp = dfTemp[['severity','impact','impactLabel']]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "dfResult = dfResult.append(dfTemp, ignore_index=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "dfResult.to_csv(path_or_buf=\"vsaiEMSE.csv\", index=False)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }