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 ACTION_MOVE = 1
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
Else
Set CdoFolder = CdoFolders.GetNext
End If
Loop
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
objRules.Update

' Log off and cleanup
objSession.Logoff

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  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

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

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