473,796 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help beautify ugly heuristic code

I have a function that recognizes PTR records for dynamic IPs. There is
no hard and fast rule for this - every ISP does it differently, and may
change their policy at any time, and use different conventions in
different places. Nevertheless, it is useful to apply stricter
authentication standards to incoming email when the PTR for the IP
indicates a dynamic IP (namely, the PTR record is ignored since it doesn't
mean anything except to the ISP). This is because Windoze Zombies are the
favorite platform of spammers.

Here is the very ugly code so far. It offends me to look at it, but
haven't had any better ideas. I have lots of test data from mail logs.

# examples we don't yet recognize:
#
# 1Cust65.tnt4.at l4.da.uu.net at ('67.192.40.65' , 4588)
# 1Cust200.tnt8.b ne1.da.uu.net at ('203.61.67.200 ', 4144)
# 1Cust141.tnt30. rtm1.nld.da.uu. net at ('213.116.154.1 41', 2036)
# user64.net2045. mo.sprint-hsd.net at ('67.77.185.64' , 3901)
# wiley-268-8196.roadrunner .nf.net at ('205.251.174.4 6', 4810)
# 221.fib163.satn et.net at ('200.69.163.22 1', 3301)
# cpc2-ches1-4-0-cust8.lutn.cabl e.ntl.com at ('80.4.105.8', 61099)
# user239.res.ope nband.net at ('65.246.82.239 ', 1392)
# xdsl-2449.zgora.dial og.net.pl at ('81.168.237.14 5', 1238)
# spr1-runc1-4-0-cust25.bagu.bro adband.ntl.com at ('80.5.10.25', 1684)
# user-0c6s7hv.cable.m indspring.com at ('24.110.30.63' , 3720)
# user-0c8hvet.cable.m indspring.com at ('24.136.253.22 1', 4529)
# user-0cdf5j8.cable.m indspring.com at ('24.215.150.10 4', 3783)
# mmds-dhcp-11-143.plateautel. net at ('63.99.131.143 ', 4858)
# ca-santaanahub-cuda3-c6b-134.anhmca.adel phia.net at ('68.67.152.134 ', 62047)
# cbl-sd-02-79.aster.com.do at ('200.88.62.79' , 4153)
# h105n6c2o912.br edband.skanova. com at ('213.67.33.105 ', 3259)

import re

ip3 = re.compile('([0-9]{1,3})[.x-]([0-9]{1,3})[.x-]([0-9]{1,3})')
rehmac = re.compile(
'h[0-9a-f]{12}[.]|pcp[0-9]{6,10}pcs[.]|no-reverse|S[0-9a-f]{16}[.][a-z]{2}[.]'
)

def is_dynip(host,a ddr):
"""Return True if hostname is for a dynamic ip.
Examples:
is_dynip('post3 .fabulousdealz. com','69.60.99. 112') False is_dynip('adsl-69-208-201-177.dsl.emhril. ameritech.net', '69.208.201.177 ') True is_dynip('[1.2.3.4]','1.2.3.4')

True
"""
if host.startswith ('[') and host.endswith(']'):
return True
if addr:
if host.find(addr) >= 0: return True
a = addr.split('.')
ia = map(int,a)
m = ip3.search(host )
if m:
g = map(int,m.group s())
if g == ia[1:] or g == ia[:3]: return True
if g[0] == ia[3] and g[1:] == ia[:2]: return True
g.reverse()
if g == ia[1:] or g == ia[:3]: return True
if rehmac.search(h ost): return True
if host.find("%s." % '-'.join(a[2:])) >= 0: return True
if host.find("w%s. " % '-'.join(a[:2])) >= 0: return True
if host.find("dsl% s-" % '-'.join(a[:2])) >= 0: return True
if host.find(''.jo in(a[:3])) >= 0: return True
if host.find(''.jo in(a[1:])) >= 0: return True
x = "%02x%02x%02x%0 2x" % tuple(ia)
if host.lower().fi nd(x) >= 0: return True
z = [n.zfill(3) for n in a]
if host.find('-'.join(z)) >= 0: return True
if host.find("-%s." % '-'.join(z[2:])) >= 0: return True
if host.find("%s." % ''.join(z[2:])) >= 0: return True
if host.find(''.jo in(z)) >= 0: return True
a.reverse()
if host.find("%s." % '-'.join(a[:2])) >= 0: return True
if host.find("%s." % '.'.join(a[:2])) >= 0: return True
if host.find("%s." % a[0]) >= 0 and \
host.find('.ads l.') > 0 or host.find('.dia l-up.') > 0: return True
return False

if __name__ == '__main__':
import fileinput
for ln in fileinput.input ():
a = ln.split()
if len(a) == 2:
ip,host = a
if host.startswith ('[') and host.endswith(']'):
continue # no PTR
if is_dynip(host,i p):
print ip,host
Jul 18 '05
14 2508
On Wed, 08 Dec 2004 18:38:14 -0500, Stuart D. Gathman wrote:
Here are the last 20 (which my subjective judgement says are correct):
65.112.76.15 usfshlxmx01.myr eg.net 201.128.108.41

[snip] 80.143.79.97 p508F4F61.dip0. t-ipconnect.de DYN


Looks like you could do something like look for a part of the dns name
which is over a certain length with a high fraction of numbers and
punctuation characters.

Of course, you could build up a probability tree of the chance of a
character being followed by another character in a dynamic name and a
static name.

This might work well, as many are sequences of numbers (high chance of
number followed by number), and mixed characters and numbers, which are
unusual in normal dns names.

Jeremy

Jul 18 '05 #11
On Thu, 09 Dec 2004 00:01:36 -0800, Lonnie Princehouse wrote:
I believe you can still do this with only compiling a regex once and
then performing a few substitutions on the hostname.


Cool idea. Convert ip matches to fixed patterns before matching a fixed
regex. The leftovers like shaw cable (which has the MAC address of the
cable modem instead of the IP) can still be handled with regex patterns.

I had an idea last night to compile 254 regexes, one for each possible
last IP byte - but I think your idea is better.

Mitja suggested a socring system reminiscent of SpamAssassin.

That gives me a few things to try.

--
Stuart D. Gathman <st****@bmsi.co m>
Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.

Jul 18 '05 #12
Stuart D. Gathman schreef:
I have a function that recognizes PTR records for dynamic IPs. There
is no hard and fast rule for this - every ISP does it differently, and
may change their policy at any time, and use different conventions in
different places. Nevertheless, it is useful to apply stricter
authentication standards to incoming email when the PTR for the IP
indicates a dynamic IP (namely, the PTR record is ignored since it
doesn't mean anything except to the ISP). This is because Windoze
Zombies are the favorite platform of spammers.


Did you also think about ISPs that use such a PTR record for both dynamic
and fixed IPs?

--
JanC

"Be strict when sending and tolerant when receiving."
RFC 1958 - Architectural Principles of the Internet - section 3.9
Jul 18 '05 #13
On Fri, 10 Dec 2004 22:03:20 +0000, JanC wrote:
Stuart D. Gathman schreef:
I have a function that recognizes PTR records for dynamic IPs. There
Did you also think about ISPs that use such a PTR record for both dynamic
and fixed IPs?


There seems to be a lot of misunderstandin g about this. I am not blocking
anyones mail because they have a dynamic looking PTR. I simply don't
accept such a PTR as MTA authentication. You see, MTAs *SHOULD* provide a
fully qualified domain as their HELO name which resolves to the IP of the
MTA. Sadly, however, many internet facing MTAs don't do this, but I
accept a meaningful PTR as a substitute. I also waive the requirement for
MTA authentication if the MAIL FROM has an SPF record
(http://spf.pobox.com).

So, if your MTA complies with RFC HELO recommendations , you'll have no
trouble sending me mail. You can even use a dynamic IP with a dynamic DNS
service.

I 'do* block PTR names of "." or "localhost" . I would like to block all
single word HELO names - but there are too many clueless mail admins out
there. People seem to be unsure of what to send for HELO.

--
Stuart D. Gathman <st****@bmsi.co m>
Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.

Jul 18 '05 #14
On Thu, 09 Dec 2004 00:01:36 -0800, Lonnie Princehouse wrote:
I believe you can still do this with only compiling a regex once and
then performing a few substitutions on the hostname.
That is a interesting idea. Convert ip matches to fixed patterns, and
*then* match the regex. I think I would convert hex matches to the same
pattern as decimal (and roman numeral). How would you handle zero fill?

1.2.3.4 001002003004foo .isp.com

An idea I had last night is to precompile 254 regexes - one for each of
the possible last ip bytes. However, your idea is cleaner - except, how
would it handle ip bytes that are the same: 1.2.2.2

Mitja has proposed a scoring system reminiscent of SpamAssassin.

This gives me a few things to try.

--
Stuart D. Gathman <st****@bmsi.co m>
Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.

Jul 25 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3400
by: Andrzej Adam Filip | last post by:
Could you post some recommendation/advices which options should be used when using tidy to beautify xhtml ? It seems that "wrapped" xhtml produced by standards settings is not "liked" by some search engines . e.g. tidy inserted line breaks in attributes after opening " .....<a href=" http://example.com/"> ...
40
3254
by: Peter Row | last post by:
Hi all, Here is my problem: I have a SQL Server 2000 DB with various NVarChar, NText fields in its tables. For some stupid reason the data was inserted into these fields in UTF8 encoding. However when you retrieve these values into a dataset and ToString() them
13
2147
by: EggsAckley | last post by:
Hi: I have a file that I have been told is a SQL Server backup from a server somewhere. The file is about 200MB in size I am trying to create the database on my local server using RESTORE. I created the backup device, associated it with a backup name etc., copied the file into the backup dir. When I run the RESTORE command, Query Analyzer tells me the database
24
2852
by: chri_schiller | last post by:
I have a home-made website that provides a free 1100 page physics textbook. It is written in html and css. I recently added some chinese text, and since that day there are problems. The entry page has two chinese characters, but these are not seen on all browsers, even though the page is validated by the w3c validator. ( http://www.motionmountain.net/welcome.html)
5
1229
by: Buchwald | last post by:
hello group, I have a long (large) script that shows a random picture when a webpage is refreshed. It's long because i have a lot of pictures: 246 Here is some code: ----------------------------------------------------------------------------------------------- <!-- image1="smallpics/001-smallpic.jpg"
11
2818
by: davecph | last post by:
I'm constructing a website with a layout created with div-tags. They have a fixed width, float left, and display inline. When one of the div's contain a select-element the right-most div floats down for no apparent reason, but when the select-elements are gone they all align as expected. No css apply to the select-elements. image of prob.: http://sdc.novasol.com/site/nov/TMP/withSelectBoxes.gif image of expected:...
4
1326
by: pbd22 | last post by:
Hi. In my script the below code creates a new element on the page with an associated delete button: var row_element = new Element( 'div', { 'class':'container', 'events':{
2
4184
by: =?Utf-8?B?SnJ4dHVzZXIx?= | last post by:
I just started using Windows Live OneCare, I had been using Norton, but was unable to fix the problems I was having. I have yet been unsuccessful with OneCare as well. I keep getting the same warning from OneCare, one is for Adware, the other is for a trojan, I clean both, but almost immediatly, I get the same warning? My Windows Defender is also shut down, not by me as I have no idea how to do this(or to turn it back on), but am still...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.