Skip to main content

Auditing Inbox rules (and looking for hidden rules) with EWS in OnPrem Exchange

 After the events of the last weeks around the latest zero day vulnerabilities in Exchange  and once you've finished cleaning up any back doors that may have been left on servers its a good idea to review some other less known but established ways bad actors may hide persistent access within Mailboxes. One of these are Inbox Rules (but Mail Flow rules could also be used) and a more advanced method is the hidden Inbox rule exploit that was first talked about https://blog.compass-security.com/2018/09/hidden-inbox-rules-in-microsoft-exchange/ and I covered it in https://gsexdev.blogspot.com/2019/05/audting-inbox-rules-with-ews-and-graph.html and somebody else https://mgreen27.github.io/posts/2019/06/09/O365HiddenRules.html there are a number of tools and techniques around detecting these types of rule but are all focused more toward Office365 as that was where at the time this exploit was being mostly employed. In my post at the time I modified the Microsoft script https://github.com/gscales/O365-InvestigationTooling/blob/master/Get-AllTenantRulesAndForms.ps1 so it would include the PidTagRuleMsgProvider property in its final report so you could easy spot any null, empty or custom values that would effectively hide rules from being enumerated via other more conventional methods. Because this tool was designed for o365 it can't easily be run against onPrem Exchange so I've put together a modified version that can be and posted it up here.  https://github.com/gscales/Powershell-Scripts/blob/master/EWShiddenRuleEnum.ps1  I've retained the funky Encoded compressed version of the EWS Managed API dll that the original used and extended some of the properties to include things like the display-name of the user and the last modified date time of the rule object. To use this script you need to have EWS Impersonation enabled for the user you either pass into the script or the user the script is running under. https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-configure-impersonation

Mostly the changes to the script involved (other then cleanup) where around the remote power shell differences (this script will try to open a remote ps connection if one isn't already established) and passing in the hostname of the EWS endpoint.

To use this script first import the script module

Import-module ./EWShiddenRuleEnum.ps1

Then 

Invoke-EnumerateAllInboxRules

parameters that can/must be used

EWSHostName this is the EWS endpoint hostname the script doesn't use autodiscover so if your not running directly on the destination server you must pass this in

ExchangePSHostName this is the servername that the Remote powershell session will be open against if your not running directly on the destination server you must pass this in

Credentials are the credentials that will be used in EWS to connect and impersonate mailboxes, the remote powershell credentials used are the running users. (credentials can be entered in either UPN format or downlevel format domain\username and this can be environment dependent as to what works)

Filter this allows you to filter the Get-Mailbox query with any valid filter you would normally use in the PowerShell eg Invoke-EnumerateAllInboxRules -filter '{servername -eq hostname}'

The script will produce a CSV report of rules and forms in the current directory, you can open this in Excel and apply filters to make it easy to read and interpret. 

If you don't go down the path of rebuilding Exchange servers that where exploited make sure you also look at any Transport Agents you have installed and verify the assemblies, its a a lesser known method of persistent access but it has been used before https://www.bankinfosecurity.com/researchers-spies-exploit-microsoft-exchange-backdoor-a-12459 to some success.   




Popular posts from this blog

Export calendar Items to a CSV file using EWS and Powershell

Somebody asked about this last week and while I have a lot of EWS scripts that do access the Calendar I didn't have a simple example that just exported a list of the Calendar events with relevant information to a CSV file so here it is. I've talked on this one before in this howto  but when you query the calendar folder using EWS you need to use a CalendarView which will expand any recurring appointments in a calendar. There are some limits when you use a calendarview in that you can only return a maximum of 2 years of appointments at a time and paging will limit the max number of items to 1000 per call. So if you have a calendar with a very large number of appointments you need to break your query into small date time blocks. In this example script I'm just grabbing the next 7 days of appointments if you want to query a longer period you need to adjust the following lines (keeping in mind what I just mentioned) #Define Date to Query $StartDate = (Get-Date) $EndDate

Downloading a shared file from Onedrive for business using Powershell

I thought I'd quickly share this script I came up with to download a file that was shared using One Drive for Business (which is SharePoint under the covers) with Powershell. The following script takes a OneDrive for business URL which would look like https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication. The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like ./spdownload.ps1 ' https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filena

Writing a simple scripted process to download attachmentts in Exchange 2007/ 2010 using the EWS Managed API

Every complicated thing in life is made up of smaller simpler building blocks, when it comes to writing a script (or any code really) the more of these little building blocks you have to figure out the more the process of solving a problem can become bewildering. The Internet generally provides you with lots of half eaten sandwiches of information something someone else has taken a bite out but a lot of the time half done, and as with any code its usefulness declines over time as new and better API's and methods are derived. In this post I'm going to go through a simple scripted process that hopefully covers a few more of these smaller building blocks that you might face when asked to come up with a simple costless solution to perform an automated business function with a script. So the process im going to look at is one that comes up a lot and that is you have an Email that comes into to certain mailbox every day with a certain subject in my case "Daily Export" this
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.