aboutsummaryrefslogtreecommitdiffhomepage
path: root/notebooks/examples/fips.ipynb
blob: 6d9edee4e4436658e5d98d83faa2a4e02f994555 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# FIPS Dataset class\n",
    "\n",
    "This notebook illustrates basic functionality with the `FIPSDataset` class that holds FIPS 140 dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sec_certs.dataset.fips import FIPSDataset\n",
    "from sec_certs.sample import FIPSCertificate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get fresh dataset snapshot from mirror"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dset: FIPSDataset = FIPSDataset.from_web_latest()\n",
    "print(len(dset))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Do some basic dataset serialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dump dataset into json and load it back\n",
    "dset.to_json(\"./fips_dataset.json\")\n",
    "new_dset = FIPSDataset.from_json(\"./fips_dataset.json\")\n",
    "assert dset == new_dset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple dataset manipulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get certificates from a single manufacturer\n",
    "cisco_certs = [cert for cert in dset if \"Cisco\" in cert.manufacturer]\n",
    "\n",
    "# Get certificates with some CVE\n",
    "vulnerable_certs = [cert for cert in dset if cert.heuristics.related_cves]\n",
    "\n",
    "# Show CVE ids of some vulnerable certificate\n",
    "print(f\"{vulnerable_certs[0].heuristics.related_cves=}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dissect single certificate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Select a certificate and print some attributes\n",
    "cert: FIPSCertificate = dset[\"542cacae1d41132a\"]\n",
    "\n",
    "print(f\"{cert.web_data.module_name=}\")\n",
    "print(f\"{cert.heuristics.cpe_matches=}\")\n",
    "print(f\"{cert.web_data.level=}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Serialize single certificate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "cert.to_json(\"./cert.json\")\n",
    "new_cert: FIPSCertificate = FIPSCertificate.from_json(\"./cert.json\")\n",
    "assert new_cert == cert"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create new dataset and fully process it\n",
    "\n",
    "*Warning*: It's not good idea to run this from notebook. It may take several hours to finnish. We recommend using `from_web_latest()` or turning this into a Python script."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dset = FIPSDataset()\n",
    "dset.get_certs_from_web()\n",
    "dset.process_auxillary_datasets()\n",
    "dset.download_all_artifacts()\n",
    "dset.convert_all_pdfs()\n",
    "dset.analyze_certificates()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "6386d1612879d92d026c363e7667e428bc38d86c5a080d58c3d70e7cd43df67d"
  },
  "kernelspec": {
   "display_name": "Python 3.8.1 ('certsvenv': venv)",
   "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.8.1"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}