Tuesday, November 16, 2010

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

2 comments:

Kevin Buchan said...

I am on Exchange 2007 and am being asked to report on 3rd party custom information that is accessible via Associated Items. I am not an expert on EWS (learned everything I know from these blogs) and I have wasted most of my allotted time trying to crack this nut with EWS.

What would you suggest I use to get this information? The Redemption library?
Any tips would be welcome.
Thanks!!
kevin.buchan@troutmansanders.com

Glen Scales said...

Yeah on 2007 EWS wont work for you so you need to either using MAPI or WebDAV. Redemption is great library and is going to save you a lot of time but WebDAV should work as well although it will be harder to write code for

Cheers
Glen