PowerShell: Quickly finding source of Brute Force attack on O365 Tenant

A small PowerShell Script to quickly find out source IPs in case of a brute force attack on O365 Infra. This can save manual efforts and can improve turnaround time to mitigate the issue in Infrastructures which still not using MFA or ExtraNetLockOut. This is just a basic code. One can set email alerts basis the thresholds or even can write a custom event on any of the server to be picked by SCOM or OMS.

Pre-requisites: Auditing should be enabled on Federation servers


# To investigate Account lockouts on Federation Servers
# Author: Nitish Kumar
# v1.0

# Thresholds
$Period = 1
$LockedAccountsThreshold = 50

#federation Servers
$FServers = ("XYZ01", "ABC01") # Replace the name with actual federation server names
$LastEvents = @()
$LockEvents = @()

#collect events from all federation servers
ForEach($Server in $FServers){
$Events = Get-WinEvent -Computername $Server -FilterHashtable @{LogName='Security';ID=411; StartTime=$((get-date).addhours(-($Period)))}| ?{$_.message -like "*Locked*"} | Select-object MachineName, TimeCreated, Message
$LastEvents += $Events
}

ForEach($Ev in $LastEvents){
$Array = $Ev.Message.split("`n")
$a = $Array[13].split("@")
$Temp = New-Object -TypeName PSObject -Property @{
FederationServer = $Ev.MachineName
TimeWritten = $Ev.TimeCreated
IPAddress = [string]$Array[10]
UserAccount = $a[0]
}
$LockEvents += $Temp
}

# Find unique users from the lockouts
$LockedAccounts = $LockEvents.UserAccount | sort-object -unique
write-host "A total $($LockedAccounts.count) Accounts locked out in last $($Period) hours: `n$($LockedAccounts -join ",")" -foregroundcolor RED

$Logfile = "c:\temp\Lockouts_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv"
$LockEvents | Select-object FederationServer, TimeWritten, UserAccount, IPAddress | Export-csv -nti $Logfile
Advertisements

PowerShell script for DNS Inventory

Here goes the PowerShell script for DNS Inventory for AD Integrated DNS zones. This script needs to be altered to set variable for DC Name and once ran, it would fetch details from each zone and would save it in separate files.

Updated the code to provide option to dynamic selection of DNS Server. Thanks for input from @singhalyogi22

Note: Skipping SOA records etc to avoid complexity

Continue reading →

PowerShell Script for patching Domain Servers remotely

In line with the last post on File Server inventory, here comes the another script to perform patching for Domain Servers from a remote machine.

PowerShell already has methods to perform patching on any server, where we can list what all patches are available and create a downloader to download and install them all, but a limitation to the capability is, the same can not be done remotely. As a workaround for that, we would be creating schedule task on the remote machine via the script and performing the patching via triggering that schedule task.

It would involve two scripts InstallPatches.ps1 and PatchServer.ps1, whose code I would be listing below. It would make use of c:\temp directory and would be keeping logs and scripts in the same location. Also we would need any shared location, where we would keep a copy of InstallPatches.ps1, which then would be copied dynamically over the servers during patching. I have kept script source path as “\\ABCXYZ\sources\installpatches.ps1” while it can be changed as per your environment.

Continue reading →

PowerShell Script for File Share inventory of all File Servers

Managing a large infra never happens to be easy without right kind of tools and tools usually come with cost. In past Unix users used to taunt Windows users on this point as they used to enjoy many open source products and scripts which Windows users weren’t privy of. Things changed a lot since PowerShell.

Presenting you a script to have a size and permission inventory of all your shares on all file servers. Below would be the pre-requisites:

1. Names of all file servers.
2. All the file servers should have WinRM enabled (Windows 2012 servers have it on by default and for Windows 2008, winrm quickconfig may do the trick).
3. Obviously Local Administrator permissions on all File Servers, which should have permissions on shares as well.

After that just change the variable $RemoteComputers value with the name of file servers in place of dummy values placed in form of array right now. That’s it.

This script lists names of all servers (provided by you), shares on them, NTFS permissions, Size in MB, Last modified date and Last accessed date.

Continue reading →

In embrace of sea and greenery of South-Memoirs from day 7, 8 and 9

IMG_20170630_085006

Munnar was the only location, where we were scheduled to stay for two nights means full one day for the place as the day next, we were to leave as early as possible to reach Bangalore in time.

When we woke up, it was raining but nevertheless, we got ready and then by 7.30 AM, we would have been on nearby restaurant for a heavy breakfast. After which we started driving after 8.30 AM. This time we had hired a guide to drive with us and show us around @ 500Rs.

IMG_20170630_085004

It continued to rain though we had hopes that it would settle till the time we reach to the viewpoints and luckily it happened that way.

Continue reading →

In embrace of sea and greenery of South-Memoirs from day 5 and 6

DSCN9292

We all waited for morning to have more fun on beach, elders and kids alike. The first job in morning to get ready after bath etc and head to beach with sand castle tools.

DSCN9283

DSCN9319

We must have reached the beach by 7 AM. The lighthouse wasn’t gonna open before 10AM which means it wouldn’t cut into our schedule. It was all about the time on beach now and mild showers were worrying us.

Continue reading →