Skip to content

Commit

Permalink
fix win 11 remove tasks bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Spectrum735 committed Jul 11, 2023
1 parent e8b8101 commit 6813893
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 45 deletions.
84 changes: 55 additions & 29 deletions MinerSearch/MinerSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public class MinerSearch
@"C:\ProgramData\ReaItekHD\taskhost.exe",
@"C:\ProgramData\ReaItekHD\taskhostw.exe",
@"C:\ProgramData\RealtekHD\taskhost.exe",
@"C:\ProgramData\ReaItekHD\taskhostw.exe",
@"C:\ProgramData\RealtekHD\taskhostw.exe",
@"C:\ProgramData\Windows Tasks Service\winserv.exe",
@"C:\ProgramData\WindowsTask\AMD.exe",
@"C:\ProgramData\WindowsTask\AppModule.exe",
Expand Down Expand Up @@ -197,7 +197,7 @@ public class MinerSearch
@"C:\ProgramData\ReaItekHD\taskhost.exe",
@"C:\ProgramData\ReaItekHD\taskhostw.exe",
@"C:\ProgramData\RealtekHD\taskhost.exe",
@"C:\ProgramData\ReaItekHD\taskhostw.exe",
@"C:\ProgramData\RealtekHD\taskhostw.exe",
@"C:\ProgramData\Windows Tasks Service\winserv.exe",
@"C:\ProgramData\WindowsTask\AMD.exe",
@"C:\ProgramData\WindowsTask\AppModule.exe",
Expand Down Expand Up @@ -615,11 +615,23 @@ public void Clean()
{
Logger.WriteLog($"\t[!!] Cannot delete file {path}", Logger.cautionLow);
Logger.WriteLog($"\t[.] Trying to unlock directory...", ConsoleColor.White);
UnlockDirectory(new FileInfo(path).DirectoryName);
try
{
UnlockDirectory(new FileInfo(path).DirectoryName);
try
{
uint processId = GetProcessIdByFilePath(path);

UnlockFile(path);
if (processId != 0)
{
Process process = Process.GetProcessById((int)processId);
if (!process.HasExited)
{
process.Kill();
}
}
}
catch (Exception) { }
File.Delete(path);
if (!File.Exists(path))
{
Expand Down Expand Up @@ -658,11 +670,24 @@ public void Clean()
{
Logger.WriteLog($"\t[!!] Cannot delete file {path}", Logger.cautionLow);
Logger.WriteLog($"\t[.] Trying to unlock directory...", ConsoleColor.White);
UnlockDirectory(new FileInfo(path).DirectoryName);
try
{
UnlockDirectory(new FileInfo(path).DirectoryName);

UnlockFile(path);
try
{
uint processId = GetProcessIdByFilePath(path);

if (processId != 0)
{
Process process = Process.GetProcessById((int)processId);
if (!process.HasExited)
{
process.Kill();
}
}
}
catch (Exception) { }
File.Delete(path);
if (!File.Exists(path))
{
Expand Down Expand Up @@ -1201,12 +1226,19 @@ void ScanTaskScheduler()
{
string taskName = task.Name;
string taskFolder = task.Folder.ToString();

taskService.GetFolder(task.Folder.ToString()).DeleteTask(task.Name);
if (taskService.GetTask($"{taskFolder}\\{taskName}") == null)
try
{
Logger.WriteLog($"\t[+] Empty Task {taskName} was deleted", Logger.success);
taskService.GetFolder(task.Folder.ToString()).DeleteTask(task.Name);
if (taskService.GetTask($"{taskFolder}\\{taskName}") == null)
{
Logger.WriteLog($"\t[+] Empty Task {taskName} was deleted", Logger.success);
}
}
catch (Exception ex)
{
Logger.WriteLog($"\t[+] Cannot delete task {taskFolder}\\{taskName} | {ex.Message}", Logger.error);
}

}

}
Expand All @@ -1232,10 +1264,17 @@ void ScanTaskScheduler()
string taskName = task.Name;
string taskFolder = task.Folder.ToString();

taskService.GetFolder(task.Folder.ToString()).DeleteTask(task.Name);
if (taskService.GetTask($"{taskFolder}\\{taskName}") == null)
try
{
taskService.GetFolder(task.Folder.ToString()).DeleteTask(task.Name);
if (taskService.GetTask($"{taskFolder}\\{taskName}") == null)
{
Logger.WriteLog($"\t[+] Empty Task {taskName} was deleted", Logger.success);
}
}
catch (Exception ex)
{
Logger.WriteLog($"\t[+] Empty Task {taskName} was deleted", Logger.success);
Logger.WriteLog($"\t[+] Cannot delete task {taskFolder}\\{taskName} | {ex.Message}", Logger.error);
}
}
}
Expand Down Expand Up @@ -1286,6 +1325,7 @@ void UnlockDirectory(string dir)
directoryInfo.SetAccessControl(directorySecurity);
File.SetAttributes(dir, FileAttributes.Normal);
}

}
catch (Exception ex)
{
Expand All @@ -1296,7 +1336,7 @@ void UnlockFile(string filePath)
{
try
{
FileInfo directoryInfo = new FileInfo(filePath);
FileInfo fileInfo = new FileInfo(filePath);
FileSecurity fileSecurity = new FileSecurity();
fileSecurity.SetOwner(WindowsIdentity.GetCurrent().User);
File.SetAccessControl(filePath, fileSecurity);
Expand All @@ -1318,6 +1358,7 @@ void UnlockFile(string filePath)
fileSecurity.AddAccessRule(systemRights);
fileSecurity.AddAccessRule(authRight);
File.SetAccessControl(filePath, fileSecurity);

}
catch (Exception ex)
{
Expand All @@ -1328,21 +1369,6 @@ void UnlockFile(string filePath)
#endif
}

try
{
uint processId = GetProcessIdByFilePath(filePath);

if (processId != 0)
{
Process process = Process.GetProcessById((int)processId);
if (!process.HasExited)
{
process.Kill();
}
}
}
catch (Exception) { }

}
public string GetHash()
{
Expand Down
5 changes: 1 addition & 4 deletions MinerSearch/MinerSearch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Security" />
<Reference Include="System.Windows.Forms" />
Expand Down Expand Up @@ -109,10 +110,6 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\SecurityCodeScan.VS2019.5.6.7\analyzers\dotnet\SecurityCodeScan.VS2019.dll" />
<Analyzer Include="..\packages\SecurityCodeScan.VS2019.5.6.7\analyzers\dotnet\YamlDotNet.dll" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.7.2">
<Visible>False</Visible>
Expand Down
26 changes: 14 additions & 12 deletions MinerSearch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MinerSearch
{
Expand All @@ -23,6 +24,19 @@ static void Main(string[] args)
Console.Title = GetRandomTitle("miner search");
WaterMark();

Directory.SetCurrentDirectory(Path.GetDirectoryName(Application.ExecutablePath));
if (!File.Exists("Microsoft.Win32.TaskScheduler.dll"))
{
try
{
File.WriteAllBytes("Microsoft.Win32.TaskScheduler.dll", Resources.TaskScheduler);
}
catch (Exception ex)
{
Logger.WriteLog($"Scan Task error: {ex.Message}", Logger.error);
}
}

if (args.Length > 0)
{
foreach (var arg in args)
Expand Down Expand Up @@ -78,18 +92,6 @@ static void Main(string[] args)
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
MinerSearch mk = new MinerSearch();

if (!File.Exists("Microsoft.Win32.TaskScheduler.dll"))
{
try
{
File.WriteAllBytes("Microsoft.Win32.TaskScheduler.dll", Resources.TaskScheduler);
}
catch (Exception ex)
{
Logger.WriteLog($"Scan Task error: {ex.Message}", Logger.error);
}
}

if (!no_runtime)
{
Logger.WriteLog("Preparing to scan processes, please wait...", Logger.head);
Expand Down

0 comments on commit 6813893

Please sign in to comment.