-
Notifications
You must be signed in to change notification settings - Fork 0
/
SshTarpit.razor
254 lines (232 loc) · 9.13 KB
/
SshTarpit.razor
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
@page "/"
@using UI.SshTarpitAnalyzer.Blazor.Data
<h3>SshTarpit</h3>
<FileEdit @ref="fileEdit" Changed="@OnChanged" Progressed="@OnProgressed"
Started="@OnStarted" Ended="@OnEnded" MaxMessageSize="1048576" />
<p>@uploadProgress</p>
<Field Horizontal="true">
<FieldLabel ColumnSize="ColumnSize.Is2">Filter by IP:</FieldLabel>
<FieldBody ColumnSize="ColumnSize.Is10">
<TextEdit Text="@filterIp" TextChanged="@(async (string value) => await OnFilterIpChanged(value))"></TextEdit>
</FieldBody>
</Field>
<Fields>
<Field ColumnSize="ColumnSize.Is2.OnDesktop">
<Button Color="Color.Primary" Clicked="@(async () => await HandleRedraw())">Analyze log</Button>
</Field>
<Field ColumnSize="ColumnSize.Is2.OnDesktop">
<NumericEdit TValue="int" @ref="timeIntervallToGroupInput" Value="10" Max="10000"></NumericEdit>
</Field>
<Field ColumnSize="ColumnSize.Is2.OnDesktop">
<Select TValue="TimeIntervall" @bind-SelectedValue="@selectedTimeIntervall">
<SelectItem Value="TimeIntervall.Seconds">Seconds</SelectItem>
<SelectItem Value="TimeIntervall.Minutes">Minutes</SelectItem>
<SelectItem Value="TimeIntervall.Hours">Hours</SelectItem>
</Select>
</Field>
<Field ColumnSize="ColumnSize.Is2.OnDesktop">
<Button Color="Color.Primary" Clicked="@(async () => await HandleRedraw(true))">Group by</Button>
</Field>
</Fields>
<p>@connections</p>
<LineChart @ref="lineChart" TItem="double" />
@if (attackerInfoList != null)
{
<table class="table">
<thead>
<tr>
<th>#</th>
<th>IP</th>
<th>Count</th>
<th>∅ time</th>
<th>Standard derivation</th>
</tr>
</thead>
<tbody>
@foreach ((AttackerInfo attackerInfo, int index) in attackerInfoList
.OrderByDescending(x => x.Count)
.Take(10)
.Select((x, i) => (x, i)))
{
<tr>
<td>@(index+1)</td>
<td>@attackerInfo.Ip</td>
<td>@attackerInfo.Count</td>
<td>@GetTimeText(attackerInfo.AverageConnectionTime)</td>
<td>@GetTimeText(attackerInfo.StandardDerivation)</td>
</tr>
}
</tbody>
</table>
}
@code {
FileEdit fileEdit;
NumericEdit<int> timeIntervallToGroupInput;
string uploadProgress;
LineChart<double> lineChart;
IReadOnlyList<ConnectionInformation> connectionInformationList;
IReadOnlyList<ConnectionInformation> connectionInformationFilteredList;
string sshTarpitLog;
ILogFileParser logFileParser = new LogFileParser();
string connections;
string filterIp;
IReadOnlyList<AttackerInfo> attackerInfoList = null;
TimeIntervall selectedTimeIntervall = TimeIntervall.Seconds;
async Task OnChanged(FileChangedEventArgs e)
{
try
{
foreach (IFileEntry file in e.Files)
{
// A stream is going to be the destination stream we're writing to.
using (MemoryStream stream = new MemoryStream())
{
// Here we're telling the FileEdit where to write the upload result
await file.WriteToStreamAsync(stream);
// Once we reach this line it means the file is fully uploaded.
// In this case we're going to offset to the beginning of file
// so we can read it.
stream.Seek(0, SeekOrigin.Begin);
// Use the stream reader to read the content of uploaded file,
// in this case we can assume it is a textual file.
using (StreamReader reader = new StreamReader(stream))
{
sshTarpitLog = await reader.ReadToEndAsync();
}
}
}
}
finally
{
this.StateHasChanged();
}
}
void OnProgressed(FileProgressedEventArgs e)
{
uploadProgress = $"Upload progress: {e.Percentage:N2} %";
}
void OnStarted(FileStartedEventArgs e)
{
uploadProgress = $"Upload starts... ({e.File?.Name})";
}
void OnEnded(FileEndedEventArgs e)
{
uploadProgress = "Upload complete. You can now analyze the data.";
}
async Task OnFilterIpChanged(string value)
{
this.filterIp = value;
await HandleRedraw();
}
async Task HandleRedraw()
{
this.connectionInformationList = await GetConnectionInformationList();
this.attackerInfoList = this.logFileParser.GetAttackerInfoList(this.connectionInformationList);
await HandleRedraw(false);
}
async Task HandleRedraw(bool group)
{
await lineChart.Clear();
this.connectionInformationFilteredList = connectionInformationList;
if (!string.IsNullOrWhiteSpace(filterIp))
{
this.connectionInformationFilteredList = connectionInformationList
.Where(x => x.Ip == filterIp).ToList();
}
List<(int seconds, int count)> logFileDataList
= await Task.Run<List<(int seconds, int count)>>(() => LogFileData(connectionInformationFilteredList, group));
List<string> labelList = new();
List<double> dataList = new();
foreach ((int seconds, int count) in logFileDataList)
{
string label = GetTimeText(seconds);
labelList.Add(label);
dataList.Add(count);
}
await lineChart.AddLabelsDatasetsAndUpdate(labelList, GetLineChartDataset(dataList));
this.connections = $"Connections: {connectionInformationFilteredList.Count}";
}
LineChartDataset<double> GetLineChartDataset(List<double> dataList)
{
return new LineChartDataset<double>
{
Label = "# of seconds in the tarpit",
Data = dataList,
BackgroundColor = backgroundColors,
BorderColor = borderColors,
Fill = true,
PointRadius = 2,
BorderDash = new List<int> { }
};
}
List<string> backgroundColors = new List<string> { ChartColor.FromRgba(255, 99, 132, 0.2f), ChartColor.FromRgba(54, 162, 235, 0.2f), ChartColor.FromRgba(255, 206, 86, 0.2f), ChartColor.FromRgba(75, 192, 192, 0.2f), ChartColor.FromRgba(153, 102, 255, 0.2f), ChartColor.FromRgba(255, 159, 64, 0.2f) };
List<string> borderColors = new List<string> { ChartColor.FromRgba(255, 99, 132, 1f), ChartColor.FromRgba(54, 162, 235, 1f), ChartColor.FromRgba(255, 206, 86, 1f), ChartColor.FromRgba(75, 192, 192, 1f), ChartColor.FromRgba(153, 102, 255, 1f), ChartColor.FromRgba(255, 159, 64, 1f) };
async Task<IReadOnlyList<ConnectionInformation>> GetConnectionInformationList()
{
if (string.IsNullOrEmpty(this.sshTarpitLog))
{
return new List<ConnectionInformation>();
}
IReadOnlyList<ConnectionInformation> connectionInformationList
= await Task<IReadOnlyList<ConnectionInformation>>.Run(() => logFileParser.Parse(sshTarpitLog));
return connectionInformationList;
}
List<(int seconds, int count)> LogFileData(IReadOnlyList<ConnectionInformation> connectionInformationList,
bool group)
{
List<(int seconds, int count)> ret = new();
int groupPerSeconds = 1;
if (group)
{
groupPerSeconds = GetGroupBySeconds();
}
foreach (var grouped in connectionInformationList
.GroupBy(x => ((int)x.Duration.TotalSeconds) / groupPerSeconds)
.OrderBy(x => x.Key))
{
int count = grouped.Count();
int xValue = (group) ? (grouped.Key * groupPerSeconds) + groupPerSeconds : grouped.Key;
ret.Add((xValue, count));
}
return ret;
}
int GetGroupBySeconds()
{
int seconds = 1;
if (timeIntervallToGroupInput.Value > 0)
{
switch (selectedTimeIntervall)
{
case TimeIntervall.Seconds:
seconds = timeIntervallToGroupInput.Value;
break;
case TimeIntervall.Minutes:
seconds = timeIntervallToGroupInput.Value * 60;
break;
case TimeIntervall.Hours:
seconds = timeIntervallToGroupInput.Value * 60 * 60;
break;
default:
throw new ArgumentOutOfRangeException($"{nameof(selectedTimeIntervall)} = {selectedTimeIntervall}");
}
}
return seconds;
}
string GetTimeText(int seconds)
{
string label = $"{seconds.ToString()} s";
if (seconds >= 60)
{
int minutes = seconds / 60;
int secondsPart = seconds % 60;
label = $"{minutes}m {secondsPart}s";
if (minutes >= 60)
{
int hours = minutes / 60;
int minutesPart = minutes % 60;
label = $"{hours}h {minutesPart}m {secondsPart}s";
}
}
return label;
}
}