Repository navigation
Expand file tree
/
Copy pathremediation.py
More file actions
66 lines (62 loc) · 2.78 KB
/
Copy pathremediation.py
File metadata and controls
66 lines (62 loc) · 2.78 KB
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
"""
Remediation knowledge base for SQL injection and related fixes. Cross-platform.
"""
from typing import Dict, Any, List
class RemediationKnowledgeBase:
def __init__(self) -> None:
self.patterns: Dict[str, Dict[str, Dict[str, Any]]] = {
"SQL_INJECTION": {
"java": {
"solution": "Use PreparedStatement",
"example": 'String sql = "SELECT * FROM users WHERE id = ?";',
"libraries": ["java.sql.PreparedStatement"],
"extra": "stmt.setString(1, userInput);",
},
"python": {
"solution": "Use parameterized queries",
"example": 'cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))',
"libraries": ["psycopg2", "sqlite3", "SQLAlchemy", "mysql.connector"],
"extra": "Never use % or .format() on the query string; pass params as second argument.",
},
"php": {
"solution": "Use PDO prepared statements",
"example": '$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute([$id]);',
"libraries": ["PDO", "mysqli"],
},
"csharp": {
"solution": "Use parameterized SqlCommand",
"example": 'cmd.CommandText = "SELECT * FROM users WHERE id = @id"; cmd.Parameters.AddWithValue("@id", id);',
"libraries": ["System.Data.SqlClient"],
},
},
"XSS": {
"python": {
"solution": "Escape output and use CSP",
"example": "from markupsafe import escape; escape(user_input)",
"libraries": ["markupsafe", "bleach"],
},
"java": {
"solution": "Use OWASP Java Encoder",
"example": "Encoder.forHtml(userInput)",
"libraries": ["org.owasp.encoder"],
},
},
"COMMAND_INJECTION": {
"python": {
"solution": "Use subprocess with list args, never shell=True with user input",
"example": "subprocess.run([\"ls\", \"-la\"], capture_output=True)",
"libraries": ["subprocess"],
},
},
}
def get_remediation(
self, vulnerability_type: str, language: str
) -> Dict[str, Any]:
return (
self.patterns.get(vulnerability_type, {})
.get(language, {})
.copy()
or {}
)
def get_languages_for_type(self, vulnerability_type: str) -> List[str]:
return list(self.patterns.get(vulnerability_type, {}).keys())