-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_security.py
87 lines (72 loc) · 2.5 KB
/
test_security.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
import requests
import json
from Demos.getfilever import n
BASE_URL = 'https://petstore.swagger.io/v2'
def test_sql_injection():
url = f'{BASE_URL}/pet'
body = {
"id": 103,
"category": {
"id": 1,
"name": "dogs"
},
"name": "jimmyPet'; DROP TABLE pets; --",
"photoUrls": [
"./PetDogs.jpg"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
response = requests.post(url, headers=headers, data=json.dumps(body))
print("\n====================Start JSON File====================")
print(json.dumps(response.json(), indent=4))
print("=====================End JSON File=====================")
# بررسی دقیقتر محتوای پاسخ سرور
assert response.status_code == 200, f"Request failed with status code {response.status_code}"
response_text = response.text.lower()
sql_errors = ["syntax error", "unrecognized token", "near", "unterminated", "error in your sql syntax"]
assert not any(error in response_text for error in sql_errors), "Possible SQL Injection vulnerability detected"
def test_xss():
url = f'{BASE_URL}/pet'
body = {
"id": 103,
"category": {
"id": 1,
"name": "dogs"
},
"name": "<script>alert('XSS');</script>",
"photoUrls": [
"./PetDogs.jpg"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
response = requests.post(url, headers=headers, data=json.dumps(body))
print("\n====================Start JSON File====================")
print(json.dumps(response.json(), indent=4))
print("=====================End JSON File=====================")
assert response.status_code == 200, f"Request failed with status code {response.status_code}"
response_text = response.text.lower() # تبدیل پاسخ به حروف کوچک برای مقایسه آسانتر
# بررسی وجود اسکریپت در پاسخ سرور
assert 'alert(' in response_text, "Possible XSS vulnerability detected"
if __name__ == "__main__":
test_sql_injection()
test_xss()