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

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 Gr...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.

Export calendar Items to a CSV file using Microsoft Graph and Powershell

For the last couple of years the most constantly popular post by number of views on this blog has been  Export calendar Items to a CSV file using EWS and Powershell closely followed by the contact exports scripts. It goes to show this is just a perennial issue that exists around Mail servers, I think the first VBS script I wrote to do this type of thing was late 90's against Exchange 5.5 using cdo 1.2. Now it's 2020 and if your running Office365 you should really be using the Microsoft Graph API to do this. So what I've done is create a PowerShell Module (and I made it a one file script for those that are more comfortable with that format) that's a port of the EWS script above that is so popular. This script uses the ADAL library for Modern Authentication (which if you grab the library from the PowerShell gallery will come down with the module). Most EWS properties map one to one with the Graph and the Graph actually provides better information on recurrences then...
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.