Delete yourself.frm
Code: Select all
'https://github.com/LloydLabs/delete-self-poc
'#Region "Form"
#ifndef _WIN32_WINNT
#define _WIN32_WINNT &H0600 '仅仅工作在Windows Vista及以上,编译为64位就只能在Windows系统是64位上正常工作 works with Windows Vista and above. only
#endif
#if defined(__FB_MAIN__) AndAlso Not defined(__MAIN_FILE__)
#ifdef __FB_WIN32__
#cmdline "Form1.rc"
#endif
Const _MAIN_FILE_ = __FILE__
#endif
#include once "mff/Form.bi"
#include once "mff/CommandButton.bi"
#include once "mff/TextBox.bi"
#include once "mff/Dialogs.bi"
Function RenameHandle(HHANDLE As HANDLE) As Boolean '重命名文件
Dim fRename As FILE_RENAME_INFO '文件重命名结构体
RtlSecureZeroMemory(@fRename, SizeOf(fRename))
Dim lpwStream As WString Ptr = @":666666" ' @":666666" '文件名 'CAllocate((MAX_PATH + 1) * SizeOf(WString)) '
fRename.FileNameLength = SizeOf(lpwStream) '文件名长度
'SizeOf(lpwStream)=8 SizeOf(fRename)=24 Print "SizeOf(lpwStream)=" & SizeOf(lpwStream) & " SizeOf(fRename)=" & SizeOf(fRename)
RtlCopyMemory(@fRename.FileName, lpwStream, SizeOf(lpwStream)) '填充文件名
'FileRenameInfo 应更改文件名,用于文件句柄(来源 FILE_INFO_BY_HANDLE_CLASS枚举值)
'SetFileInformationByHandle 设置指定文件的文件信息
Return SetFileInformationByHandle(HHANDLE, FileRenameInfo, @fRename, SizeOf(fRename))
End Function
Function DepositeHandle(HHANDLE As HANDLE) As Boolean
Dim fDelete As FILE_DISPOSITION_INFO
fDelete.DeleteFileW = True '指示是否应删除该文件,设置为TRUE以删除文件。如果该句柄使用FILE_FLAG_DELETE_ON_CLOSE打开,则此成员不起作用。
'FileDispositionInfo应删除该文件,用于任何句柄(来源 FILE_INFO_BY_HANDLE_CLASS枚举值)
Return SetFileInformationByHandle(HHANDLE, FileDispositionInfo, @fDelete, SizeOf(fDelete))
End Function
Function DeleteSelf(ByRef wcPath As WString) As Boolean
'1.打开自身文件,给定 DELETE 权限(仅需要DELETE,在FB中DELETE是关键字,故写成DELETE__)
Dim hCurrent As HANDLE = CreateFileW(wcPath, DELETE__, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
If hCurrent = INVALID_HANDLE_VALUE Then
Debug.Print "failed to acquire handle to current running process" & hCurrent '无法获取当前正在运行的进程的句柄
Return False
End If
'2.使用 SetFileInformationByHandle 将主文件流 :$DATA 重命名为 :wtfbbq。
Debug.Print "attempting to rename file name"
If RenameHandle(hCurrent) = False Then
Debug.Print "failed to rename to stream!" '试图重命名文件名,无法重命名
Return False
End If
'3.关闭HANDLE
Debug.Print "successfully renamed file primary :$DATA ADS to specified stream, closing initial handle"
CloseHandle(hCurrent)
'4.打开自身文件,将 FileDispositionInfo 类的 DeleteFile 设置为 TRUE。
hCurrent = CreateFileW(wcPath, DELETE__, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)
If hCurrent = INVALID_HANDLE_VALUE Then
Debug.Print "failed to reopen current module! hCurrent=" & hCurrent '无法重新打开当前模块
Return False
End If
If DepositeHandle(hCurrent) = False Then
Debug.Print "failed to set delete deposition!" '无法设置删除
Return False
End If
'5.关闭HANDLE以触发文件处置
Debug.Print "closing handle to trigger deletion deposition"
CloseHandle(hCurrent)
If FileExists(wcPath) = True Then
Debug.Print "failed to delete copy, file still exists!" '文件仍然存在,删除失败
Return False
End If
Return True
End Function
Using My.Sys.Forms
Type Form1Type Extends Form
Declare Static Sub _CommandButton1_Click(ByRef Sender As Control)
Declare Sub CommandButton1_Click(ByRef Sender As Control)
Declare Static Sub _cmdBrows_Click(ByRef Sender As Control)
Declare Sub cmdBrows_Click(ByRef Sender As Control)
Declare Constructor
Dim As CommandButton CommandButton1, cmdBrows
Dim As TextBox txtFileName
Dim As OpenFileDialog OpenFileDialog1
End Type
Constructor Form1Type
' Form1
With This
.Name = "Form1"
.Text = "Form1"
.Designer = @This
.SetBounds 0, 0, 600, 70
End With
' CommandButton1
With CommandButton1
.Name = "CommandButton1"
.Text = "Delete yourself"
.TabIndex = 0
.SetBounds 10, 10, 90, 20
.Designer = @This
.OnClick = @_CommandButton1_Click
.Parent = @This
End With
' txtFileName
With txtFileName
.Name = "txtFileName"
.Text = ExePath & "\" & App.ExeName & ".exe"
.TabIndex = 1
.Anchor.Right = AnchorStyle.asAnchor
.Anchor.Left = AnchorStyle.asAnchor
.SetBounds 100, 10, 420, 20
.Designer = @This
.Parent = @This
End With
' cmdBrows
With cmdBrows
.Name = "cmdBrows"
.Text = "..."
.TabIndex = 2
.ControlIndex = 0
.Caption = "..."
.Anchor.Right = AnchorStyle.asAnchorProportional
.Anchor.Left = AnchorStyle.asAnchorProportional
.SetBounds 528, 10, 62, 20
.Designer = @This
.OnClick = @_cmdBrows_Click
.Parent = @This
End With
' OpenFileDialog1
With OpenFileDialog1
.Name = "OpenFileDialog1"
.SetBounds 10, 40, 16, 16
.Designer = @This
.Parent = @This
End With
End Constructor
Private Sub Form1Type._cmdBrows_Click(ByRef Sender As Control)
(*Cast(Form1Type Ptr, Sender.Designer)).cmdBrows_Click(Sender)
End Sub
Private Sub Form1Type._CommandButton1_Click(ByRef Sender As Control)
(*Cast(Form1Type Ptr, Sender.Designer)).CommandButton1_Click(Sender)
End Sub
Dim Shared Form1 As Form1Type
#if _MAIN_FILE_ = __FILE__
App.DarkMode = True
Form1.MainForm = True
Form1.Show
App.Run
#endif
'#End Region
Private Sub Form1Type.CommandButton1_Click(ByRef Sender As Control)
If FileExists(txtFileName.Text) Then
'txtFileName.Text = IIf(DeleteSelf(txtFileName.Text), "删除成功", "删除失败")
MsgBox IIf(DeleteSelf(txtFileName.Text), "Delete successfully", "Delete failed")
End If
End Sub
Private Sub Form1Type.cmdBrows_Click(ByRef Sender As Control)
Dim As OpenFileDialog OpenD
OpenD.InitialDir = ExePath
OpenD.Caption = "Select a files..." '选择一个文件
OpenD.Filter = "All Files" & "|*.*|"
If Not OpenD.Execute Then Exit Sub
txtFileName.Text = OpenD.FileName
End Sub