-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_simulation.py
More file actions
205 lines (161 loc) · 7.72 KB
/
data_simulation.py
File metadata and controls
205 lines (161 loc) · 7.72 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
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
import pandas as pd
import datetime
import random
from random import getrandbits
from ipaddress import IPv4Address
import constants
import uuid
import time
import numpy as np
import geolocation
NUMBER_OF_USERS = 100
ITERATIONS = 50000
usernames = None
currentTime = None
index = 0
def generate_random_ipv4():
bits = getrandbits(32) # generates an integer with 32 random bits
addr = IPv4Address(bits) # instances an IPv4Address object from those bits
return str(addr)
ipAddressesForUsers = {}
geoLocationForIP = {}
index = 0
# Generate successful login
def generate_successful_login(df, given_time=None, username=None, context_id=None, ip=None, is_suspicious=False):
global currentTime, usernames, ipAddressesForUsers, geoLocationForIP, index
if username is None:
username = usernames[random.randrange(NUMBER_OF_USERS)]
event_time = given_time
if event_time is None:
currentTime = currentTime + datetime.timedelta(seconds=random.randrange(3000))
event_time = currentTime
if context_id is None:
context_id = uuid.uuid4()
if ip is None:
ip = ipAddressesForUsers[username]
geoloc = geoLocationForIP[ip]
df.at[index, constants.HEADER_USERNAME] = username
df.at[index + 1, constants.HEADER_USERNAME] = username
df.at[index, constants.HEADER_CONTEXTID] = context_id
df.at[index + 1, constants.HEADER_CONTEXTID] = context_id
df.at[index, constants.HEADER_EVENTTYPE] = constants.EVENTYPE_STEP
df.at[index + 1, constants.HEADER_EVENTTYPE] = constants.EVENTYPE_OVERALL
df.at[index, constants.HEADER_TIMESTAMP] = str(int(time.mktime(event_time.timetuple()) * 1e3))
currentTime = currentTime + datetime.timedelta(seconds=random.randrange(3))
event_time = currentTime
df.at[index + 1, constants.HEADER_TIMESTAMP] = str(int(time.mktime(event_time.timetuple()) * 1e3))
df.at[index, constants.HEADER_REMOTEIP] = ip
df.at[index + 1, constants.HEADER_REMOTEIP] = ip
df.at[index, constants.HEADER_AUTHENTICATIONSUCCESS] = 0
df.at[index + 1, constants.HEADER_AUTHENTICATIONSUCCESS] = 1
if is_suspicious:
df.at[index, constants.HEADER_SUSPICIOUS_LOGIN] = 1
df.at[index + 1, constants.HEADER_SUSPICIOUS_LOGIN] = 1
else:
df.at[index, constants.HEADER_SUSPICIOUS_LOGIN] = 0
df.at[index + 1, constants.HEADER_SUSPICIOUS_LOGIN] = 0
df.at[index, constants.HEADER_LATITUDE] = geoloc.latitude
df.at[index, constants.HEADER_LONGITUDE] = geoloc.longitude
df.at[index, constants.HEADER_COUNTRY] = geoloc.country
df.at[index + 1, constants.HEADER_LATITUDE] = geoloc.latitude
df.at[index + 1, constants.HEADER_LONGITUDE] = geoloc.longitude
df.at[index + 1, constants.HEADER_COUNTRY] = geoloc.country
index = index + 2
# Generate failure login
def generate_failure_login(df, iterations=0, given_time=None, username=None, context_id=None, ip=None):
global currentTime, usernames, ipAddressesForUsers, geoLocationForIP, index
if username is None:
username = usernames[random.randrange(NUMBER_OF_USERS)]
event_time = given_time
if event_time is None:
currentTime = currentTime + datetime.timedelta(seconds=random.randrange(3000))
event_time = currentTime
if context_id is None:
context_id = uuid.uuid4()
if ip is None:
ip = ipAddressesForUsers[username]
geoloc = geoLocationForIP[ip]
for i in range(iterations + 1):
df.at[index, constants.HEADER_USERNAME] = username
df.at[index, constants.HEADER_CONTEXTID] = context_id
df.at[index, constants.HEADER_EVENTTYPE] = constants.EVENTYPE_STEP
df.at[index, constants.HEADER_TIMESTAMP] = str(int(time.mktime(event_time.timetuple()) * 1e3))
currentTime = currentTime + datetime.timedelta(seconds=random.randrange(3))
event_time = currentTime
df.at[index, constants.HEADER_REMOTEIP] = ipAddressesForUsers[username]
df.at[index, constants.HEADER_AUTHENTICATIONSUCCESS] = 0
df.at[index, constants.HEADER_SUSPICIOUS_LOGIN] = 0
df.at[index, constants.HEADER_LATITUDE] = geoloc.latitude
df.at[index, constants.HEADER_LONGITUDE] = geoloc.longitude
df.at[index, constants.HEADER_COUNTRY] = geoloc.country
index = index + 1
# Generate Suspicious login - Login after consecutive failed logins
def generate_suspicious_login_scenario1(df):
global currentTime, usernames, ipAddressesForUsers, index
username = usernames[random.randrange(NUMBER_OF_USERS)]
context_id = uuid.uuid4()
currentTime = currentTime + datetime.timedelta(seconds=random.randrange(3000))
failure_count = random.randrange(4, 7)
generate_failure_login(df, failure_count, currentTime, username, context_id)
index = index + failure_count
currentTime = currentTime + datetime.timedelta(seconds=random.randrange(3))
generate_successful_login(df, currentTime, username, context_id, is_suspicious=True)
index = index + 1
# Generate Suspicious login 2 - Login from suspicious IP
def generate_suspicious_login_scenario2(df):
global currentTime, usernames, ipAddressesForUsers, index
username = usernames[random.randrange(NUMBER_OF_USERS)]
currentTime = currentTime + datetime.timedelta(seconds=random.randrange(3000))
ip = generate_random_ipv4()
geo_loc = None
while geo_loc is None:
while ip in geoLocationForIP:
ip = generate_random_ipv4()
geo_loc = geolocation.get_geolocation(ip)
if geo_loc.latitude == geoLocationForIP[ipAddressesForUsers[username]].latitude and geo_loc.longitude == \
geoLocationForIP[ipAddressesForUsers[username]].longitude:
geo_loc = None
ip = generate_random_ipv4()
geoLocationForIP[ip] = geo_loc
generate_successful_login(df, currentTime, username)
index = index + 1
currentTime = currentTime + datetime.timedelta(seconds=random.randrange(60))
generate_successful_login(df, currentTime, username, ip, is_suspicious=True)
index = index + 1
LOGIN_TABLE_COLUMNS = [constants.HEADER_USERNAME, constants.HEADER_CONTEXTID, constants.HEADER_EVENTTYPE,
constants.HEADER_AUTHENTICATIONSUCCESS, constants.HEADER_REMOTEIP, constants.HEADER_TIMESTAMP,
constants.HEADER_LATITUDE,
constants.HEADER_LONGITUDE, constants.HEADER_COUNTRY, constants.HEADER_SUSPICIOUS_LOGIN]
def main():
global currentTime, usernames, ipAddressesForUsers, geoLocationForIP, index, currentTime
# generate usernames
usernames = ['user' + str(x) for x in range(1, NUMBER_OF_USERS + 1)]
# set current time
currentTime = datetime.datetime.now()
print("Generating IP addresses...")
# generate an IP for each user
for username in usernames:
ipAddressesForUsers[username] = generate_random_ipv4()
geoLocationForIP[ipAddressesForUsers[username]] = geolocation.get_geolocation(ipAddressesForUsers[username])
login_data = pd.DataFrame(index=None, columns=LOGIN_TABLE_COLUMNS)
index = 0
iterations = ITERATIONS
choices = [1, 2, 3, 4]
p = [0.3, 0.3, 0.3, 0.1]
print("Simulating user logins...")
for i in range(iterations):
c = np.random.choice(choices, p=p)
if c == 1:
generate_successful_login(login_data)
elif c == 2:
generate_failure_login(login_data)
elif c == 3:
generate_suspicious_login_scenario1(login_data)
else:
generate_suspicious_login_scenario2(login_data)
print('Iteration ', i, ' of ', iterations, ' completed.')
fileName = 'login_data-' + datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + '.csv'
login_data.to_csv(fileName)
print('Login data saved in \'' + fileName + '\'.')
if __name__ == '__main__':
main()