Skip to main content

Creating a Server Side rule to move suspect messages with inline gifs to another folder using Rule.dll

Recently there has been a large increase in the amount of image based spam being sent out and also an increase in the amount of SPAM making it though SPAM filters because of the methods being employed such as randomizing images and also modifying images so they are difficult for any OCR based spam filter to decode the text. Stopping this at Edge device is the ultimate goal of any decent Sys Admin but this continuing war between spammers and those that create the software that can fight spam (and the accountants that stop us buying said software) means that we have to deal with the stuff that makes it though any defenses we might have and the inevitable complaints from the end users that stem from this. I decided to see if I could make a rule that at least could move any of these image based emails into a separate folder mainly for my postmaster account which tends to get hammered.

Analyzing the basic image based spam message it consists of one inline gif image and a bunch of text. So to write a server rule with rule dll I needed to come up with a rule that would act on two basic logic constraints. Using the PR_TRANSPORT_MESSAGE_HEADERS property which stores the Entire header of message which includes information about the bodyparts of a message eg if it’s a html body part or a text bodypart or an attachment etc. So the rule looks at the message header and the first logic constraint looks to see if this message has any body parts that has a content type of image/gif then next logic condition which is a Bitwise NOT looks to see if this message has any body parts with have a content disposition set to attachment; which means they will actually be an attached file vs inline attachments. The two logic constraints get tied together via a Bitwise AND logic condition (eg the first condition is positive if there is a bodypart of image/gif and the second is positive is there are no attached files). The end result is a rule that will fire on any message that are received with one inline gif image.

BUT!!!! Before you go tearing off and wanting to look at using this on a users mailbox you should first consider this rule may generate a lot of false positives. For instance if someone has an inline gif in there signature this would cause the rule to fire. Some HTML based newsletters will also cause this to fire. For a postmaster account it seems to work pretty well it also seems to even be able to move image base spam messages that are located attached to NDR notification messages because the headers from the bounced message are still included in the NDR’s PR_TRANSPORT_MESSAGE_HEADERS prop.

The Code is setup to move these images into the Junk Email Folder you may however want to look at creating another folder like Potential Image Spam to hold messages that are moved by the rule.

This code requires the Rule.dll to be registered on the machine you’re running the script on if you have never used rule.dll you should read up about the restrictions around rules created this way.

I’ve put a download of this code here the code itself looks like.

const SUBSTRING = 1 ' Substring
const IGNORECASE = &H00010000 ' Ignore case
const B_NEZ = 2
const L_OR = 3
Const REL_EQ = 7
const MSG_ATTACH = 2
Const PR_Transport_Headers = &H007D001E

servername = "servername"
mailboxname = "user"

Set objSession = CreateObject("MAPI.Session")

objSession.Logon "","",false,true,true,true,servername & vbLF & mailboxname
Set objRules = CreateObject("MSExchange.Rules")
objRules.Folder = objSession.Inbox
Set objInbox = objSession.Inbox

Set CdoInfoStore = objSession.GetInfoStore
Set CdoFolderRoot = CdoInfoStore.RootFolder
Set CdoFolders = CdoFolderRoot.Folders

bFound = False
Set CdoFolder = CdoFolders.GetFirst
Do While (Not bFound) And Not (CdoFolder Is Nothing)
If CdoFolder.Name = "Junk E-mail" Then
bFound = True
Set CdoFolder = CdoFolders.GetNext
End If
Set ActionFolder = CdoFolder

Set importPropVal = CreateObject("MSExchange.PropertyValue")
importPropVal.Tag = PR_Transport_Headers
importPropVal.Value = " attachment;"

Set importPropCond = CreateObject("MSExchange.ContentCondition")
importPropCond.PropertyType = PR_Transport_Headers
importPropCond.Operator = SUBSTRING + IGNORECASE
importPropCond.Value = importPropVal

Set importPropVal1 = CreateObject("MSExchange.PropertyValue")
importPropVal1.Tag = PR_Transport_Headers
importPropVal1.Value = "image/gif"

Set importPropCond1 = CreateObject("MSExchange.ContentCondition")
importPropCond1.PropertyType = PR_Transport_Headers
importPropCond1.Operator = SUBSTRING + IGNORECASE
importPropCond1.Value = importPropVal1

Set logPropCond1 = CreateObject("MSExchange.LogicalCondition")
logPropCond1.Operator = 3
logPropCond1.Add importPropCond

Set logPropCond = CreateObject("MSExchange.LogicalCondition")
logPropCond.Operator = 1
logPropCond.Add importPropCond1
logPropCond.Add logPropCond1

' Create action
Set objAction = CreateObject("MSExchange.Action")
objAction.ActionType = ACTION_MOVE
objAction.Arg = ActionFolder

' Create new rule
Set objRule = CreateObject("MSExchange.Rule")
objRule.Name = "Gif Image Move Rule"

' Add action and assign condition
objRule.Actions.Add , objAction
objRule.Condition = logPropCond

' Add rule and update
objRules.Add , objRule

' Log off and cleanup

Set objRules = Nothing
Set objSession = Nothing
Set importProp = Nothing
Set importPropVal = Nothing
Set objAction = Nothing
Set objRule = Nothing

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

How to access and restore deleted Items (Recoverable Items) in the Exchange Online Mailbox dumpster with the Microsoft Graph API and PowerShell

As the information on how to do this would cover multiple posts, I've bound this into a series of mini post docs in my GitHub Repo to try and make this subject a little easier to understand and hopefully navigate for most people.   The Binder index is   The topics covered are How you can access the Recoverable Items Folders (and get the size of these folders)  How you can access and search for items in the Deletions and Purges Folders and also how you can Export an item to an Eml from that folder How you can Restore a Deleted Item back to the folder it was deleted from (using the Last Active Parent FolderId) and the sample script is located

Using the MSAL (Microsoft Authentication Library) in EWS with Office365

Last July Microsoft announced here they would be disabling basic authentication in EWS on October 13 2020 which is now a little over a year away. Given the amount of time that has passed since the announcement any line of business applications or third party applications that you use that had been using Basic authentication should have been modified or upgraded to support using oAuth. If this isn't the case the time to take action is now. When you need to migrate a .NET app or script you have using EWS and basic Authentication you have two Authentication libraries you can choose from ADAL - Azure AD Authentication Library (uses the v1 Azure AD Endpoint) MSAL - Microsoft Authentication Library (uses the v2 Microsoft Identity Platform Endpoint) the most common library you will come across in use is the ADAL libraries because its been around the longest, has good support across a number of languages and allows complex authentications scenarios with support for SAML etc. The
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.