Skip to content

Commit c125875

Browse files
committed
Added ZAP Sessions
Added Automation with ZAP Sessions to the Mini Workshop
1 parent 0e7b74b commit c125875

7 files changed

+610
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## ZAP Sessions\n",
8+
"ZAP allows the user to store a session for a crawled application, reload that into context and perform scan operations on the session. However, there are few things to remember here.\n",
9+
"1. You can only use a session file once. Once the session is used for scan operations, it is rendered \"tainted\" which means that it has scan results, etc. \n",
10+
"2. So, in order to use a session file to perform \"parameterization\", its advisable to store the untainted version of the session file in a location, copy it over to another location and then load the copied session file in ZAP for scanning, etc. \n",
11+
"\n",
12+
"Here we have a session file called `api.session` in the `orig_session` directory. We are going to copy the `api.session` over to the `temp_session` directory and perform ZAP Scanning Operations on it, programmatically. We will then use the copied session file at ZAP and perform, scanning, etc. "
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"metadata": {},
18+
"source": [
19+
"### Copying the session file to `temp_session` folder"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 15,
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
"File has been copied successfully\n"
32+
]
33+
}
34+
],
35+
"source": [
36+
"import shutil\n",
37+
"import os\n",
38+
"import glob\n",
39+
"\n",
40+
"src = 'orig_session/'\n",
41+
"dst = 'temp_session/'\n",
42+
"for filename in glob.glob(os.path.join(src, '*.*')):\n",
43+
" shutil.copy(filename, dst)\n",
44+
"## check if file exists in temp session directory\n",
45+
"if os.path.isfile('temp_session/api.session'):\n",
46+
" print(\"File has been copied successfully\")\n",
47+
"else:\n",
48+
" print(\"File has not been found in temp session directory\")"
49+
]
50+
},
51+
{
52+
"cell_type": "markdown",
53+
"metadata": {},
54+
"source": [
55+
"### Start ZAP and initialize API"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 2,
61+
"metadata": {},
62+
"outputs": [],
63+
"source": [
64+
"# we will be using python's subprocess to start ZAP in GUI and headless modes\n",
65+
"\n",
66+
"import subprocess\n",
67+
"import os\n",
68+
"from IPython.display import display\n",
69+
"\n",
70+
"#GUI ZAP\n",
71+
"base_path = '/Applications/OWASP_ZAP.app/Contents/Java/'\n",
72+
"gui_command = base_path + 'zap.sh -config api.disablekey=true -port 8090'\n",
73+
"# you can use the config param to specify set specific configurations you need when you launch the CLI.\n",
74+
"# In this case, I am (actually don't need to) starting ZAP with the API Key disabled and listening port 8090\n",
75+
"\n",
76+
"headless_command = base_path + 'zap.sh -daemon -config api.disablekey=true -port 8090'\n",
77+
"#by specifying 'daemon' in the CLI, ZAP starts in Headless mode\n",
78+
"\n",
79+
"zap_process = subprocess.Popen(gui_command.split(' '), stdout = open(os.devnull, 'w'))"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"metadata": {},
85+
"source": [
86+
"### Initialize ZAP API"
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"execution_count": 16,
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"from zapv2 import ZAPv2 as ZAP #import ZAP library\n",
96+
"import time\n",
97+
"\n",
98+
"zap = ZAP(proxies = {'http': 'http://localhost:8090', 'https': 'http://localhost:8090'})\n",
99+
"#setting the local ZAP instance that is open on your local system"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"### Load Session"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": 17,
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"name": "stdout",
116+
"output_type": "stream",
117+
"text": [
118+
"/Users/abhaybhargav/Documents/Code/Python/zap_mini_workshop/temp_session/api.session\n"
119+
]
120+
},
121+
{
122+
"data": {
123+
"text/plain": [
124+
"'OK'"
125+
]
126+
},
127+
"execution_count": 17,
128+
"metadata": {},
129+
"output_type": "execute_result"
130+
}
131+
],
132+
"source": [
133+
"session_file = os.path.join(os.getcwd(), 'temp_session/api.session')\n",
134+
"print(session_file)\n",
135+
"zap.core.load_session(session_file)"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"### Run Active Scan on Host"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": 18,
148+
"metadata": {},
149+
"outputs": [
150+
{
151+
"name": "stdout",
152+
"output_type": "stream",
153+
"text": [
154+
"active scan id: 0\n",
155+
"Current Status of ZAP Active Scan: 0%\n",
156+
"Current Status of ZAP Active Scan: 89%\n",
157+
"Current Status of ZAP Active Scan: 99%\n"
158+
]
159+
}
160+
],
161+
"source": [
162+
"# using ZAP's ascan object to start scanning, with the \"Light\" Policy. If you don't specify the policy\n",
163+
"# ZAP Automatically uses the \"Default\" policy\n",
164+
"\n",
165+
"target_url = 'http://localhost:5050/'\n",
166+
"active_scan_id = zap.ascan.scan(target_url, scanpolicyname='Light')\n",
167+
"\n",
168+
"print(\"active scan id: {0}\".format(active_scan_id))\n",
169+
"\n",
170+
"#now we can start monitoring the spider's status\n",
171+
"while int(zap.ascan.status(active_scan_id)) < 100:\n",
172+
" print(\"Current Status of ZAP Active Scan: {0}%\".format(zap.ascan.status(active_scan_id)))\n",
173+
" time.sleep(10)"
174+
]
175+
},
176+
{
177+
"cell_type": "markdown",
178+
"metadata": {},
179+
"source": [
180+
"### Shutdown ZAP"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": 20,
186+
"metadata": {},
187+
"outputs": [
188+
{
189+
"data": {
190+
"text/plain": [
191+
"'OK'"
192+
]
193+
},
194+
"execution_count": 20,
195+
"metadata": {},
196+
"output_type": "execute_result"
197+
}
198+
],
199+
"source": [
200+
"zap.core.shutdown()"
201+
]
202+
},
203+
{
204+
"cell_type": "markdown",
205+
"metadata": {},
206+
"source": [
207+
"### Delete \"Tainted\" Session files in `temp_session`"
208+
]
209+
},
210+
{
211+
"cell_type": "code",
212+
"execution_count": 21,
213+
"metadata": {},
214+
"outputs": [],
215+
"source": [
216+
"temp_dir = 'temp_session'\n",
217+
"for filename in glob.glob(os.path.join(temp_dir, '*.*')):\n",
218+
" os.remove(filename)"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": null,
224+
"metadata": {},
225+
"outputs": [],
226+
"source": []
227+
}
228+
],
229+
"metadata": {
230+
"kernelspec": {
231+
"display_name": "Python 3",
232+
"language": "python",
233+
"name": "python3"
234+
},
235+
"language_info": {
236+
"codemirror_mode": {
237+
"name": "ipython",
238+
"version": 3
239+
},
240+
"file_extension": ".py",
241+
"mimetype": "text/x-python",
242+
"name": "python",
243+
"nbconvert_exporter": "python",
244+
"pygments_lexer": "ipython3",
245+
"version": "3.6.1"
246+
}
247+
},
248+
"nbformat": 4,
249+
"nbformat_minor": 2
250+
}

ZAP 2.7.0 Mini Workshop.ipynb

+14-7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@
111111
"Let's start with running the spider against intentionally vulnerable site `demo.testfire.net`"
112112
]
113113
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 2,
117+
"metadata": {},
118+
"outputs": [],
119+
"source": [
120+
"from zapv2 import ZAPv2 as ZAP #import ZAP library\n",
121+
"import time\n",
122+
"\n",
123+
"zap = ZAP(proxies = {'http': 'http://localhost:8090', 'https': 'http://localhost:8090'})\n",
124+
"#setting the local ZAP instance that is open on your local system"
125+
]
126+
},
114127
{
115128
"cell_type": "code",
116129
"execution_count": 2,
@@ -131,12 +144,6 @@
131144
}
132145
],
133146
"source": [
134-
"from zapv2 import ZAPv2 as ZAP #import ZAP library\n",
135-
"import time\n",
136-
"\n",
137-
"zap = ZAP(proxies = {'http': 'http://localhost:8090', 'https': 'http://localhost:8090'})\n",
138-
"#setting the local ZAP instance that is open on your local system\n",
139-
"\n",
140147
"target_site = 'http://demo.testfire.net'\n",
141148
"\n",
142149
"zap.urlopen(target_site)\n",
@@ -639,7 +646,7 @@
639646
},
640647
{
641648
"cell_type": "code",
642-
"execution_count": 15,
649+
"execution_count": 7,
643650
"metadata": {},
644651
"outputs": [
645652
{

0 commit comments

Comments
 (0)