Introduce file ownership management and improve error handling and backup process

- Added `changeOwnership` feature to allow users to change file ownership to Administrators with full control permissions.
- Enhanced file backup process by appending a timestamp to backup file names, ensuring uniqueness.
- Implemented comprehensive error handling across multiple operations, providing user-friendly error messages.
- Refactored existing methods to integrate the new ownership and error handling features, improving code clarity and maintainability.
- Updated configuration files (`app.config`, `Settings.Designer.cs`, `Settings.settings`) to include the new `changeOwnership` setting.
- Modified project file (`Win_1337_Patch.csproj`) to accommodate new dependencies and settings for the added features.
This commit is contained in:
ramhaidar 2024-08-25 06:44:09 +07:00
commit 6f33264cb4
5 changed files with 240 additions and 117 deletions

View file

@ -24,23 +24,30 @@ namespace Win_1337_Patch
this.Text = "Win 1337 Apply Patch File " + ver;
linkdfox.Text = ver + " By DeltaFoX";
}
private void set()
{
try
{
t1337.Text = Ellipsis.Compact(f1337, t1337, EllipsisFormat.Path);
toolTip1.SetToolTip(t1337, f1337);
Properties.Settings.Default["url1337"] = f1337;
Properties.Settings.Default.Save();
string[] lines = File.ReadAllLines(f1337);
if (!check_Symbol(lines[0]))
return;
string unf = lines[0].Substring(1).ToLower().Trim();
string nf = Path.GetFileName(unf);
string ext = Path.GetExtension(unf);
OpenFileDialog apriDialogoFile1 = new OpenFileDialog();
apriDialogoFile1.FileName = nf;
apriDialogoFile1.Filter = "File " + ext + "|" + nf;
apriDialogoFile1.FilterIndex = 0;
apriDialogoFile1.Title = "Select the file \"" + nf + "\" File...";
OpenFileDialog apriDialogoFile1 = new OpenFileDialog
{
FileName = nf,
Filter = "File " + ext + "|" + nf,
FilterIndex = 0,
Title = "Select the file \"" + nf + "\" File..."
};
if (apriDialogoFile1.ShowDialog() == DialogResult.OK)
{
@ -58,6 +65,12 @@ namespace Win_1337_Patch
exe = String.Empty;
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred while setting up the file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void t1337_DragDrop(object sender, DragEventArgs e)
{
try
@ -65,40 +78,48 @@ namespace Win_1337_Patch
f1337 = ((string[])e.Data.GetData(DataFormats.FileDrop, false))[0];
set();
}
catch
catch (Exception ex)
{
//Nothing;
MessageBox.Show($"An error occurred while processing drag and drop: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
private void btnSelect1337_Click(object sender, EventArgs e)
{
try
{
string url1337 = Properties.Settings.Default["url1337"].ToString();
OpenFileDialog apriDialogoFile1 = new OpenFileDialog();
apriDialogoFile1.Filter = "File 1337|*.*";
apriDialogoFile1.FilterIndex = 0;
apriDialogoFile1.Title = "Select the .1337 File...";
apriDialogoFile1.InitialDirectory = url1337 != "" ? url1337 : Directory.GetCurrentDirectory() + "\\";
apriDialogoFile1.RestoreDirectory = true;
OpenFileDialog apriDialogoFile1 = new OpenFileDialog
{
Filter = "File 1337|*.*",
FilterIndex = 0,
Title = "Select the .1337 File...",
InitialDirectory = url1337 != "" ? url1337 : Directory.GetCurrentDirectory() + "\\",
RestoreDirectory = true
};
if (apriDialogoFile1.ShowDialog() == DialogResult.OK)
{
f1337 = apriDialogoFile1.FileName;
set();
}
}
catch
catch (Exception ex)
{
//Nothing;
MessageBox.Show($"An error occurred while selecting the .1337 file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
private void t1337_DragEnter(object sender, DragEventArgs e)
{
try
{
e.Effect = DragDropEffects.All;
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred during DragEnter: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private bool check_Symbol(string s)
{
@ -121,12 +142,12 @@ namespace Win_1337_Patch
{
DFoX_Patch();
}
catch
catch (Exception ex)
{
MessageBox.Show("Problem occured when Patching...", "Info...", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
MessageBox.Show($"A problem occurred when patching: {ex.Message}", "Info...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void DFoX_Patch()
{
if (!File.Exists(exe) || !File.Exists(f1337))
@ -134,6 +155,20 @@ namespace Win_1337_Patch
MessageBox.Show("Files are no Longer Present...", "Info...", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (cchangeOwnership.Checked)
{
try
{
UnlockDLL(exe);
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred while changing ownership: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
string[] lines = File.ReadAllLines(f1337);
if (!check_Symbol(lines[0]))
return;
@ -167,9 +202,12 @@ namespace Win_1337_Patch
{
if (controlloBackup.Checked == true)
{
if (File.Exists(exe + ".BAK"))
File.Delete(exe + ".BAK");
File.Copy(exe, exe + ".BAK");
string dateSuffix = DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss-tt");
string backupFileName = $"{exe}.{dateSuffix}.BAK";
if (File.Exists(backupFileName))
File.Delete(backupFileName);
File.Copy(exe, backupFileName);
}
if (File.Exists(exe))
File.Delete(exe);
@ -177,8 +215,8 @@ namespace Win_1337_Patch
SistemaPeCks(exe);
MessageBox.Show("File " + Path.GetFileName(exe) + " Patched...", "Info...", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return;
}
private void SistemaPeCks(string file)
{
try
@ -186,23 +224,26 @@ namespace Win_1337_Patch
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite))
{
ImageRemoveCertificate(fs.SafeFileHandle.DangerousGetHandle(), 0);
fs.Close();
}
mCheckSum PE = new mCheckSum();
PE.FixCheckSum(file);
}
catch
catch (Exception ex)
{
//Nothing
MessageBox.Show($"An error occurred while processing PE checksum: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return;
}
private void DFoX_Load(object sender, EventArgs e)
{
try
{
string urlexe = Properties.Settings.Default["urlexe"].ToString().Trim();
string url1337 = Properties.Settings.Default["url1337"].ToString().Trim();
cfileoffsett.Checked = (bool)Properties.Settings.Default["fixoffset"];
controlloBackup.Checked = (bool)Properties.Settings.Default["backup"];
cchangeOwnership.Checked = (bool)Properties.Settings.Default["changeOwnership"];
if (urlexe != "")
{
texe.Text = Ellipsis.Compact(Path.GetFileName(urlexe), texe, EllipsisFormat.Path);
@ -211,6 +252,7 @@ namespace Win_1337_Patch
}
else
texe.Text = "Select the Exe/Dll to Patch...";
if (url1337 != "" && urlexe != "")
{
t1337.Text = Ellipsis.Compact(url1337, t1337, EllipsisFormat.Path);
@ -220,23 +262,63 @@ namespace Win_1337_Patch
else
t1337.Text = "Select a .1337 File...";
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred during form load: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cfileoffsett_CheckedChanged(object sender, EventArgs e)
{
try
{
Properties.Settings.Default["fixoffset"] = cfileoffsett.Checked;
Properties.Settings.Default.Save();
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred while saving fix offset setting: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void controlloBackup_CheckedChanged(object sender, EventArgs e)
{
try
{
Properties.Settings.Default["backup"] = controlloBackup.Checked;
Properties.Settings.Default.Save();
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred while saving backup setting: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void cchangeOwnership_CheckedChanged(object sender, EventArgs e)
{
try
{
Properties.Settings.Default["changeOwnership"] = cchangeOwnership.Checked;
Properties.Settings.Default.Save();
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred while saving change ownership setting: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void linkdfox_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
try
{
_apriUrl(@"https://github.com/Deltafox79/Win_1337_Apply_Patch");
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred while opening the URL: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void _apriUrl(string url)
{
try
@ -244,16 +326,21 @@ namespace Win_1337_Patch
string browserPath = ottieniLaPathBrowser();
if (browserPath == string.Empty)
browserPath = "iexplore";
Process process = new Process();
process.StartInfo = new ProcessStartInfo(browserPath);
process.StartInfo.Arguments = url;
Process process = new Process
{
StartInfo = new ProcessStartInfo(browserPath)
{
Arguments = url
}
};
process.Start();
}
catch
catch (Exception ex)
{
//Nothing
MessageBox.Show($"An error occurred while opening the browser: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static string ottieniLaPathBrowser()
{
string name = String.Empty;
@ -268,11 +355,10 @@ namespace Win_1337_Patch
if (!name.EndsWith("exe"))
name = name.Substring(0, name.LastIndexOf(".exe") + 4);
}
catch
catch (Exception ex)
{
return String.Empty;
MessageBox.Show($"An error occurred while retrieving browser path: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
@ -283,32 +369,14 @@ namespace Win_1337_Patch
}
private void t1337_DoubleClick(object sender, EventArgs e)
{
btnSelect1337.PerformClick();
}
private void cchangeOwnership_CheckedChanged(object sender, EventArgs e)
{
if (cchangeOwnership.Checked)
{
try
{
if (!string.IsNullOrEmpty(exe))
{
UnlockDLL(exe);
MessageBox.Show($"Ownership of {exe} changed to Administrators and full control granted.", "Ownership Changed", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Please select a DLL/EXE file first.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cchangeOwnership.Checked = false;
}
btnSelect1337.PerformClick();
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
cchangeOwnership.Checked = false;
}
MessageBox.Show($"An error occurred during double click: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
@ -322,22 +390,32 @@ namespace Win_1337_Patch
try
{
FileSecurity fileSecurity = File.GetAccessControl(filePath);
IdentityReference administrators = new NTAccount("Administrators");
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe")
{
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true
};
fileSecurity.SetOwner(administrators);
using (Process process = new Process())
{
process.StartInfo = psi;
process.Start();
File.SetAccessControl(filePath, fileSecurity);
using (StreamWriter sw = process.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine($"takeown /F \"{filePath}\"");
sw.WriteLine($"icacls \"{filePath}\" /grant Administrators:F");
}
}
FileSystemAccessRule accessRule = new FileSystemAccessRule(administrators,
FileSystemRights.FullControl,
AccessControlType.Allow);
process.WaitForExit();
}
fileSecurity.AddAccessRule(accessRule);
File.SetAccessControl(filePath, fileSecurity);
MessageBox.Show($"Owner changed to 'Administrators' and full control permissions granted to 'Administrators' for file: {filePath}", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show($"Ownership and permissions of {filePath} have been successfully changed.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{

View file

@ -70,5 +70,17 @@ namespace Win_1337_Patch.Properties {
this["backup"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("True")]
public bool changeOwnership {
get {
return ((bool)(this["changeOwnership"]));
}
set {
this["changeOwnership"] = value;
}
}
}
}

View file

@ -14,5 +14,8 @@
<Setting Name="backup" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="changeOwnership" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
</Settings>
</SettingsFile>

View file

@ -12,6 +12,21 @@
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -91,5 +106,17 @@
<ItemGroup>
<Content Include="vampire.ico" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.8">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4.8 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View file

@ -19,6 +19,9 @@
<setting name="backup" serializeAs="String">
<value>True</value>
</setting>
<setting name="changeOwnership" serializeAs="String">
<value>True</value>
</setting>
</Win_1337_Patch.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/></startup></configuration>