IMAP Spam Begone

Version 7 (Thomas Lecavelier, 03/03/2010 01:28 pm)

1 1
h1. IMAP Spam Begone
2 1
3 2 Thomas Lecavelier
isbg is a script that makes it easy to scan an IMAP inbox for spam using SpamAssassin and get your spam moved to another folder.
4 1
5 4 Thomas Lecavelier
Unlike the normal mode of deployments for SpamAssassin, isbg does not need to be involved in mail delivery, and can run on completely different machines to where your mailbox actually is. So this is the perfect tool to take good care of your ISP mailbox without having to leave it.
6 1
7 6 Thomas Lecavelier
* Current *stable* version is 0.99 - 3rd march 2010
8 1
9 2 Thomas Lecavelier
There is a Yahoo group - "imapspambegone":http://tech.groups.yahoo.com/group/imapspambegone/ for discussion and questions.                                                                                          
10 1
11 1
12 3 Thomas Lecavelier
{{TOC}}
13 2 Thomas Lecavelier
14 2 Thomas Lecavelier
h1(#features). Features
15 2 Thomas Lecavelier
16 2 Thomas Lecavelier
* Works with all common IMAP servers
17 4 Thomas Lecavelier
* Works on Linux, MacOS X and Windows (even smartphones!)
18 2 Thomas Lecavelier
* Can do IMAP over SSL
19 2 Thomas Lecavelier
* Can remember your password
20 1
* Will work painlessly against multiple IMAP accounts and servers
21 1
* Is not involved in the mail delivery process, and so can run on any machine that can contact your IMAP server
22 1
* Highly configurable
23 1
* Sensible defaults so you don't have to do any configuring :-)
24 6 Thomas Lecavelier
* Compatibility with Python 2.4, 2.5, 2.6
25 6 Thomas Lecavelier
* Possibility to skip spam detection to stick only to the teach feature
26 6 Thomas Lecavelier
* Don’t fail when meeting horrible and bad formed mail
27 6 Thomas Lecavelier
* Lock file to prevent multiple instance to run at the same time (for cron jobs)
28 4 Thomas Lecavelier
29 4 Thomas Lecavelier
h2. New in 0.99
30 4 Thomas Lecavelier
31 4 Thomas Lecavelier
* Drastic performance enhancement (up to 1000x speeder!)
32 4 Thomas Lecavelier
* Lock file to prevent multiple instance to run at the same time (for cron jobs)
33 2 Thomas Lecavelier
* SSL fixed
34 2 Thomas Lecavelier
* Don’t fail when meeting horrible and bad formed mail
35 2 Thomas Lecavelier
* Remember mails teached as Spam and Ham
36 2 Thomas Lecavelier
* Automatically delete mail with very high spam score
37 2 Thomas Lecavelier
38 2 Thomas Lecavelier
h1(#bugs). Bugs
39 2 Thomas Lecavelier
40 5 Thomas Lecavelier
Well, it's a bug tracker, you know. So have a look to the "bug list":http://redmine.ookook.fr/projects/isbg/issues?query_id=1
41 2 Thomas Lecavelier
42 5 Thomas Lecavelier
You can request or discuss feature there, too.
43 2 Thomas Lecavelier
44 2 Thomas Lecavelier
h1(#work). Does it work?
45 2 Thomas Lecavelier
46 2 Thomas Lecavelier
Yes, very well. It has been filtering my work email, some of which comes from a legacy email address that is from a spam unfiltered system. I run it as a cronjob every 15 minutes, and have had it doing that for 6 months. I get about 500 spams a month. Many other users have been using it for a variety of servers from a variety of platforms.
47 2 Thomas Lecavelier
There are no known outstanding bugs.
48 2 Thomas Lecavelier
49 2 Thomas Lecavelier
h1(#installation). Installation
50 2 Thomas Lecavelier
51 2 Thomas Lecavelier
Make sure you have SpamAssassin installed. This is described in spamassassin.org/dist/INSTALL.
52 2 Thomas Lecavelier
53 2 Thomas Lecavelier
SpamAssassin should be on your $PATH (it installs in /usr/bin by default)
54 2 Thomas Lecavelier
55 2 Thomas Lecavelier
Download isbg.py. You can rename it to anything you want, and make it executable (chmod 555 isbg.py). It is written in the Python scripting language. Python is installed by default on most Linux systems. You can can find out more about Python at www.python.org
56 2 Thomas Lecavelier
57 2 Thomas Lecavelier
Simply invoke it by whatever name you called the file.
58 2 Thomas Lecavelier
59 2 Thomas Lecavelier
h1(#firstRun). Your first run
60 2 Thomas Lecavelier
61 2 Thomas Lecavelier
h2. SpamAssassin
62 2 Thomas Lecavelier
63 2 Thomas Lecavelier
If you have never used SpamAssassin before, you'll probably be quite nervous about it being too good and taking out legitimate email, or not taking out enough spam. It has an easily adustable threshold to change how aggressive it is. Run the following command to create your preferences file.
64 2 Thomas Lecavelier
65 7 Thomas Lecavelier
<pre>
66 7 Thomas Lecavelier
$ spamassassin  </dev/null >/dev/null
67 1
Created user preferences file: /home/rogerb/.spamassassin/user_prefs
68 7 Thomas Lecavelier
</pre>
69 2 Thomas Lecavelier
70 1
You can then edit $HOME/.spamassassin/user_prefs and change the thresholds.
71 7 Thomas Lecavelier
72 7 Thomas Lecavelier
If you want to use the --learnspambox or --learnhambox, you'll have to [[configure your spamassassin]].
73 2 Thomas Lecavelier
74 2 Thomas Lecavelier
h2. isbg
75 2 Thomas Lecavelier
76 2 Thomas Lecavelier
The default behaviour of isbg is to not make any changes your Inbox unless you specify specific command line options. Consequently you can experiment without worry at the begining.
77 2 Thomas Lecavelier
78 2 Thomas Lecavelier
Your first step is to create a new folder to receive suspected spam. I use one named 'spam'.
79 2 Thomas Lecavelier
80 2 Thomas Lecavelier
Run isbg with the --help option to see what options are available:
81 2 Thomas Lecavelier
82 2 Thomas Lecavelier
bc.    $ isbg.py --help
83 2 Thomas Lecavelier
    isbg: IMAP Spam begone 0.97-26Mar03
84 2 Thomas Lecavelier
    All options are optional
85 2 Thomas Lecavelier
   
86 2 Thomas Lecavelier
      --imaphost hostname   IMAP server name [localhost]
87 2 Thomas Lecavelier
      --ssl                 Make an SSL connection to the IMAP server
88 2 Thomas Lecavelier
      --imapuser username   Who you login as [rogerb]
89 2 Thomas Lecavelier
      --imapinbox mbox      Name of your inbox folder [INBOX]
90 2 Thomas Lecavelier
      --spaminbox mbox      Name of your spam folder [INBOX.spam]
91 2 Thomas Lecavelier
      --maxsize numbytes    Messages larger than this will be ignored as they are
92 2 Thomas Lecavelier
                            unlikely to be spam [120000]
93 2 Thomas Lecavelier
      --noreport            Don't include the SpamAssassin report in the message
94 2 Thomas Lecavelier
                            copied to your spam folder
95 2 Thomas Lecavelier
      --flag                The spams will be flagged in your inbox
96 2 Thomas Lecavelier
      --delete              The spams will be marked for deletion from your inbox
97 2 Thomas Lecavelier
      --expunge             Cause marked for deletion messages to also be deleted
98 2 Thomas Lecavelier
                            (only useful if --delete is specified)
99 2 Thomas Lecavelier
      --verbose             Show IMAP stuff happening
100 2 Thomas Lecavelier
      --spamc               Use spamc instead of standalone SpamAssassin binary
101 2 Thomas Lecavelier
      --savepw              Store the password to be used in future runs
102 2 Thomas Lecavelier
      --nostats             Don't print stats
103 2 Thomas Lecavelier
      --exitcodes           Use different exitcodes (see doc)
104 2 Thomas Lecavelier
    (Your inbox will remain untouched unless you specify --flag or --delete)
105 2 Thomas Lecavelier
106 2 Thomas Lecavelier
h2. Do your first run.
107 2 Thomas Lecavelier
108 2 Thomas Lecavelier
bc.  $ isbg.py --imaphost mail.example.com  --savepw
109 2 Thomas Lecavelier
IMAP password for rogerb@mail.example.com: 
110 2 Thomas Lecavelier
111 2 Thomas Lecavelier
The amount of time it takes will be proportional to the size of your inbox. You can specify --verbose if you want to see the gory details of what is going on.
112 2 Thomas Lecavelier
113 2 Thomas Lecavelier
You can now examine your spam folder and will see what spam was detected. You can change the SpamAssassin threshold in your user_prefs file it created earlier.
114 2 Thomas Lecavelier
115 2 Thomas Lecavelier
isbg remembers which messages it has already seen, so that it doesn't process them again every time it is run. If you are testing and do want it to run again, then remove the trackfile (default $HOME/.isbg-track*).
116 2 Thomas Lecavelier
117 2 Thomas Lecavelier
If you specified --savepw then isbg will remember your password the next time you run against the same server with the same username. You should not specify --savepw in future runs unless you want to change the saved password.
118 2 Thomas Lecavelier
119 2 Thomas Lecavelier
h1(#running). Running it
120 2 Thomas Lecavelier
121 2 Thomas Lecavelier
You'll probably want something to actually be done with the original spams in your inbox. By default nothing happens to them, but you have two options available. If you specify --flag then spams will be flagged.
122 2 Thomas Lecavelier
123 2 Thomas Lecavelier
You can get the messages marked for deletion by specifying --delete. If you never want to see them in your inbox, also specify the --expunge option after --delete and they will be removed when isbg logs out of the IMAP server.
124 2 Thomas Lecavelier
125 2 Thomas Lecavelier
h1(#folderNames). Your folder names
126 2 Thomas Lecavelier
127 2 Thomas Lecavelier
Each IMAP implementation names their folders differently, and most IMAP clients manage to hide most of this from you. If your IMAP server is Courier, then your folders are all below INBOX, and use dots to seperate the components.
128 2 Thomas Lecavelier
129 2 Thomas Lecavelier
The UWash server typically has the folders below Mail and uses slash '/' to seperate components.
130 2 Thomas Lecavelier
131 2 Thomas Lecavelier
h1(#advanced). Advanced options
132 2 Thomas Lecavelier
133 2 Thomas Lecavelier
If you would like to do something beyond the options listed in the usage message above, isbg actually has several more options that can be used. You will easily be able to figure them out from looking in the isbg.py file.
134 2 Thomas Lecavelier
135 2 Thomas Lecavelier
h1(#how). How does it work?
136 2 Thomas Lecavelier
137 2 Thomas Lecavelier
IMAP assigns each message in a folder a unique id. isbg scans the folder for messages it hasn't seen before, and for each one, downloads the message and feeds it to SpamAssassin. If SpamAssassin says the message is spam, then the SpamAssassin report is uploaded into your spam folder. Unless you specify the --noreport option, in which case the message is copied from your Inbox to the Spam folder (the copy happens     on the IMAP server itself so this option is good if you are on a low bandwidth connection).
138 2 Thomas Lecavelier
139 2 Thomas Lecavelier
h1(#multiple). Multiple accounts
140 2 Thomas Lecavelier
141 2 Thomas Lecavelier
By default isbg saves the list of seen IMAP message unique ids in a file in your home directory. It is named .isbg-trackXXXX where XXXX is a 16 byte identifier based on the IMAP host, username and port number. Consequently you can just run isbg against different servers/accounts and it will automatically keep the tracked uids seperate. You can override the filename with --trackfile.
142 2 Thomas Lecavelier
143 2 Thomas Lecavelier
h1(#password). Saving your password
144 2 Thomas Lecavelier
145 2 Thomas Lecavelier
If you don't want isbg to prompt you for your password each time, you can specify the --savepw option. This will save the password in a file in your home directory. The file is named .isbg-XXXX where XXXX is a 16 byte identifier based on the IMAP host, username and port number (the same as for the multiple accounts description above). You can override the filename with --passwordfilename
146 2 Thomas Lecavelier
147 2 Thomas Lecavelier
The password is obfuscated, so anyone just looking at the contents won't be able to see what it is. However, if they study the code to isbg then they will be able to figure out how to de-obfuscate it, and recover the original password. (isbg needs the original password each time it is run as well).
148 2 Thomas Lecavelier
                                                                                                                                                                                                                    
149 2 Thomas Lecavelier
Consequently you should regard this as providing minimal protection if someone can read the file.
150 2 Thomas Lecavelier
151 2 Thomas Lecavelier
h1(#ssl). SSL
152 2 Thomas Lecavelier
153 2 Thomas Lecavelier
isbg can do IMAP over SSL if your version of Python has been compiled with SSL support. (Specifically it looks for the function socket.ssl). You can tell if you have ssl support by running isbg.py --help. If --ssl is listed in the options then you have ssl support.
154 2 Thomas Lecavelier
155 2 Thomas Lecavelier
156 2 Thomas Lecavelier
However you should be aware that the SSL support does NOT check the certificate name nor validate the issuer. If an attacker can intercept the connection and modify all the packets flowing by, then they will be able to pose as the IMAP server. Other than that, the connection will have the usual security features of SSL.
157 2 Thomas Lecavelier
158 2 Thomas Lecavelier
h1(#exit). Exit Codes
159 2 Thomas Lecavelier
160 2 Thomas Lecavelier
When ISBG exits, it uses the exit code to tell you what happened. In general it is zero if all went well, and non-zero if there was a problem. You can turn on additional reporting by using the --exitcodes command line option.
161 2 Thomas Lecavelier
code  --exitcodes needed  description
162 2 Thomas Lecavelier
0       All went well
163 2 Thomas Lecavelier
1   yes   There was at least one new message, and none of the messages were spam
164 2 Thomas Lecavelier
2   yes   There was at least one new message, and all messages were spam
165 2 Thomas Lecavelier
3   yes   There were new messages, with at least one spam and one non-spam
166 2 Thomas Lecavelier
10      There were errors in the command line arguments
167 2 Thomas Lecavelier
11      The IMAP server reported an error
168 2 Thomas Lecavelier
169 2 Thomas Lecavelier
h1(#flags). Read and Seen flags
170 2 Thomas Lecavelier
171 2 Thomas Lecavelier
There are two flags IMAP uses to mark messages, Recent and Seen. Recent is sent to the first IMAP client that connects after a new message is received. Other clients or subsequent connections won't see that flag. The Seen flag is used to mark a message as read. IMAP clients explicitly set Seen when a message is being read.
172 2 Thomas Lecavelier
173 2 Thomas Lecavelier
Pine and some other mailers use the Recent flag to mark new mail. Unfortunately this means that if isbg or any other IMAP client has even looked at the Inbox, the messages won't be shown as new. It really should be using Seen.
174 2 Thomas Lecavelier
175 2 Thomas Lecavelier
The IMAP specification does not permit clients to change the Recent flag.
176 2 Thomas Lecavelier
177 2 Thomas Lecavelier
178 2 Thomas Lecavelier
h1(#todo). Todo list
179 2 Thomas Lecavelier
180 2 Thomas Lecavelier
* Auto report messages to Razor (high scoring ones that are definitely spam)
181 2 Thomas Lecavelier
* Seperate out messages that may be false positives (scores close to SpamAssassin thresholds) from the definite spam ones.
182 2 Thomas Lecavelier
183 2 Thomas Lecavelier
h1(#contact). Contact and about
184 2 Thomas Lecavelier
185 2 Thomas Lecavelier
This software was written by Roger Binns <rogerb@rogerbinns.com> and is maintained by Thomas Lecavelier <thomas@lecavelier.name> since november 2009.
186 6 Thomas Lecavelier
With the great help of Anders Jenbo since v0.99.
187 2 Thomas Lecavelier
                                                                                                                                                                                                                    
188 2 Thomas Lecavelier
h1(#license). License
189 2 Thomas Lecavelier
190 2 Thomas Lecavelier
As said by Roger Binns when he hang over isbg to Thomas Lecavelier:  " You may use isbg under any OSI approved open source license such as those listed at http://opensource.org/licenses/alphabetical "
191 2 Thomas Lecavelier
192 2 Thomas Lecavelier
There is a Yahoo group - "imapspambegone":http://tech.groups.yahoo.com/group/imapspambegone/ for discussion and questions.