Friday, November 10, 2006

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