Tag Archives: powershell

Monitor Scheduled Tasks on Windows Server

Scheduled Tasks are a great way to run scripts at specified time intervals. Similar to Unix cron. Because it’s such a powerful tool, it can be easy to abuse by programs that install their own schedule tasks. Most times without any transparency to the user that a task is being created. It’s also a potential target for malware to install persistent tasks that go unnoticed.

On managed servers, I want transparency into which tasks are installed. I created a powershell script with an array of “whitelisted” tasks, then created a scheduled tasks to check the name of the scheduled tasks installed. The results are sent to InfluxDB and If a task is not on my white list, an alert is sent with Grafana.

The powershell script and scheduled task can be found here.

When first installed, it reports the total number of scheduled tasks, and total number of non-Microsoft tasks. By default, I ignore Microsoft tasks installed on a new install.

In this scenario you can see an alert has been triggered, because there is an Unapproved task, and to the right, it shows the name of the task, User_Feed_ ...

Now you can log into the computer and check the task to decided if it needs added to your white list or deleted.

Once the task has been deleted, the alarm is cleared and all tasks report as Approved

Azure: Adding A Network Security Rule To An Existing Network Security Group

This has caused me a lot of pain, I hope it saves someone else.

One of the things I do after creating a new VM in Azure is add rules to the NSG. This can be mundane and error prone, especially if you have a lot of rules. It’s much easier to deploy your VM with the basic RDP rule and then add your custom rules later. It’s even easier if you can script it. I prefer to use Powershell and the the AZ module on macOS, that way I don’t have to switch computers.

One of the issues I had was adding a NSG rule where “-SourceAddressPrefix” had multiple values. I can easily do this in the GUI by adding the rule manually.

(The IP addresses are just an example to show an IP address and a CIDR notation)

Screen Shot 2018-12-27 at 7.25.42 PM

If I click add here, I create a new rule. But I want to do this with Powershell. But every time I run my script, I get a 400 error on “SourceAddressPrefix”, telling me it’s a bad request. To make it worse, Microsoft doesn’t even offer an example in the official documentation of how to add more than 1 IP or CIDR value.

It took a lot of just trial and error, but I finally got it. If you’re passing more than 1 value to “-SourceAddressPrefix”, it expects that the “,” is NOT in quotations.

So where it feels natural to create a variable like this:

$officeWhiteList = "8.8.8.8,10.1.1.0/24"

It’s wrong. Your “,” is inside the quotes. If you change it to this:

$officeWhiteList = "8.8.8.8","10.1.1.0/24"

It works! Some simple documentation or better error messages would have saved me a ton of time. Here’s my full example to add a network security rule to an existing NSG:

#---------------------------------------------------------------------------
# Summary:
# Add an Azure Network Security Group Rule to an existing NSG

#---------------------------------------------------------------------------
$subscriptionToUse = "12345678-1234-1234-1234-123456789012" #just an example
$officeWhiteList = "8.8.8.8","10.1.1.0/25" #just an example
$rgName = "Resource-Group"
$nsgName = "Resource-Group-nsg"
$customServerRule = "allow-custom-server"
$customServerPort = 15001

# Select subscription ------------------------------------------------------
Set-AzureRmContext -SubscriptionId $subscriptionToUse

# Get the NSG resource -----------------------------------------------------
$resource = Get-AzureRmResource | Where-Object `
{$_.ResourceGroupName `
–eq $rgName `
-and $_.ResourceType `
-eq "Microsoft.Network/networkSecurityGroups"}

$nsg = Get-AzureRmNetworkSecurityGroup `
-Name $nsgName `
-ResourceGroupName $rgName

# Add customServerPort --------------------------------------------------------
$nsg | Add-AzureRmNetworkSecurityRuleConfig `
-Name $customServerRule `
-Description $customServerRule `
-Access Allow `
-Protocol * `
-Direction Inbound `
-Priority 301 `
-SourceAddressPrefix $officeWhiteList `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange $customServerPort

# Save changes to NSG ------------------------------------------------------
$nsg | Set-AzureRmNetworkSecurityGroup

If you want to add more than 1 rule, just add another “$nsg | Add-AzureRmNetworkSecurityRuleConfig” statement. When you’re done, make sure the last statement is “$nsg | Set-AzureRmNetworkSecurityGroup“, this saves your changes to the NSG