aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAdam Janovsky2023-06-15 15:21:22 +0200
committerAdam Janovsky2023-06-15 15:21:22 +0200
commit9799a40709fac5346a4770ab9870682cbeab8e54 (patch)
treed5ded2a398abfe5ec876c5e77c781639e40044e9
parent5640a954c2332331d33b219fe28d46be02484988 (diff)
downloadsec-certs-9799a40709fac5346a4770ab9870682cbeab8e54.tar.gz
sec-certs-9799a40709fac5346a4770ab9870682cbeab8e54.tar.zst
sec-certs-9799a40709fac5346a4770ab9870682cbeab8e54.zip
minor tweaks in reference notebook
-rw-r--r--notebooks/cc/references.ipynb246
1 files changed, 166 insertions, 80 deletions
diff --git a/notebooks/cc/references.ipynb b/notebooks/cc/references.ipynb
index 271dad5a..28e589ec 100644
--- a/notebooks/cc/references.ipynb
+++ b/notebooks/cc/references.ipynb
@@ -150,15 +150,8 @@
"df_id_rich[\"has_outgoing_indirect_references\"] = df_id_rich.indirectly_referencing.notnull()\n",
"df_id_rich[\"has_incoming_indirect_references\"] = df_id_rich.indirectly_referenced_by.notnull()\n",
"\n",
- "archived_cert_id_list = set(df_id_rich[df_id_rich.status == \"archived\"].cert_id)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": [
+ "archived_cert_id_list = set(df_id_rich[df_id_rich.status == \"archived\"].cert_id)\n",
+ "\n",
"def len_if_exists(x) -> int:\n",
" return len(x) if pd.notnull(x) else 0\n",
"\n",
@@ -240,7 +233,7 @@
"df_reach_evolution_melted = df_reach_evolution.melt(id_vars=\"date\", var_name=\"certificate\", value_name=\"reach\")\n",
"\n",
"g = sns.lineplot(data=df_reach_evolution_melted, x=\"date\", y=\"reach\", hue=\"certificate\")\n",
- "g.set(title=\"Certificate reach over time\", xlabel=\"Time\", ylabel=\"Certificate reach\")\n",
+ "g.set(title=\"Reach of top-10 certificates in time\", xlabel=\"Time\", ylabel=\"Certificate reach\")\n",
"plt.savefig(RESULTS_DIR / \"lineplot_top_certificate_reach.pdf\", bbox_inches=\"tight\")\n",
"plt.show()"
]
@@ -259,36 +252,66 @@
"metadata": {},
"outputs": [],
"source": [
- "def compute_avg_references(df: pd.DataFrame, smartcards: bool, variable: str) -> pd.DataFrame:\n",
+ "def compute_avg_references(df: pd.DataFrame, smartcards: bool, variable: str, date_range: pd.DateTimeIndex) -> dict:\n",
" \"\"\"\n",
- " Computes a series where index is date and value is average 'variable' value for certificates that were valid at that time,\n",
- " coming from either smardcards or other categories. E.g., variable can be `directly_referencing`.\n",
+ " Computes a dictionary (from which series can be created) where index is date and value is average 'variable' value\n",
+ " for certificates that were valid at that time, coming from either smardcards or other categories.\n",
+ " E.g., variable can be `directly_referencing`.\n",
" \"\"\"\n",
" df_copy = df.copy()\n",
" df_copy[\"target_variable\"] = df[variable].map(lambda x: len(x) if pd.notnull(x) else 0)\n",
"\n",
- " date_range = pd.date_range(df_copy.not_valid_before.min(), df_copy.not_valid_before.max())\n",
" dct = {}\n",
" for date in date_range:\n",
" if smartcards:\n",
- " dct[date] = df_copy.loc[(date >= df_copy.not_valid_before) & (date <= df_copy.not_valid_after) & (df_copy.category == \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")][\"target_variable\"].mean()\n",
+ " dct[date] = df_copy.loc[\n",
+ " (date >= df_copy.not_valid_before)\n",
+ " & (date <= df_copy.not_valid_after)\n",
+ " & (df_copy.category == \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ][\"target_variable\"].mean()\n",
" else:\n",
- " dct[date] = df_copy.loc[(date >= df_copy.not_valid_before) & (date <= df_copy.not_valid_after) & (df_copy.category != \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")][\"target_variable\"].mean()\n",
- " series_name = variable + \" smartcards\" if smartcards else variable + \" other categories\"\n",
- " return pd.Series(dct, name=series_name) \n",
+ " dct[date] = df_copy.loc[\n",
+ " (date >= df_copy.not_valid_before)\n",
+ " & (date <= df_copy.not_valid_after)\n",
+ " & (df_copy.category != \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ][\"target_variable\"].mean()\n",
+ " return dct\n",
"\n",
- "refs_smartcards = compute_avg_references(df_id_rich, True, \"directly_referencing\")\n",
- "trans_refs_smartcards = compute_avg_references(df_id_rich, True, \"indirectly_referencing\")\n",
- "refs_others = compute_avg_references(df_id_rich, False, \"directly_referencing\")\n",
- "trans_refs_others = compute_avg_references(df_id_rich, False, \"indirectly_referencing\")\n",
- "df_avg_num_refs = pd.concat([refs_smartcards, refs_others, trans_refs_smartcards, trans_refs_others], axis=1)\n",
+ "date_range = pd.date_range(df_id_rich.not_valid_before.min(), df_id_rich.not_valid_before.max())\n",
+ "\n",
+ "refs_smartcards = compute_avg_references(df_id_rich, True, \"directly_referencing\", date_range)\n",
+ "trans_refs_smartcards = compute_avg_references(df_id_rich, True, \"indirectly_referencing\", date_range)\n",
+ "refs_others = compute_avg_references(df_id_rich, False, \"directly_referencing\", date_range)\n",
+ "trans_refs_others = compute_avg_references(df_id_rich, False, \"indirectly_referencing\", date_range)\n",
+ "df_avg_num_refs = pd.concat(\n",
+ " [\n",
+ " pd.Series(refs_smartcards, name=\"smartcard references\"),\n",
+ " pd.Series(refs_others, name=\"other references\"),\n",
+ " pd.Series(trans_refs_smartcards, name=\"smartcard transitive references\"),\n",
+ " pd.Series(trans_refs_others, name=\"other transitive references\"),\n",
+ " ],\n",
+ " axis=1,\n",
+ ")\n",
"df_avg_num_refs.index.name = \"date\"\n",
"df_avg_num_refs = df_avg_num_refs.reset_index()\n",
"df_avg_num_refs_melted = df_avg_num_refs.melt(id_vars=[\"date\"], var_name=\"category\", value_name=\"n_references\")\n",
"\n",
- "reach_smartcards = compute_avg_references(df_id_rich, True, \"indirectly_referenced_by\")\n",
- "reach_others = compute_avg_references(df_id_rich, False, \"indirectly_referenced_by\")\n",
- "df_avg_reach = pd.concat([reach_smartcards, reach_others], axis=1)\n",
+ "reach_smartcards = compute_avg_references(df_id_rich, True, \"indirectly_referenced_by\", date_range)\n",
+ "reach_others = compute_avg_references(df_id_rich, False, \"indirectly_referenced_by\", date_range)\n",
+ "\n",
+ "df_vulnerable = df_id_rich.loc[df_id_rich.related_cves.notnull()]\n",
+ "reach_vuln_smartcards = compute_avg_references(df_vulnerable, True, \"indirectly_referenced_by\", date_range)\n",
+ "reach_vuln_others = compute_avg_references(df_vulnerable, False, \"indirectly_referenced_by\", date_range)\n",
+ "\n",
+ "df_avg_reach = pd.concat(\n",
+ " [\n",
+ " pd.Series(reach_smartcards, name=\"transitively referenced smartcards\"),\n",
+ " pd.Series(reach_others, name=\"transitively referenced by others\"),\n",
+ " pd.Series(reach_vuln_smartcards, name=\"transitively referenced vulnerable smartcard\"),\n",
+ " pd.Series(reach_vuln_others, name=\"transitively referenced vulnerable other cert.\"),\n",
+ " ],\n",
+ " axis=1,\n",
+ ")\n",
"df_avg_reach.index.name = \"date\"\n",
"df_avg_reach = df_avg_reach.reset_index()\n",
"df_avg_reach_melted = df_avg_reach.melt(id_vars=[\"date\"], var_name=\"category\", value_name=\"certificate reach\")\n",
@@ -298,10 +321,10 @@
"plt.savefig(RESULTS_DIR / \"lineplot_avg_n_references.pdf\", bbox_inches=\"tight\")\n",
"plt.show()\n",
"\n",
- "g = sns.lineplot(data=df_avg_reach_melted, x=\"date\", y=\"certificate reach\", hue=\"category\")\n",
+ "g = sns.lineplot(data=df_avg_reach_melted, x=\"date\", y=\"certificate reach\", hue=\"category\", errorbar=None)\n",
"g.set(title=\"Average reach of a certificate in time\", xlabel=\"Time\", ylabel=\"Reach\")\n",
"plt.savefig(RESULTS_DIR / \"lineplot_avg_reach.pdf\", bbox_inches=\"tight\")\n",
- "plt.show()"
+ "plt.show()\n"
]
},
{
@@ -375,30 +398,130 @@
"metadata": {},
"outputs": [],
"source": [
- "# For every date range, I first get a subset of all certificates that are valid and have some references in that date.\n",
- "# - I also get a list of all cert_ids that are valid at that date\n",
- "# - Then I take a dataframe subset of certificates that have non-empty outgoing references, but the intersection of their refs with all cert_ids valid at that date (previous row) is empty => they reference an archived cert. \n",
- "\n",
"date_range = pd.date_range(df_id_rich.not_valid_before.min(), df_id_rich.not_valid_before.max())\n",
- "dct_direct = {}\n",
- "dct_transitive = {}\n",
+ "dct_direct_others = {}\n",
+ "dct_direct_smartcards = {}\n",
+ "dct_transitive_others = {}\n",
+ "dct_transitive_smartcards = {}\n",
"for date in tqdm(date_range):\n",
" active_certs = df_id_rich.loc[(date >= df_id_rich.not_valid_before) & (date <= df_id_rich.not_valid_after)].copy()\n",
" active_certs_cert_ids = set(active_certs[\"cert_id\"].tolist())\n",
- " active_certs[\"no_intersection\"] = active_certs.directly_referencing.map(lambda x: False if pd.isnull(x) else not x.intersection(active_certs_cert_ids))\n",
- " active_certs[\"no_transitive_intersection\"] = active_certs.indirectly_referencing.map(lambda x: False if pd.isnull(x) else not x.intersection(active_certs_cert_ids))\n",
- " dct_direct[date] = active_certs.loc[active_certs.no_intersection].shape[0]\n",
- " dct_transitive[date] = active_certs.loc[active_certs.no_transitive_intersection].shape[0]\n",
+ " active_certs[\"no_intersection\"] = active_certs.directly_referencing.map(\n",
+ " lambda x: False if pd.isnull(x) else not x.intersection(active_certs_cert_ids)\n",
+ " )\n",
+ " active_certs[\"no_transitive_intersection\"] = active_certs.indirectly_referencing.map(\n",
+ " lambda x: False if pd.isnull(x) else not x.intersection(active_certs_cert_ids)\n",
+ " )\n",
+ " dct_direct_others[date] = active_certs.loc[\n",
+ " (active_certs.no_intersection)\n",
+ " & (active_certs.category != \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ].shape[0]\n",
+ " dct_transitive_others[date] = active_certs.loc[\n",
+ " (active_certs.no_transitive_intersection)\n",
+ " & (active_certs.category != \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ].shape[0]\n",
+ " dct_direct_smartcards[date] = active_certs.loc[\n",
+ " (active_certs.no_intersection)\n",
+ " & (active_certs.category == \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ].shape[0]\n",
+ " dct_transitive_smartcards[date] = active_certs.loc[\n",
+ " (active_certs.no_transitive_intersection)\n",
+ " & (active_certs.category == \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ].shape[0]\n",
"\n",
- "df_refs_to_archived = pd.concat([pd.Series(dct_direct, name=\"direct reference\"), pd.Series(dct_transitive, name=\"transitive reference\")], axis=1)\n",
+ "df_refs_to_archived = pd.concat(\n",
+ " [\n",
+ " pd.Series(dct_direct_others, name=\"direct reference others\"),\n",
+ " pd.Series(dct_transitive_others, name=\"transitive reference others\"),\n",
+ " pd.Series(dct_direct_smartcards, name=\"direct reference smartcards\"),\n",
+ " pd.Series(dct_transitive_smartcards, name=\"transitive reference smartcards\"),\n",
+ " ],\n",
+ " axis=1,\n",
+ ")\n",
"df_refs_to_archived.index.name = \"date\"\n",
"df_refs_to_archived = df_refs_to_archived.reset_index()\n",
- "df_refs_to_archived_melted = df_refs_to_archived.melt(id_vars=[\"date\"], var_name=\"reference type\", value_name=\"number of certificates\")\n",
+ "df_refs_to_archived_melted = df_refs_to_archived.melt(\n",
+ " id_vars=[\"date\"], var_name=\"reference type\", value_name=\"number of certificates\"\n",
+ ")\n",
"\n",
"g = sns.lineplot(data=df_refs_to_archived_melted, x=\"date\", y=\"number of certificates\", hue=\"reference type\")\n",
- "g.set(title=\"Number of active certificates that reference some archived certificate\", xlabel=\"Time\", ylabel=\"Number of certificates\")\n",
+ "g.set(\n",
+ " title=\"Number of active certificates that reference some archived certificate\",\n",
+ " xlabel=\"Time\",\n",
+ " ylabel=\"Number of certificates\",\n",
+ ")\n",
"plt.savefig(RESULTS_DIR / \"lienplot_active_certs_referencing_archived.pdf\", bbox_inches=\"tight\")\n",
- "plt.show()"
+ "plt.show()\n"
+ ]
+ },
+ {
+ "attachments": {},
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Certificates referencing vulnerable certificates in time"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "date_range = pd.date_range(df_id_rich.not_valid_before.min(), df_id_rich.not_valid_before.max())\n",
+ "vulnerable_cert_ids = set(df_id_rich.loc[df_id_rich.related_cves.notnull()].cert_id.tolist())\n",
+ "dct_direct_others = {}\n",
+ "dct_transitive_others = {}\n",
+ "dct_direct_smartcards = {}\n",
+ "dct_transitive_smartcards = {}\n",
+ "for date in tqdm(date_range):\n",
+ " active_certs = df_id_rich.loc[(date >= df_id_rich.not_valid_before) & (date <= df_id_rich.not_valid_after)].copy()\n",
+ " active_certs[\"directly_references_vulnerable_cert\"] = active_certs.directly_referencing.map(\n",
+ " lambda x: False if pd.isnull(x) else bool(x.intersection(vulnerable_cert_ids))\n",
+ " )\n",
+ " active_certs[\"transitively_references_vulnerable_cert\"] = active_certs.indirectly_referencing.map(\n",
+ " lambda x: False if pd.isnull(x) else bool(x.intersection(vulnerable_cert_ids))\n",
+ " )\n",
+ " dct_direct_others[date] = active_certs.loc[\n",
+ " (active_certs.directly_references_vulnerable_cert)\n",
+ " & (active_certs.category != \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ].shape[0]\n",
+ " dct_transitive_others[date] = active_certs.loc[\n",
+ " (active_certs.transitively_references_vulnerable_cert)\n",
+ " & (active_certs.category != \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ].shape[0]\n",
+ " dct_direct_smartcards[date] = active_certs.loc[\n",
+ " (active_certs.directly_references_vulnerable_cert)\n",
+ " & (active_certs.category == \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ].shape[0]\n",
+ " dct_transitive_smartcards[date] = active_certs.loc[\n",
+ " (active_certs.transitively_references_vulnerable_cert)\n",
+ " & (active_certs.category == \"ICs, Smart Cards and Smart Card-Related Devices and Systems\")\n",
+ " ].shape[0]\n",
+ "\n",
+ "df_references_vuln = pd.concat(\n",
+ " [\n",
+ " pd.Series(dct_direct_others, name=\"direct references others\"),\n",
+ " pd.Series(dct_transitive_others, name=\"transitive references others\"),\n",
+ " pd.Series(dct_direct_smartcards, name=\"direct references smartcards\"),\n",
+ " pd.Series(dct_transitive_smartcards, name=\"transitive references smartcards\"),\n",
+ " ],\n",
+ " axis=1,\n",
+ ")\n",
+ "df_references_vuln.index.name = \"date\"\n",
+ "df_references_vuln = df_references_vuln.reset_index()\n",
+ "df_references_vuln_melted = df_references_vuln.melt(\n",
+ " id_vars=\"date\", var_name=\"reference type\", value_name=\"number of certificates\"\n",
+ ")\n",
+ "\n",
+ "g = sns.lineplot(data=df_references_vuln_melted, x=\"date\", y=\"number of certificates\", hue=\"reference type\")\n",
+ "g.set(\n",
+ " title=\"Number of active certificates that reference some vulnerable certificate\",\n",
+ " xlabel=\"Time\",\n",
+ " ylabel=\"Number of certificates\",\n",
+ ")\n",
+ "plt.savefig(RESULTS_DIR / \"lienplot_active_certs_referencing_vulnerable.pdf\", bbox_inches=\"tight\")\n",
+ "plt.show()\n"
]
},
{
@@ -604,6 +727,7 @@
"\n",
"figure.savefig(str(RESULTS_DIR / \"scheme_references.pdf\"), bbox_inches=\"tight\")\n",
"figure.savefig(str(RESULTS_DIR / \"scheme_references.pgf\"), bbox_inches=\"tight\")\n",
+ "plt.show()\n",
"plt.close(figure)"
]
},
@@ -612,43 +736,6 @@
"cell_type": "markdown",
"metadata": {},
"source": [
- "### Temporal evolution of references\n",
- "\n",
- "Shows plot with relative number of certificates for a given year that reference some other certificate"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "pycharm": {
- "name": "#%%\n"
- }
- },
- "outputs": [],
- "source": [
- "# TODO: Again, this plot is neither shown nor saved as a figure\n",
- "df_temporal = df.loc[df.year_from < 2022].groupby([\"year_from\"])[\"directly_referencing\"].count().reset_index().set_index(\"year_from\")\n",
- "n_issued_certs = df.groupby(\"year_from\").name.count().reset_index().rename(columns={\"name\": \"n_certs\"}).set_index(\"year_from\")\n",
- "df_temporal.directly_referencing = 100 * df_temporal.directly_referencing / n_issued_certs.n_certs\n",
- "\n",
- "line = sns.lineplot(data=df_temporal, x=\"year_from\", y=\"directly_referencing\")\n",
- "line.yaxis.set_major_formatter(mtick.PercentFormatter())"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### Cross references"
- ]
- },
- {
- "attachments": {},
- "cell_type": "markdown",
- "metadata": {},
- "source": [
"## Reference network visualization"
]
},
@@ -820,7 +907,6 @@
"metadata": {},
"outputs": [],
"source": [
- "# TODO: Not sure what this thing does\n",
"nx.draw(view, pos=nx.planar_layout(view), with_labels=True)"
]
},