简体   繁体   中英

VB.net Reboot and Continue Application

I have a VB.net Application; within this Application some other tools are installed, and I need to restart the Computer afterwards.

I am executing

System.Diagnostics.Process.Start("ShutDown", "/r /t 00")

Am I able to automatically continue my VB.net program as soon as the PC has restarted?

Thanks in advance!

You can schedule the task to run at the computer startup:

wShell.Run "SchTasks /Create /MO ONLOGON /TN ""Test Task"" /TR ""<name of your file>""", 0

When the computer starts up again and you log in, the script will be re-run.

Here's an even better method I created, which uses GPO to get the security identifier that schedules a task at the Highest Administrative Level . I tested it as a non-administrator and it is highly effective.

For further review of Security Identifiers, please see:

https://learn.microsoft.com/en-us/windows/security/identity-protection/access-control/security-identifiers

Imports System.ComponentModel

Public Class Form1

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      
        Rythorians_Scheduler()       

    End Sub


#Region " GPO Security Identifier | Creators Owner ID, (Highest Mandatory Level) | Schedule Task  "
    REM:GPO cmdlet creates a GPO with a specified name. By default, the newly created GPO is not linked to a site,
    REM:domain, or organizational unit (OU). You can use this cmdlet To create a GPO that Is based On a starter GPO by
    REM:specifying the GUID Or the display name of the Starter GPO, Or by piping a StarterGpo Object into the cmdlet.
    REM:The cmdlet returns a GPO Object, which represents the created GPO that you can pipe "To other Group Policy cmdlets."
    Public Function GP_OverRide(cmd As String,
                        Optional args As String = "",
                        Optional startin As String = "") As String
        GP_OverRide = ""
        Try
            Dim px = New Process With {
                .StartInfo = New ProcessStartInfo(cmd, args)
            }
            If startin <> "" Then px.StartInfo.WorkingDirectory = startin
            px.StartInfo.RedirectStandardOutput = True
            px.StartInfo.RedirectStandardError = True
            px.StartInfo.UseShellExecute = False
            px.StartInfo.CreateNoWindow = True
            px.Start()
            px.WaitForExit()
            Dim sMax = px.StandardOutput.ReadToEnd
            sMax += px.StandardError.ReadToEnd
            GP_OverRide = sMax
        Catch ex As Exception
        End Try
    End Function REM:Get Process Output.

    REM:Display owner System Via; The <Security Identifier>
    Public Function SecurityIdentifier() As Boolean
        SecurityIdentifier = False
        'Displays user, group, and privileged information for the user who is currently logged on to the local system.
        'If used without parameters, whoami displays the current domain and user name.
        'https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/whoami
        Dim xxyy = GP_OverRide("c: \windows\system32\cmd.exe",
                    "/c whoami /all | findstr /I /C:""S-1-5-32-544""") '<<This is a Security Identifier
        If xxyy.Contains("S-1-5-32-544") Then SecurityIdentifier = True
    End Function ' Check if can get Higher.

    'Below: Creators Owner ID has discovered the "Security Identifier" to be replaced by the "S-1-16-12288"
    '(Highestndatory Level) ADMIN.
    'A Security Identifier (SID) is used to uniquely identify a security principal or security group. Security principals can represent any entity
    'that can be authenticated by the operating system, such as a user account, a computer account, or a thread or process that runs in the security
    'context of a user or computer account.Each account Or group, Or process running in the security context of the account,
    'has a unique SID that Is issued by an authority, such as a Windows domain controller. It Is stored in a security database.
    'The system generates the SID that identifies a particular account Or group at the time the account Or group Is created.
    'When a SID has been used as the unique identifier for a user Or group, it can never be used again to identify another user Or group.
    'Each time a user signs in, the system creates an access token for that user. The access token contains the user's SID, user rights, and the SIDs
    'for any groups the user belongs to. This token provides the security context for whatever actions the user performs on that computer.
    'In addition to the uniquely created, domain-specific SIDs that are assigned to specific users And groups, there are well-known SIDs that identify
    'generic groups And generic users. For example, the Everyone And World SIDs identify a group that includes all users. Well-known SIDs have values
    'that remain constant across all operating systems. SIDs are a fundamental building block Of the Windows security model.
    'They work With specific components Of the authorization And access control technologies In the security infrastructure Of the
    'Windows Server operating systems. This helps protect access To network resources And provides a more secure computing environment.
    '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    'How security identifiers work:
    'Users refer To accounts by Using the account name, but the operating system internally refers To accounts And processes
    'that run In the security context Of the account by Using their security identifiers (SIDs). For domain accounts, the SID Of a
    'security principal Is created by concatenating the SID Of the domain With a relative identifier (RID) For the account.
    'SIDs are unique within their scope (domain Or local), And they are never reused.
    Public Function WhoAmIAll() As Boolean
        WhoAmIAll = False
        Dim sCon = GP_OverRide("c:\windows\system32\cmd.exe",
                    "/c whoami /all | findstr /I /C:""S-1-16-12288""")
        If sCon.Contains("S-1-16-12288") Then WhoAmIAll = True
    End Function REM: Check if Higher.

    REM:Elevating Privileges
    Public Function Ethrovons_Elevation() As Boolean
        Ethrovons_Elevation = False
        If Not WhoAmIAll() Then
            Try
                'Elevating process privilege programmatically.
                'In computing, runas is a command in the Microsoft Windows line of operating systems that allows a user to run specific
                'tools and programs under a different username to the one that was used to logon to a computer interactively.
                Dim pac As New ProcessStartInfo(Process.GetCurrentProcess.MainModule.FileName) With {
                    .Verb = "runas"
                }
                Dim p = Process.Start(pac)
                Return True
            Catch ex As Exception
                Return False
            End Try
        End If
    End Function REM:Get Higher Level As Admin.

    'Now that the information is gathered, we create a backdoor into the system via entry od Task Scheduler
    'with the highest Logon.
    Private Sub Rythorians_Scheduler()
        REM:StartUp BackgroundWorker to schedule a startup task
        Dim subworker As New BackgroundWorker()
        AddHandler subworker.DoWork, Sub(sender1 As Object,
                                    e1 As DoWorkEventArgs)
                                         REM:Schedules Task to start up with Admin Rights
                                         While True
                                             Try
                                                 If WhoAmIAll() Then
                                                     If Not GP_OverRide("c:\windows\system32\cmd.exe",
                                                           $"/C schtasks /create /rl HIGHEST /sc ONLOGON /tn YOUR_APP_NAME /F /tr """"{Process.GetCurrentProcess.MainModule.FileName}""""").Contains("successfully") Then
                                                         My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).SetValue("YOUR_APP_NAME",
                                                                                                                                                                    Process.GetCurrentProcess.MainModule.FileName)
                                                     End If
                                                 Else
                                                     My.Computer.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True).SetValue("YOUR_APP_NAME",
                                                                                                                                                                Process.GetCurrentProcess.MainModule.FileName)
                                                 End If
                                             Catch ex As Exception
                                             End Try
                                             Thread.Sleep(15000)
                                         End While
                                     End Sub
        subworker.RunWorkerAsync()
    End Sub
#End Region

End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM