-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessMonitor.cs
146 lines (128 loc) · 5.31 KB
/
ProcessMonitor.cs
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
using System;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace ArtemisSecurity
{
public static class ProcessMonitor
{
[DllImport("user32.dll")]
private static extern int GetWIndowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
private static readonly List<Regex> BlacklistedProcessPatterns = new List<Regex>
{
new Regex(@"dnspy|ida|ida64|idag|idag64|idaw|idaw64|idaq|idaq64|idau|idau64", RegexOptions.IgnoreCase),
new Regex(@"ollydbg|x64dbg|x32dbg|windbg|IMMUNITYDEBUGGER", RegexOptions.IgnoreCase),
new Regex(@"wireshark|fiddler|charles|burpsuite", RegexOptions.IgnoreCase),
new Regex(@"javaw|radare2|binary ninja|hopper", RegexOptions.IgnoreCase),
new Regex(@"ilspy|dotpeek|justdecompile|reflector", RegexOptions.IgnoreCase),
new Regex(@"de4dot|simplify|deobfuscator", RegexOptions.IgnoreCase)
};
public static List<string> GetSimilarWindowTitles(string processName)
{
var similarWindows = new List<string>();
Process[] processes = Process.GetProcessesByName(processName);
foreach (Process process in processes)
{
IntPtr mainWindowHandle = process.MainWindowHandle;
if (mainWindowHandle != IntPtr.Zero)
{
int textLength = GetWIndowTextLength(mainWindowHandle);
StringBuilder windowTitle = new StringBuilder(textLength + 1);
GetWindowText(mainWindowHandle, windowTitle, windowTitle.Capacity);
similarWindows.Add(windowTitle.ToString());
}
}
return similarWindows;
}
private static readonly List<string> WhitelistedProcesses = new List<string>
{
"explorer", "svchost", "csrss", "winlogon"
};
public static bool DetectBlacklistedProcesses(ThreatAnalyzer threatAnalyzer, List<(string Name, float Level)> detectedThreats)
{
var runningProcesses = Process.GetProcesses();
bool detected = false;
foreach (var process in runningProcesses)
{
if (WhitelistedProcesses.Contains(process.ProcessName, StringComparer.OrdinalIgnoreCase))
{
continue;
}
if (BlacklistedProcessPatterns.Any(pattern => pattern.IsMatch(process.ProcessName)))
{
Console.WriteLine($"Unauthorized process detected: {process.ProcessName}");
float threatLevel = CalculateThreatLevel(process);
detectedThreats.Add((process.ProcessName, threatLevel));
// Log and analyze the detected threat
threatAnalyzer.LogAndAnalyzeThreat(process.ProcessName, threatLevel);
MonitorProcessDetails(process);
detected = true;
}
}
return detected;
}
private static void MonitorProcessDetails(Process process)
{
Console.WriteLine($"Process ID: {process.Id}");
Console.WriteLine($"Memory Usage: {process.WorkingSet64 / 1024 / 1024} MB");
Console.WriteLine($"CPU Time: {process.TotalProcessorTime}");
try
{
Console.WriteLine($"Start Time: {process.StartTime}");
}
catch (Exception)
{
Console.WriteLine("Start Time: Unable to retrieve");
}
// Attempt to block IPC
try
{
process.PriorityClass = ProcessPriorityClass.Idle;
Console.WriteLine("Process priority set to Idle");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to modify process priority: {ex.Message}");
}
}
private static float CalculateThreatLevel(Process process)
{
string processName = process.ProcessName.ToLower();
var threatLevels = new Dictionary<string, float>
{
{"debugger", 1.0f},
{"sniffer", 1.0f},
{"backdoor", 1.0f},
{"deobfuscator", 1.0f},
{"decompiler", 1.0f},
{"emulator", 1.0f},
{"disassembler", 1.0f},
{"injector", 0.5f},
{"rootkit", 1.0f},
{"keylogger", 0.7f},
{"trojan", 0.5f},
{"adware", 0.3f},
{"spyware", 0.2f},
{"ransomware", 0.5f},
{"phishingtool", 1.0f},
{"cryptominer", 0.3f},
{"remoteaccesstool", 0.7f},
{"packetanalyzer", 0.8f},
{"fakeantivirus", 0.4f}
};
foreach (var threat in threatLevels)
{
if (processName.Contains(threat.Key))
{
return threat.Value;
}
}
return 0.1f;
}
}
}