Skip to main content

Breadcrumb auditing in Exchange 2010 with EWS and Powershell

Great strides have been made since Exchange 2007 SP2 with the introduction of more advanced audit levels into Exchange which have again improved in Exchange 2010 and again with 2010 SP1. Auditing however relies on a few things that after the fact of an event you may have wished to audit (or more likely a Superior wanting to know what mailboxes where accessed) . But like footprints in the sand when you walk along the beach many of the actions we do in Outlook do leave marks that the majority of people may not realize. One such mark in the sand that happens in your mailbox when using Outlook 2010 or OWA 2010 is when you access another users folder eg calendar, contacts etc is a wunderbar link gets added to the Common Views Root folder.

What you can do with this little bit of information is deduce what other mailboxes a particular user has been access by enumerating the wunderbar links in the users common view folder. These wunderbar links are FAI (folder associated Items) so live in the FAI collection of that particular folder.

To do this with EWS requires the use of some of the nice new features of EWS in Exchange 2010 such as the ability to access the associated Folder Items collection where these WunderBar Shortcuts are stored.

To access the associated folder Items collection in EWS you just need to change the Traversal type to Associated (note this wont work on 2007).

The Wunderbar Items themselves have a bunch of extended properties that contain information that is important to see what mailbox and folder this users was accessing. A full list of the shortcut properties can be found in the following Exchange protocol document http://msdn.microsoft.com/en-us/library/cc463899%28EXCHG.80%29.aspx. The Ones I've used in this script are

PidTagWlinkEntryId EntryID the ShortCut points to
PidTagWlinkStoreEntryId StoreEntryID the ShortCut points to
PidTagWlinkAddressBookEID Like the above but contains just the users info
PidTagWlinkFolderType Contains the GUID to let you know what type of folder it is
PidTagWlinkGroupName The name of the group

I've put this altogether is a powershell script that use the new EWS 1.1 Managed API beta (which allows you to access the associated folders collection). To use this script you need to customize the the following variable the output of this script is a html report.

$casserverName = "casservername"
$userName = "username"
$password = "password"
$domain = "domain"
$Mailboxname = "user@domain.com"
$fileName = "c:\report.htm"

I've put a download of this script here the script itself looks like.

$casserverName = "casservername"
$userName = "username"
$password = "password"
$domain = "domain"
$Mailboxname = "user@domain.com"
$fileName = "c:\report.htm"

$rptCollection = @()

function ReturnFolderType($guid){
switch($guid){
"0C78060000000000C000000000000046" { return "MailFolder"}
"0278060000000000C000000000000046" { return "Calendar" }
"0178060000000000C000000000000046" { return "Contacts" }
"0378060000000000C000000000000046" { return "Tasks"}
"0478060000000000C000000000000046" { return "Notes"}
"0878060000000000C000000000000046" { return "Journal"}
}

}



## Code From http://poshcode.org/624
## Create a compilation environment
$Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler=$Provider.CreateCompiler()
$Params=New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable=$False
$Params.GenerateInMemory=$True
$Params.IncludeDebugInformation=$False
$Params.ReferencedAssemblies.Add("System.DLL") | Out-Null

$TASource=@'
namespace Local.ToolkitExtensions.Net.CertificatePolicy{
public class TrustAll : System.Net.ICertificatePolicy {
public TrustAll() {
}
public bool CheckValidationResult(System.Net.ServicePoint sp,
System.Security.Cryptography.X509Certificates.X509Certificate cert,
System.Net.WebRequest req, int problem) {
return true;
}
}
}
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly

## We now create an instance of the TrustAll and attach it to the ServicePointManager
$TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy=$TrustAll

## end code from http://poshcode.org/624

$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)
$uri=[system.URI] ("https://" + $casserverName + "/ews/exchange.asmx")
$service.Url = $uri
$service.Credentials = New-Object System.Net.NetworkCredential($username,$password,$domain)
$rfFolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
$fview = New-Object Microsoft.Exchange.WebServices.Data.FolderView(1000)
$Sfir = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName, "Common Views")
$findfldResults = $Service.FindFolders($rfFolderid,$Sfir,$fview)
if ($findfldResults.Folders.Count -eq 1) {
$findfldResults.Folders[0].DisplayName
$PidTagWlinkEntryId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26700, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkStoreEntryId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26702, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkAddressBookEID = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26708, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkFolderType = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26703, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagWlinkGroupName = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(26705, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$Propset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$Propset.add($PidTagWlinkEntryId)
$Propset.add($PidTagWlinkStoreEntryId)
$Propset.add($PidTagWlinkAddressBookEID)
$Propset.add($PidTagWlinkFolderType)
$Propset.add($PidTagWlinkGroupName)
$iv = New-Object Microsoft.Exchange.WebServices.Data.ItemView(10000)
$iv.PropertySet = $Propset
$iv.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Associated
$fiResults = $findfldResults.Folders[0].FindItems($iv)
foreach($itItem in $fiResults.Items){
$itItem.Subject
$WlinkStoreEntryId = $null
$rptobj = "" | select MailboxName,LinkName,GroupName,FolderType,DateCreated,LastModified
$lnLegDN = ""
if ($itItem.TryGetProperty($PidTagWlinkStoreEntryId,[ref]$WlinkStoreEntryId))
{

$leLegDnStart = 0
for ($ssArraynum = (([byte[]]$WlinkStoreEntryId).Length - 2); $ssArraynum -ne 0; $ssArraynum--)
{
if (([byte[]]$WlinkStoreEntryId)[$ssArraynum] -eq 0)
{
$leLegDnStart = $ssArraynum;
$lnLegDN = [System.Text.ASCIIEncoding]::ASCII.GetString(([byte[]]$WlinkStoreEntryId), $leLegDnStart + 1, (([byte[]]$WlinkStoreEntryId).Length - ($leLegDnStart + 2)));
$ssArraynum = 1;
}
}

}
else{
if ($itItem.TryGetProperty($PidTagWlinkAddressBookEID,[ref]$WlinkStoreEntryId))
{

$leLegDnStart = 0
$lnLegDN = ""
for ($ssArraynum = (([byte[]]$WlinkStoreEntryId).Length - 2); $ssArraynum -ne 0; $ssArraynum--)
{
if (([byte[]]$WlinkStoreEntryId)[$ssArraynum] -eq 0)
{
$leLegDnStart = $ssArraynum;
$lnLegDN = [System.Text.ASCIIEncoding]::ASCII.GetString(([byte[]]$WlinkStoreEntryId), $leLegDnStart + 1, (([byte[]]$WlinkStoreEntryId).Length - ($leLegDnStart + 2)));
$ssArraynum = 1;
}
}
}

}
if($lnLegDN -ne ""){
$ncCol = $service.ResolveName($lnLegDN)
$mbox = ""
foreach ($NameResolution in $ncCol)
{
$mbox = $NameResolution.Mailbox.Address
}
$FolderType = $null
if ($itItem.TryGetProperty($PidTagWlinkFolderType,[ref]$FolderType)){
$hexArr = $FolderType | ForEach-Object { $_.ToString("X2") }
$hexString = $hexArr -join ''
$rptobj.FolderType = ReturnFolderType($hexString)
$hexString

}
$GroupName = $null
if ($itItem.TryGetProperty($PidTagWlinkGroupName,[ref]$GroupName)){
$rptobj.GroupName = $GroupName
}
$rptobj.MailboxName = $mbox
$rptobj.LinkName = $itItem.Subject
$rptobj.DateCreated = $itItem.DateTimeCreated
$rptobj.LastModified = $itItem.LastModifiedTime
$rptCollection += $rptobj}
}
}
$rptCollection
$tableStyle = @"
<style>
BODY{background-color:white;}
TABLE{border-width: 1px;
border-style: solid;
border-color: black;
border-collapse: collapse;
}
TH{border-width: 1px;
padding: 10px;
border-style: solid;
border-color: black;
background-color:#66CCCC
}
TD{border-width: 1px;
padding: 2px;
border-style: solid;
border-color: black;
background-color:white
}
</style>
"@

$body = @"
<p style="font-size:25px;family:calibri;color:#ff9100">
$TableHeader
</p>
"@

$rptCollection | ConvertTo-HTML -head $tableStyle –body $body | Out-file $fileName

Popular posts from this blog

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
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.