-
Notifications
You must be signed in to change notification settings - Fork 3
/
SN_updated.py
188 lines (138 loc) · 5.49 KB
/
SN_updated.py
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
##########################3
# Encryption algorithms should be used with secure mode and padding scheme
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes
# Example for a symmetric cipher: AES
aes = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend()) # Noncompliant
# Example for a asymmetric cipher: RSA
ciphertext = public_key.encrypt(
message,
padding.PKCS1v15() # Noncompliant
)
plaintext = private_key.decrypt(
ciphertext,
padding.PKCS1v15() # Noncompliant
)
##################
# Dynamic code execution should not be vulnerable to injection attacks
from flask import request
@app.route('/')
def index():
module = request.args.get("module")
exec("import urllib%s as urllib" % module) # Noncompliant
#################
# HTTP request redirections should not be open to forging attacks
from flask import request, redirect
@app.route('move')
def move():
url = request.args["next"]
return redirect(url) # Noncompliant
from django.http import HttpResponseRedirect
def move(request):
url = request.GET.get("next", "/")
return HttpResponseRedirect(url) # Noncompliant
#######################
# Deserialization should not be vulnerable to injection attacks
from flask import request
import pickle
import yaml
@app.route('/pickle')
def pickle_loads():
file = request.files['pickle']
pickle.load(file) # Noncompliant; Never use pickle module to deserialize user inputs
@app.route('/yaml')
def yaml_load():
data = request.GET.get("data")
yaml.load(data, Loader=yaml.Loader) # Noncompliant; Avoid using yaml.load with unsafe yaml.Loader
############################
# Cryptographic key generation should be based on strong parameters
from cryptography.hazmat.primitives.asymmetric import rsa, ec, dsa
dsa.generate_private_key(key_size=1024, backend=backend) # Noncompliant
ec.generate_private_key(curve=ec.SECT163R2, backend=backend) # Noncompliant
#############################
# Database queries should not be vulnerable to injection attacks
from flask import request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import text
from database.users import User
@app.route('hello')
def hello():
id = request.args.get("id")
stmt = text("SELECT * FROM users where id=%s" % id) # Query is constructed based on user inputs
query = SQLAlchemy().session.query(User).from_statement(stmt) # Noncompliant
user = query.one()
return "Hello %s" % user.username
from django.http import HttpResponse
from django.db import connection
def hello(request):
id = request.GET.get("id", "")
cursor = connection.cursor()
cursor.execute("SELECT username FROM auth_user WHERE id=%s" % id) # Noncompliant; Query is constructed based on user inputs
row = cursor.fetchone()
return HttpResponse("Hello %s" % row[0])
###################################
# Databases should be password-protected
from mysql.connector import connection
connection.MySQLConnection(host='localhost', user='guardrails', password='') # Noncompliant
####################################
# XPath expressions should not be vulnerable to injection attacks
from flask import request
import xml.etree.ElementTree as ET
tree = ET.parse('users.xml')
root = tree.getroot()
@app.route('/user')
def user_location():
username = request.args['username']
query = "./users/user/[@name='"+username+"']/location"
elmts = root.findall(query) # Noncompliant
return 'Location %s' % list(elmts)
###################################
# I/O function calls should not be vulnerable to path injection attacks
from flask import request, send_file
@app.route('/download')
def download():
file = request.args['file']
return send_file("static/%s" % file, as_attachment=True) # Noncompliant
###################################
# LDAP queries should not be vulnerable to injection attacks
from flask import request
import ldap
@app.route("/user")
def user():
dn = request.args['dn']
username = request.args['username']
search_filter = "(&(objectClass=*)(uid="+username+"))"
ldap_connection = ldap.initialize("ldap://127.0.0.1:389")
user = ldap_connection.search_s(dn, ldap.SCOPE_SUBTREE, search_filter) # Noncompliant
return user[0]
######################################
# OS commands should not be vulnerable to command injection attacks
from flask import request
import os
@app.route('/ping')
def ping():
address = request.args.get("address")
cmd = "ping -c 1 %s" % address
os.popen(cmd) # Noncompliant
from flask import request
import subprocess
@app.route('/ping')
def ping():
address = request.args.get("address")
cmd = "ping -c 1 %s" % address
subprocess.Popen(cmd, shell=True) # Noncompliant; using shell=true is unsafe
#########################################
# HTTP response headers should not be vulnerable to injection attacks
from flask import Response, request
from werkzeug.datastructures import Headers
@app.route('/route')
def route():
content_type = request.args["Content-Type"]
response = Response()
headers = Headers()
headers.add("Content-Type", content_type) # Noncompliant
response.headers = headers
return response