forked from NetSPI/django.nV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfilefinal
542 lines (452 loc) · 24.8 KB
/
Jenkinsfilefinal
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
pipeline {
agent any
tools{
maven 'Maven'
}
environment {
REGISTRY = 'https://index.docker.io/v1/'
REPOSITORY = 'aatikah'
IMAGE_NAME = 'vul-djangoapp'
DOCKER_CREDENTIALS_ID = 'docker-credential'
DEFECTDOJO_URL = credentials('DEFECTDOJO_URL')
GITLEAKS_ENGAGEMENT_ID = '5'
BANDIT_ENGAGEMENT_ID = '6'
ZAP_ENGAGEMENT_ID = '7'
NIKITO_ENGAGEMENT_ID = '8'
DEFECT_DOJO = 'http://34.71.158.255:8080'
ARCHERYSEC_URL = 'http://34.170.65.15:8000'
ARCHERYSEC_PROJECT_ID = '298f50a8-1e2f-4b03-beb6-392398d125b2'
ARCHERYSEC_USER = '[email protected]'
targetUrl = 'http://35.225.219.50'
}
//DEFECTDOJO_API_KEY = 'DEFECTDOJO_API_KEY'
stages {
stage('Initialize') {
steps {
sh '''
echo "PATH = ${PATH}"
echo "M2_HOME = ${M2_HOME}"
'''
}
}
stage('Check Git Secrets With Gitleaks With Error Continue') {
steps {
script {
//Remove any existing report file
sh 'rm -f gitleaks_report.json'
// Pull the Gitleaks Docker image
sh 'sudo docker pull zricethezav/gitleaks'
// Run Gitleaks in a Docker container and capture the exit code
def gitleaksStatus = sh(script: 'sudo docker run --rm -v /var/lib/jenkins/workspace/vul-django:/repo zricethezav/gitleaks detect --source /repo --report-path /repo/gitleaks_report.json --report-format json', returnStatus: true)
// Display the Gitleaks report
sh 'cat gitleaks_report.json'
// Handle the Gitleaks exit code
if (gitleaksStatus == 1) {
echo 'Leaks found. Review the Gitleaks report for details.'
}
// Use withCredentials to handle the API key securely
// withCredentials([string(credentialsId: 'DEFECTDOJO_API_KEY', variable: 'API_KEY')]) {
// def curlCommand = """
// curl -X POST '${DEFECT_DOJO}/api/v2/import-scan/' \
// -H 'Authorization: Token ${API_KEY}' \
// -H 'accept: application/json' \
// -H 'Content-Type: multipart/form-data' \
// -F 'file=@gitleaks_report.json' \
// -F 'scan_type=Gitleaks Scan' \
// -F 'engagement=${GITLEAKS_ENGAGEMENT_ID}' \
// -F 'product_name=django-pipeline'
// """
//sh curlCommand
// def response = sh(script: curlCommand, returnStdout: true).trim()
//echo "Response from DefectDojo: ${response}"
// }
// Use withCredentials to handle the API key securely
withCredentials([string(credentialsId: 'DEFECTDOJO_API_KEY', variable: 'API_KEY')]) {
def response = sh(
script: """
curl -X POST '${DEFECT_DOJO}/api/v2/import-scan/' \
-H 'Authorization: Token $API_KEY' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'file=@gitleaks_report.json' \
-F 'scan_type=Gitleaks Scan' \
-F 'engagement=${GITLEAKS_ENGAGEMENT_ID}' \
-F 'product_name=django-pipeline'
""",
returnStdout: true
).trim()
echo "Response from DefectDojo: ${response}"
}
}
}
}
//stage('Check Git Secrets With Gitleaks') {
//steps {
// Remove any existing report file
// sh 'rm gitleaks_report.json || true'
// Pull the Gitleaks Docker image
// sh 'sudo docker pull zricethezav/gitleaks'
// Run Gitleaks in a Docker container
//sh 'sudo docker run --rm -v /var/lib/jenkins/workspace/vul-django:/repo zricethezav/gitleaks detect --source /repo --exit-code 1 --report-path /repo/gitleaks_report.json --report-format json'
//sh 'sudo docker run --rm -v /var/lib/jenkins/workspace/vul-django:/repo zricethezav/gitleaks detect --source /repo --report-path /repo/gitleaks_report.json --report-format json'
// Display the Gitleaks report
// sh 'cat gitleaks_report.json'
// }
//}
// stage('Source Composition Analysis'){
// steps{
// script{
// sh 'rm owasp* || true'
// sh 'wget "https://raw.githubusercontent.com/aatikah/django.nV/master/owasp-dependency-check.sh"'
// sh 'chmod +x owasp-dependency-check.sh'
// sh 'bash owasp-dependency-check.sh'
// sh 'cat /var/lib/jenkins/workspace/vul-django/odc-reports/dependency-check-report.xml'
// sh 'cat /var/lib/jenkins/workspace/vul-django/odc-reports/dependency-check-report.json'
//def response = sh(
// script: """
// curl -X POST ${DEFECT_DOJO}/api/v2/import-scan/ \
// -H "Authorization: Token ${DEFECTDOJO_API_KEY}" \
// -H "accept: application/json" \
// -H "Content-Type: multipart/form-data" \
// -F "file=@/var/lib/jenkins/workspace/vul-django/odc-reports/dependency-check-report.json" \
// -F 'scan_type=Dependency Check Scan' \
// -F 'engagement=${ENGAGEMENT_ID}' \
// -F 'product_name=django-pipeline'
// """,
// returnStdout: true
// ).trim()
// echo "Response from DefectDojo: ${response}"
// }
// }
// }
// stage('SAST With Sonarqube') {
//steps {
//withSonarQubeEnv('vul-django') {
// withCredentials([string(credentialsId: 'sonar-token', variable: 'SONAR_TOKEN')]) {
// sh '''
//export SONAR_TOKEN=${SONAR_TOKEN}
// mvn sonar:sonar -Dsonar.token=$SONAR_TOKEN
// '''
// }
//}
// }
//}
//stage('SAST With Sonarqube') {
// steps {
// checkout([
// $class: 'GitSCM',
// branches: [[name: '*/master']],
// userRemoteConfigs: [[url: 'https://github.com/aatikah/django.nV.git']]
// ])
// withSonarQubeEnv('vul-django') {
// withCredentials([string(credentialsId: 'sonar-token', variable: 'SONAR_TOKEN')]) {
// sh '''
// export SONAR_TOKEN=${SONAR_TOKEN}
// mvn sonar:sonar -Dsonar.token=$SONAR_TOKEN
// '''
// }
// }
// }
//}
//stage('SAST with Bandit') {
// steps {
// script {
// Remove any existing Bandit report file
// sh 'rm -f bandit_report.html'
// Ensure Bandit is installed in the Jenkins environment
// sh 'pip install bandit'
// Run Bandit on the code directory
// sh '''
// /home/abuabdillah5444/myenv/bin/bandit -r /var/lib/jenkins/workspace/vul-django -o bandit_report.html -f html
// '''
//sh 'bandit -r /var/lib/jenkins/workspace/vul-django -o bandit_report.html -f html'
// Display the Bandit report
// sh 'cat bandit_report.html'
// Assuming /var/www/html/bandit_reports is served by your web server
// sh 'cp bandit_report.html /var/www/html/bandit_reports/'
// Archive the Bandit report as an artifact
// archiveArtifacts artifacts: 'bandit_report.html', allowEmptyArchive: true
// Publish the report using HTML Publisher Plugin
//publishHTML(target: [
// reportDir: '',
// reportFiles: 'bandit_report.html',
// reportName: 'Bandit Security Report',
// keepAll: true,
// alwaysLinkToLastBuild: true
// ])
//}
// echo "Report available at: http://34.28.86.41/bandit_reports/bandit_report.html"
// }
//}
stage('SAST with Bandit') {
steps {
script {
def banditHtmlStatus = sh(
script: '/home/abuabdillah5444/myenv/bin/bandit -r /var/lib/jenkins/workspace/vul-django -o bandit_report.html -f html',
returnStatus: true
)
def banditJsonStatus = sh(
script: '/home/abuabdillah5444/myenv/bin/bandit -r /var/lib/jenkins/workspace/vul-django -o bandit_report.json -f json',
returnStatus: true
)
if (banditHtmlStatus != 0) {
echo "Bandit found issues in the code. Please review the report at bandit_report.html."
// Optionally, you can fail the build if you want to treat these issues as critical
// error("Bandit scan failed with exit code ${banditStatus}")
}
// Display the Bandit report
sh 'cat bandit_report.html'
sh 'cat bandit_report.json'
// Archive the Bandit report as an artifact
archiveArtifacts artifacts: 'bandit_report.html', allowEmptyArchive: true
// Publish the report using HTML Publisher Plugin
publishHTML(target: [
reportDir: '',
reportFiles: 'bandit_report.html',
reportName: 'Bandit Security Report',
keepAll: true,
alwaysLinkToLastBuild: true
])
// Use withCredentials to handle the API key securely
withCredentials([string(credentialsId: 'DEFECTDOJO_API_KEY', variable: 'API_KEY')]) {
def response = sh(
script: """
curl -X POST '${DEFECT_DOJO}/api/v2/import-scan/' \
-H 'Authorization: Token $API_KEY' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'file=@bandit_report.json' \
-F 'scan_type=Bandit Scan' \
-F 'engagement=${BANDIT_ENGAGEMENT_ID}' \
-F 'product_name=django-pipeline'
""",
returnStdout: true
).trim()
echo "Response from DefectDojo: ${response}"
// withCredentials([string(credentialsId: 'DEFECTDOJO_API_KEY', variable: 'API_KEY')]) {
// def curlCommand = """
// curl -X POST '${DEFECT_DOJO}/api/v2/import-scan/' \
// -H 'Authorization: Token $API_KEY' \
// -H 'accept: application/json' \
// -H 'Content-Type: multipart/form-data' \
// -F 'file=@bandit_report.json' \
// -F 'scan_type=Bandit Scan' \
// -F 'engagement=${BANDIT_ENGAGEMENT_ID}' \
// -F 'product_name=django-pipeline'
// """
//sh curlCommand
//def response = sh(script: curlCommand, returnStdout: true).trim()
//echo "Response from DefectDojo: ${response}"
}
}
}
}
}
stage('Build Docker Image') {
steps {
script {
dockerImage = docker.build("${REPOSITORY}/${IMAGE_NAME}")
}
}
}
stage('Push Docker Image') {
steps {
script {
docker.withRegistry("${REGISTRY}", DOCKER_CREDENTIALS_ID) {
dockerImage.push('v1')
}
}
}
}
stage('Deploy to GCP VM') {
steps {
sshagent(['tomcatkey']) {
sh '''
ssh -v -o StrictHostKeyChecking=no [email protected] "sudo docker pull aatikah/vul-djangoapp:v1 && sudo docker run -d -p 8000:8000 aatikah/vul-djangoapp:v1"
'''
}
}
}
// stage('DAST With OWASP ZAP Scan') {
// steps {
// script {
// Allow ZAP to start up
// sleep 10
//sh 'sudo docker run -t zaproxy/zap-stable zap-baseline.py -t http://34.135.208.39 -J zap_report.json'
// sh '''
// sudo chmod -R 777 /var/lib/jenkins/workspace/vul-django
// sudo docker run -v /var/lib/jenkins/workspace/vul-django:/zap/wrk -t zaproxy/zap-stable zap-baseline.py -t http://34.123.8.118 -J zap_report.json || true
// echo "Sleeping for 20 seconds"
// sleep 20
// '''
// sh 'cat zap_report.json || true'
// sh '''
// sudo chmod -R 777 /var/lib/jenkins/workspace/vul-django
// sudo docker run -v /var/lib/jenkins/workspace/vul-django:/zap/wrk -t zaproxy/zap-stable zap-baseline.py -t http://34.123.8.118 -r zap_report.html ||true
// echo "Sleeping for 10 seconds"
// sleep 20
// '''
// sh 'cat zap_report.html || true'
// Copy the ZAP JSON report out of the Docker container
// sh 'sudo docker cp zap:/zap/zap_report.json ./zap_report.json'
// Copy the ZAP HTML report out of the Docker container
// sh 'sudo docker cp zap:/zap/zap_report.html ./zap_report.html'
// Use the Jenkins HTML Publisher Plugin to display the report
// publishHTML(target: [
// reportDir: '.',
// reportFiles: 'zap_report.html',
// reportName: 'OWASP ZAP Report'
// ])
// Securely use DefectDojo API Key within a credentials block
//withCredentials([string(credentialsId: 'DEFECTDOJO_API_KEY', variable: 'API_KEY')]) {
// def response = sh(
// script: """
// curl -X POST '${DEFECT_DOJO}/api/v2/import-scan/' \
//-H 'Authorization: Token $API_KEY' \
// -H 'accept: application/json' \
// -H 'Content-Type: multipart/form-data' \
// -F 'file=@zap_report.json' \
// -F 'scan_type=ZAP Scan' \
// -F 'engagement=${ZAP_ENGAGEMENT_ID}' \
// -F 'product_name=django-pipeline'
// """,
// returnStdout: true
//).trim()
//echo "Response from DefectDojo: ${response}"
// withCredentials([string(credentialsId: 'DEFECTDOJO_API_KEY', variable: 'API_KEY')]) {
// def curlCommand = """
// curl -X POST '${DEFECT_DOJO}/api/v2/import-scan/' \
// -H 'Authorization: Token $API_KEY' \
// -H 'accept: application/json' \
// -H 'Content-Type: multipart/form-data' \
// -F 'file=@zap_report.json' \
// -F 'scan_type=ZAP Scan' \
// -F 'engagement=${ZAP_ENGAGEMENT_ID}' \
// -F 'product_name=django-pipeline'
//"""
//sh curlCommand
//def response = sh(script: curlCommand, returnStdout: true).trim()
//echo "Response from DefectDojo: ${response}"
// }
// Send ZAP scan results to ArcherySec
//withCredentials([string(credentialsId: 'archerysec', variable: 'ARCHERYSEC_API_KEY')]) {
// def archerysecCommand = """
// curl -X POST '${ARCHERYSEC_URL}/api/v1/uploadscan/' \
// -H 'Authorization: ${ARCHERYSEC_API_KEY}' \
// -H 'Content-Type: multipart/form-data' \
// -F 'scanner=zap_scan' \
// -F 'file=@zap_report.json' \
// -F 'scan_url=http://34.132.231.218' \
// -F 'project_id=${ARCHERYSEC_PROJECT_ID}' \
// -F 'username=${ARCHERYSEC_USER}'
// """
//sh archerysecCommand
//}
// sh '''
// rm -rf archerysec_env
// python3 -m venv archerysec_env
// . archerysec_env/bin/activate
// pip install archerysec-cli
// mkdir -p /tmp/archerysec-scans-report
// archerysec-cli -h http://34.170.65.15:8000 -t cHnQc3bpwLV3sMfiRAj2jLr42O_fGkyvYmt11KY7GD8Tjv5CbYWlG0Dqps49tDcq --cicd_id=032ce5e4-0d26-4a28-af1d-c53d512e644b --project=298f50a8-1e2f-4b03-beb6-392398d125b2 --zap-base-line-scan --report_path=/tmp/archerysec-scans-report/
// '''
//sh 'archerysec-cli -h http://34.170.65.15:8000 -t cHnQc3bpwLV3sMfiRAj2jLr42O_fGkyvYmt11KY7GD8Tjv5CbYWlG0Dqps49tDcq --cicd_id=032ce5e4-0d26-4a28-af1d-c53d512e644b --project=298f50a8-1e2f-4b03-beb6-392398d125b2 --zap-base-line-scan --report_path=/tmp/archerysec-scans-report/ --verbose || true'
// def zapReport = readFile 'zap_report.html'
// def response = httpRequest(
// url: 'http://34.170.65.15:8000/api/v1/zap-scan/',
// requestBody: zapReport,
// httpMode: 'POST',
// contentType: 'APPLICATION_JSON',
// customHeaders: [
// [name: 'Authorization', value: 'cHnQc3bpwLV3sMfiRAj2jLr42O_fGkyvYmt11KY7GD8Tjv5CbYWlG0Dqps49tDcq']
// ]
// )
// if (response.status == 201) {
// println 'ZAP report exported to ArcherySec successfully'
// } else {
// error "Failed to export ZAP report to ArcherySec: ${response.status} - ${response.content}"
// }
//}
// }
stage('DAST With NIKITO Scan') {
steps {
script {
// Run the Nikto scan command
//sh '/home/abuabdillah5444/nikto/program/nikto.pl -h ${targetUrl} -o nikto_scan_results.html || true'
sh "/home/abuabdillah5444/nikto/program/nikto.pl -h '${targetUrl}' -o nikto_scan_results.html || true"
sh 'cat nikto_scan_results.html'
// sh '/home/abuabdillah5444/nikto/program/nikto.pl -h ${targetUrl} -o nikto_scan_results.json || true'
sh "/home/abuabdillah5444/nikto/program/nikto.pl -h '${targetUrl}' -o nikto_scan_results.json || true"
sh 'cat nikto_scan_results.json'
// Use the Jenkins HTML Publisher Plugin to display the report
publishHTML(target: [
reportDir: '.',
reportFiles: 'nikto_scan_results.html',
reportName: 'NIKITO Scan Report',
keepAll: true,
alwaysLinkToLastBuild: true
])
// Use withCredentials to handle the API key securely
withCredentials([string(credentialsId: 'DEFECTDOJO_API_KEY', variable: 'API_KEY')]) {
def response = sh(
script: """
curl -X POST '${DEFECT_DOJO}/api/v2/import-scan/' \
-H 'Authorization: Token $API_KEY' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'file=@nikto_scan_results.json' \
-F 'scan_type=Nikto Scan' \
-F 'engagement=${NIKITO_ENGAGEMENT_ID}' \
-F 'product_name=django-pipeline'
""",
returnStdout: true
).trim()
echo "Response from DefectDojo: ${response}"
}
}
}
}
// stage ('DAST') {
// steps {
// sshagent(['ZAPKEY']) {
// sh 'ssh -o StrictHostKeyChecking=no [email protected] "sudo docker rm -f zap-scan-container || true"'
// sh 'ssh -o StrictHostKeyChecking=no [email protected] "sudo docker run --name zap-scan-container -t zaproxy/zap-stable zap-baseline.py -t http://35.225.219.50 -j zap-report.json"'
// sh 'ssh -o StrictHostKeyChecking=no [email protected] "sudo docker cp zap-scan-container:/zap/zap-report.json /home/abuabdillah5444/zap-report.json"'
// sh 'scp -o StrictHostKeyChecking=no [email protected]:/home/abuabdillah5444/zap-report.json /var/lib/jenkins/workspace/vul-django/zap-report.json'
// sh 'cat zap-report.json || true'
// sh 'ssh -o StrictHostKeyChecking=no [email protected] "sudo docker run --name zap-scan-container -t zaproxy/zap-stable zap-baseline.py -t http://35.225.219.50 -r zap-report.html"'
// sh 'ssh -o StrictHostKeyChecking=no [email protected] "sudo docker cp zap-scan-container:/zap/zap-report.html /home/abuabdillah5444/zap-report.html"'
// sh 'scp -o StrictHostKeyChecking=no [email protected]:/home/abuabdillah5444/zap-report.html /var/lib/jenkins/workspace/vul-django/zap-report.html'
// sh 'cat zap-report.html || true'
// publishHTML(target: [
// reportDir: '.',
// reportFiles: 'zap-report.html',
// reportName: 'ZAP Scan Report',
// keepAll: true,
// alwaysLinkToLastBuild: true
// ])
// script {
// def zapReport = readFile 'zap-report.json'
// def defectdojoUrl = 'http://34.71.158.255:8080/api/v2/import-scan'
// def defectdojoApiKey = 'f830c3e36636fa2224d00c80d49ecbac37254d96'
// def defectdojoProduct = 'django-pipeline'
// def response = httpRequest(
// url: defectdojoUrl,
// requestBody: zapReport,
// httpMode: 'POST',
// customHeaders: [
// [name: 'Authorization', value: "Token ${defectdojoApiKey}"],
// [name: 'product', value: defectdojoProduct]
// ]
// )
// if (response.status == 201) {
// println 'ZAP report exported to DefectDojo successfully'
// } else {
// error "Failed to export ZAP report to DefectDojo: ${response.status} - ${response.content}"
// }
// }
// }
// }
// }
}
}