File size: 6,527 Bytes
a3abb69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8de6eb89-7d92-4e9d-ab20-8c71ed062072",
   "metadata": {},
   "outputs": [],
   "source": [
    "import keras\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Conv2D, Flatten, MaxPooling2D\n",
    "from keras.datasets import mnist\n",
    "from keras.preprocessing.image import ImageDataGenerator\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1caec746-026a-4649-952d-98ff1ac69e97",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Intentionally including deprecated library\n",
    "import imp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e7fb59e-6d40-4be1-a51e-0162ddd02c80",
   "metadata": {},
   "outputs": [],
   "source": [
    " # added this vulnerable library (safety might use updated safe library version for this)\n",
    "import urllib3 \n",
    "print(urllib3.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf17cb05-cc6f-4ef0-a27d-fb6a5af33eb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "#using vulnerable library forcefully for safety to detect\n",
    "!pip install urllib3==1.24.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca7abfef-f88c-4766-8db4-b1f0909c8e83",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install scikit-learn==0.19.0\n",
    "import sklearn\n",
    "print(sklearn.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a78b2239-8abd-44fb-b337-9c9f0830ecaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install numpy==1.16.0\n",
    "import numpy as np\n",
    "print(np.__version__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3fb3af1-200d-4088-b2a8-5fa445e5d0ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install tensorflow==1.15.5\n",
    "import tensorflow as tf\n",
    "print(tf.__version__)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95b8762f-bd02-4f4d-9fa2-e511a2b4a326",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A real example of a library with a non-permissive license\n",
    "import gmpy2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88872ca5-c939-4b25-b37e-4351fd6ef336",
   "metadata": {},
   "outputs": [],
   "source": [
    "# GNU Octave, an interpreted high-level programming language for numerical computations\n",
    "# Licensed under GPL\n",
    "import oct2py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e844eab-9caa-467e-b1c4-2c7aac5a31a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Mock secret keys\n",
    "SECRET_KEY = \"ABCDEFG\"\n",
    "\n",
    "aws_secret_key_1 = \"A3TABCDEFGH1234567890\" \n",
    "\n",
    "AWS_SECRET_ACCESS_KEY_0 = \"AKIAIOSFODNN7EXAMPLE\"\n",
    "\n",
    "AWS_SECRET_ACCESS_KEY = \"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "901dbdc1-76b2-47d3-9428-2ddb4c043653",
   "metadata": {},
   "outputs": [],
   "source": [
    "AWS_ACCOUNT_ID = \"1234-5678-9012\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "631f40be-470b-4bf1-b645-a0b8429f0dfb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# PII Information (Just for demonstration, do not use real PII)\n",
    "user_data = {\n",
    "    'name': 'John Doe',\n",
    "    'email': 'johndoe@example.com',\n",
    "    'address': '123 Main St, Anytown, USA'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2056314b-486e-4b2f-923a-4194c8a955fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
    "\n",
    "# normalize to range 0-1\n",
    "x_train = x_train / 255.0\n",
    "x_test = x_test / 255.0\n",
    "\n",
    "# reshape\n",
    "x_train = x_train.reshape(-1, 28, 28, 1)\n",
    "x_test = x_test.reshape(-1, 28, 28, 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9766dd8-260d-4184-ac37-9e768f780d8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Define the model\n",
    "\n",
    "# %%\n",
    "model = Sequential()\n",
    "model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))\n",
    "model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "model.add(Flatten())\n",
    "model.add(Dense(128, activation='relu'))\n",
    "model.add(Dense(10, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2e6cd12-349b-4088-a189-3037da3191ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ## Compile the model\n",
    "\n",
    "# %%\n",
    "model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "156520be-06f9-45a6-8c75-5fcfa567d3de",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ## Train the model\n",
    "\n",
    "# %%\n",
    "history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bd92538-5655-444c-aa79-92c614f890d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_loss, test_accuracy = model.evaluate(x_test, y_test)\n",
    "print(f'Test loss: {test_loss}, Test accuracy: {test_accuracy}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}