Advertisement
xs4eyes

Disable-netadapter

May 23rd, 2016
565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4.     ##############################
  5.     # Disable Net adapter        #
  6.     # Updated on 06/09/2016      #
  7.     #                            #
  8.     #                            #
  9.     ##############################
  10.  
  11.     #####################################################################################################
  12.     #                                 Instructions!!                                                    #
  13.     #                                                                                                   #
  14.     #  https://community.spiceworks.com/how_to/100368-cryptolocker-canary-detect-it-early               #
  15.     #                                                                                                   #
  16.     # In this setup select the option to enable event log.                                              #
  17.     #                                                                                                   #
  18.     # Setup a task schedule to kick off this script with the options to run                             #
  19.     # C:\Windows\system32\Windowspowershell\v1.0\powershell.exe                                         #
  20.     # with arguments                                                                                    #
  21.     # -noprofile -executionpolicy bypass -command c:\disablenetadapter.ps1                              #
  22.     #                                                                                                   #
  23.     #                                                                                                   #
  24.     #####################################################################################################
  25.  
  26.  
  27.  
  28.     ######################################################################################################
  29.     #                              To Update the Canary List                                             #
  30.     #    function update-canary{                                                                         #
  31.     #  [CmdletBinding()]                                                                                 #
  32.     #  Param(                                                                                            #
  33.     #    $Export = 'Location to export',                                                                 #
  34.     #    $Backup = 'Backup last list,                                                                    #
  35.     #    $FileGroup = '"CryptoLocker Canary"',                                                           #
  36.     #    $Server = 'Server',                                                                             #
  37.     #    $NewList = 'Location of list'                                                                   #
  38.     #                                                                                                    #
  39.     #  )                                                                                                 #
  40.     #                                                                                                    #
  41.     #  $VerbosePreference = 'continue'                                                                   #
  42.     #  #" export a copy of the current file for backup                                                   #
  43.     #  Write-Verbose 'Making copy of current backup and replacing the older one.'                        #
  44.     #  Move-Item $Export $Backup -force                                                                  #
  45.     #                                                                                                    #
  46.     #  # Create new backup of the filegroup                                                              #
  47.     #  Write-Verbose 'Creating a new backup before changes.'                                             #
  48.     #  filescrn.exe filegroup export /file:$Export /filegroup:"$FileGroup" /remote:$server               #
  49.     #                                                                                                    #
  50.     #  # Get the content of the updated list                                                             #
  51.     #  Write-Verbose 'Getting the content of the new list.'                                              #
  52.     #  $canaryPipe = (Get-Content "$NewList") -join '|' -replace (' ') -replace ".$"                     #
  53.     #                                                                                                    #
  54.     #                                                                                                    #
  55.     #  # push the update to the FSRM Group to update the list                                            #
  56.     #  Write-Verbose 'Updating the list on the FSRM.'                                                    #
  57.     #  filescrn.exe filegroup modify /filegroup:"$FileGroup" /members:""$canaryPipe"" /remote:$Server    #
  58.     #                                                                                                    #
  59.     #                                                                                                    #
  60.     #  Write-Verbose 'Finished'                                                                          #
  61.     #                                                                                                    #
  62.     #                                                                                                    #
  63.     #  }                                                                                                 #
  64.     #                                                                                                    #
  65.     #    Update-canary                                                                                   #
  66.     ######################################################################################################
  67.    
  68.     [cmdletbinding()]
  69.     param(  
  70.       $domain = 'Domain\', #includes the '\'
  71.       $ToEmail = 'towho@blah.com',
  72.       $FromEmail = 'fromwho@blah.com',
  73.       $SMTPServer = 'smtp.server',
  74.       $VerbosePreference = 'Continue', # "SilentlyContinue"  # default value Use  - write-verbose
  75.       $date = (get-date -Format MMddyy),
  76.       $EventLogPC = 'Server',
  77.       $onlinePCtxt = 'C:\onlinepc.txt',
  78.       $QUserlist = 'c:\quser.csv'
  79.     )
  80.  
  81.     Write-Verbose 'Getting the user from the event log'
  82.  
  83.     $user = Get-EventLog -ComputerName $EventLogPC -Newest 1 -log application -EntryType warning -source SRMSVC | Select-Object message
  84.  
  85.     $user = $user.message.Replace($domain,'')
  86.  
  87.     ###############################################################
  88.  
  89.  
  90.     #  Email sent to notify that the script is being run. Just one more notification that its happening.
  91.     Write-Verbose 'Sending email to notify of running of script'
  92.  
  93.     Send-MailMessage -To $ToEmail -From $FromEmail -Subject "$user Crypto Disable Net Adapter!" -Body "disablenetadapter.ps1 is being run against $user" -SmtpServer $SMTPServer
  94.  
  95.     # The popup message on remote machine
  96.  
  97.     $message = "Attention $user! Your network connection has been disabled due to a possible virus infection! Do NOT attempt to re-enable it. Contact I.T immediately!"
  98.  
  99.  
  100.     ##############################################################
  101.     Write-Verbose 'Running pre-populated list'
  102.     # Run against pre-populated list
  103.     $pc = Import-Csv $QUserlist | Where-Object username -like "$user" | Select-Object -ExpandProperty pscomputername -Unique
  104.     if ($pc -ne $null){
  105.       Invoke-WmiMethod -Class win32_process -ComputerName $pc -Name create -ArgumentList "c:\windows\system32\msg.exe * $message"
  106.  
  107.       $ip = (Test-Connection $pc -count 1)
  108.  
  109.       Get-WmiObject -Class win32_networkadapter -computerName $pc -filter 'NetConnectionStatus = 2' | foreach {$_.disable()}
  110.  
  111.       $test = Test-Connection $pc -quiet
  112.       if($test -eq $false ){
  113.         Send-MailMessage -To "$ToEmail" -From "$FromEmail" -Subject "$user Crypto Disable Net Adapter!" -Body "The Net Adapter has been disabled on $pc using the pre-populated quser.exe information from past logons. The 'Test-connection' returned 'False' so this PC should not be on the network. The script will continue to run and disable any other computers that $user is logged on." -SmtpServer "$SMTPServer"
  114.       }
  115.     }
  116.     Write-Verbose 'The script is still running'
  117.  
  118.  
  119.     <#############################################################
  120.  
  121.         Disable the network adapter of the selected user
  122.  
  123.     #############################################################>
  124.  
  125.     # Set the username to disable
  126.  
  127.     #Get list of online computers
  128.     Write-Verbose 'Getting the list of online pcs'
  129.     clear-content $onlinePCtxt
  130.  
  131.     Get-ADComputer -Filter * |
  132.  
  133.     # Test each computer in the AD list for response and Store that list
  134.  
  135.     ForEach-Object {
  136.  
  137.       $rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet
  138.  
  139.       IF($rtn -match ‘True’) {$_.dnshostname | Out-File $onlinePCtxt -Append
  140.       }
  141.  
  142.     }
  143.  
  144.     $onlinePC = Get-Content $onlinePCtxt
  145.  
  146.     #Find the users that are logged in on the online pc's
  147.     Write-Verbose 'Running quser.exe to get the logged in users'
  148.  
  149.     $quser = Invoke-Command $onlinePC {(quser.exe) -replace '\s{2,}', ',' | ConvertFrom-Csv}
  150.     $quser | Export-Csv $QUserlist -NoTypeInformation
  151.  
  152.     #Get the computer name that the user is logged into
  153.  
  154.     $pc = $quser | Where-Object USERNAME -Like $user | Select-Object -Unique pscomputername
  155.  
  156.     #Popup message on the disabled pc
  157.     Write-Verbose 'Sending popup message to the offending machine'
  158.  
  159.     function Send-Message{
  160.  
  161.       Invoke-WmiMethod -Class win32_process -ComputerName $pc -Name create -ArgumentList "c:\windows\system32\msg.exe * $message"
  162.     }
  163.  
  164.     Send-Message
  165.  
  166.  
  167.     #Disable any connected net adapter
  168.     Write-Verbose 'Disabeling the net adapter on the offending machine'
  169.  
  170.     Get-WmiObject -Class win32_networkadapter -computerName $pc -filter 'NetConnectionStatus = 2' | foreach {$_.disable()}
  171.  
  172.     $test = Test-Connection $pc -quiet
  173.  
  174.     Write-Verbose 'Sending email to notify the script complete and the results'
  175.  
  176.     Send-MailMessage -To "$ToEmail" -From "$FromEmail" -Subject "$user Crypto Disable Net Adapter!" -Body "DisableNetAdapter.ps1 was    run on user $user on $pc. The final test-connection results came back as $test, If 'False' the disable was Successful. Please take action now to prevent further spread." -SmtpServer "$SMTPServer"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement