-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLLiteHandlerClass.cs
More file actions
196 lines (194 loc) · 9.91 KB
/
SQLLiteHandlerClass.cs
File metadata and controls
196 lines (194 loc) · 9.91 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
using System;
using System.IO;
using System.Data.SQLite;
using System.Diagnostics;
namespace ThreatDetectionModule
{
internal static class SQLLiteHandlerClass
{
//TODO introduce timestamp and interval for checing the time for timeout
private static readonly string databaseFileName = "uplockout.db";
private static string _configuredDbPath;
private static void InsertFailedLogon(string UserName)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:InsertFailedLogon: Enter");
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:InsertFailedLogon.UserName: {UserName}");
using (var connection = new SQLiteConnection($"Data Source = {_configuredDbPath}\\{databaseFileName}"))
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:InsertFailedLogon.Connecting to DB.Connection string:Data Source={_configuredDbPath}\\{databaseFileName}");
connection.Open();
using (var command = new SQLiteCommand(connection))
{
command.CommandText = "INSERT INTO PasswordChanges (UserName, Count) VALUES (@name, @count)";
command.Parameters.AddWithValue("@name", UserName);
command.Parameters.AddWithValue("@count", 1);
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:InsertFailedLogon.executing query: {command.CommandText}");
command.ExecuteNonQuery();
}
connection.Close();
}
Debug.WriteLine($"SQLHandler:InsertFailedLogon: Exit");
}
internal static int CheckCounter(string UserName)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CheckCounter: Enter");
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CheckCounter: UserName: {UserName}");
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CheckCounter: Database path: {_configuredDbPath}\\{databaseFileName}");
int retCounter = -1;
try
{
using (var connection = new SQLiteConnection($"Data Source = {_configuredDbPath}\\{databaseFileName}"))
{
connection.Open();
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CheckCounter: Connecting to DB. Connection string: Data Source = {_configuredDbPath}\\{databaseFileName}");
var Query = $"SELECT Count FROM PasswordChanges WHERE UserName = '{UserName}'";
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CheckCounter: Query: SELECT Count FROM PasswordChanges WHERE UserName = '{UserName}' ");
using (var command = new SQLiteCommand(Query, connection))
{
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
retCounter = reader.GetInt16(0);
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CheckCounter: Value Returned: {retCounter}");
}
}
}
connection.Close();
}
}
catch (Exception ex)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CheckCounter: Exception caugth.");
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.InnerException);
Debug.WriteLine(ex.StackTrace);
WindowsLogger.WriteWinLogEvent($"Exception occured \n\r{ex}", EventLogEntryType.Error);
var e = new Exception(ex.Message, ex.InnerException);
throw e;
}
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CheckCounter: Exit");
return retCounter;
}
internal static void IncreaseCounter(string UserName)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter: Enter");
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter: UserName: {UserName}");
int? currentCounter = -1;
try
{
using (var connection = new SQLiteConnection($"Data Source = {_configuredDbPath}\\{databaseFileName}"))
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter: Connecting to DB: Data Source = {_configuredDbPath}\\{databaseFileName}");
connection.Open();
var Query = $"SELECT Count FROM PasswordChanges WHERE UserName = '{UserName}'";
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter: Executing Query: {Query}");
using (var command = new SQLiteCommand(Query, connection))
{
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
currentCounter = reader.GetInt16(0);
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter:Returned value from SQL Get Counter Query:{currentCounter}");
}
}
}
if(currentCounter == -1)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter:Initial value of currentCounter. Will create entry");
InsertFailedLogon(UserName);
}
else if(currentCounter == null)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter:User Not present in database. Will create entry");
InsertFailedLogon(UserName);
}
else
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter:Current Coutner Value: {currentCounter}");
int newCounter = (int)currentCounter + 1;
var UpdateQuery = $"UPDATE PasswordChanges Set Count = {newCounter} WHERE UserName = '{UserName}'";
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter:Executing Update Query: {UpdateQuery}");
using (var command = new SQLiteCommand(UpdateQuery, connection))
{
var res = command.ExecuteNonQuery();
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter:Executed Update query and affeced {res} rows");
}
}
connection.Close();
}
}
catch (Exception ex)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter:Excetpion caught.");
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.InnerException);
Debug.WriteLine(ex.StackTrace);
WindowsLogger.WriteWinLogEvent($"Exception \n\r{ex}", EventLogEntryType.Error);
var e = new Exception(ex.Message, ex.InnerException);
throw e;
}
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:IncreaseCounter: Exit");
}
internal static void CreateDatabase(string databasePath)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CreateDatabase: Enter");
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CreateDatabase: databasePath: {databasePath}");
_configuredDbPath = databasePath;
var dbFile = databasePath + "\\" + databaseFileName;
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CreateDatabase: dbFile: {dbFile}");
try
{
if (!File.Exists(dbFile))
{
try
{
SQLiteConnection.CreateFile(dbFile);
}
catch (Exception ex)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CreateDatabase: Exception caught");
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.InnerException);
Debug.WriteLine(ex.StackTrace);
throw;
}
string sqlCreate = @"CREATE TABLE PasswordChanges(
""UserName"" TEXT NOT NULL UNIQUE,
""Count"" INTEGER
);";
using (var con = new SQLiteConnection($"Data Source={dbFile}"))
{
try
{
con.Open();
var cmd = new SQLiteCommand(con);
cmd.CommandText = sqlCreate;
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CreateDatabase: Executing Query - {sqlCreate}");
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CreateDatabase: Exception caught");
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.InnerException);
Debug.WriteLine(ex.StackTrace);
throw;
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CreateDatabase: Exception caught");
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.InnerException);
Debug.WriteLine(ex.StackTrace);
throw;
}
Debug.WriteLine($"ADFSUPPlugin:SQLHandler:CreateDatabase: Exit");
}
}
}