Skip to content

Commit 956bf60

Browse files
committed
Adding the initial version of the class and some examples.
1 parent 939b536 commit 956bf60

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

README

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pDNSbl is a simple php class that provides easy lookup to DNSbl results
2+
it can handle multiple lookups from multiple DNSbl site such as:
3+
efnetrbl, dronebl, spamhus, and many more!
4+
5+
This is the first version, so the examples and extras are not as much
6+
as I would hope for. Expect to find a sample IRC bot to check users
7+
that a) join b) connect to a IRC Network or Channel.
8+
9+
Later versions expect a CodeIgniter class.
10+
11+
Suggestions welcome.
12+
Enjoy!

dnsbl.class.php

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
/*
3+
This class is inteded to provide easy access to the DNSbl lists
4+
to help find open proxies and possible spammers
5+
I'll properly document the class later
6+
*/
7+
8+
class DNSbl {
9+
10+
public $defaultbl = 'dnsbl.dronebl.org';
11+
public $dnsbls = array();
12+
13+
/*
14+
Simple lookup method
15+
Checks if a ip is in the BL or not
16+
*/
17+
public function lookup($ip,$bl='dnsbl.dronebl.org',$type='A'){
18+
/* This function will not check why a host is banned or do multiple
19+
checks but it will check if it exists which is normally enough. */
20+
$ip_parts_array = explode(".",$ip);
21+
$reverse_ip = ""; // Create it so we can add to it in the loop
22+
for ($ip_segment = 3; $ip_segment >= 0; $ip_segment--) {
23+
// Reverse all the segments and create a valid a
24+
$reverse_ip .= $ip_parts_array[$ip_segment].".";
25+
}
26+
// Now we need to look up (even if its basic lookup
27+
$lookup = $reverse_ip.$bl;
28+
29+
return checkdnsrr($lookup,$type);
30+
}
31+
32+
/*
33+
Find the reason from the DNS lookup
34+
*/
35+
private function dnsbl_reason($dns_lookup){
36+
// We process the dns lookup and process loop till we get the first
37+
// result if we find none assume false
38+
$result = false;
39+
// This IS VERY BUGGY - Any thoughts would be helpful.
40+
for($i = 0; $i <= (count($dns_lookup)-1); $i++){
41+
if(isset($dns_lookup[$i]['txt'])){
42+
return $dns_lookup[$i]['txt'];
43+
}
44+
}
45+
}
46+
47+
/*
48+
Converts ip address into the reverse address for dnsbl lookup
49+
*/
50+
protected function convert_ipaddr($ip){
51+
$ip_parts_array = explode(".",$ip);
52+
$reverse_ip = ""; // Create it so we can add to it in the loop
53+
// Loop through the list till we can convert the reverse of the ip
54+
for ($ip_segment = 3; $ip_segment >= 0; $ip_segment--) {
55+
// Reverse all the segments and create a valid a
56+
$reverse_ip .= $ip_parts_array[$ip_segment].".";
57+
}
58+
// Now we need to look up (even if its basic lookup)
59+
return $reverse_ip;
60+
}
61+
62+
/*
63+
Gets the default DNSbl from the variable defaultbl
64+
*/
65+
private function default_dnsbl(){
66+
return $this->defaultbl;
67+
}
68+
69+
/*
70+
Used to look through the DNSbls and check if the ip is found
71+
in any. This is very dangerous because not all BLs might be
72+
updated.
73+
*/
74+
private function lookup_loop($ip){
75+
$return_array = array();
76+
foreach($this->dnsbls as $bl){
77+
// Return the bool result of the lookup
78+
$return_array[$bl] = $this->lookup($ip,$bl);
79+
}
80+
return $return_array;
81+
}
82+
83+
/*
84+
Function is used get the dnsbl lookups
85+
86+
*/
87+
public function dnsbls($list){
88+
if(is_array($list)){
89+
$this->dnsbls = $list;
90+
} else {
91+
return false;
92+
}
93+
}
94+
/*
95+
method is used to check through a list of DNSbls
96+
97+
This is very dangerous because not all BLs might be
98+
updated.
99+
*/
100+
public function multi_check($ip,$return_array=TRUE){
101+
// Check for an array of DNSbls
102+
// If none are found use the default
103+
if(!is_array($this->dnsbls)){
104+
$this->lookup($ip,$this->default_dnsbl());
105+
} else {
106+
// We have an array and we loop through the results.
107+
return $this->lookup_loop($ip);
108+
}
109+
}
110+
111+
/*
112+
method used to check the reason for being banned
113+
it depends on getting accurate results from the DNS server
114+
use
115+
*/
116+
public function dnsbl_type_check($ip,$bl=NULL,$type=DNS_ALL){
117+
if(!isset($bl)){
118+
$bl = $this->default_dnsbl();
119+
}
120+
$return_type = 'text';
121+
/* This function will not check why a host is banned or do multiple
122+
checks but it will check if it exists which is normally enough. */
123+
$lookup = $this->convert_ipaddr($ip).$bl;
124+
// Look up the dns record
125+
$result = dns_get_record($lookup,$type);
126+
// Check to make sure it worked
127+
//return $this->dnsbl_reason($result);
128+
if($result){
129+
// Perhaps in another version return option bool or text
130+
if($return_type == 'bool'){
131+
return true;
132+
} else {
133+
// and default + return
134+
return $this->dnsbl_reason($result);
135+
}
136+
} else {
137+
// If the value is invalid, its returned false
138+
return false;
139+
}
140+
}
141+
}
142+
143+
?>

examples.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/* EXAMPLES
3+
I will add some better examples a bit later
4+
*/
5+
include 'dnsbl.class.php';
6+
7+
$ip = "127.0.0.1"; // Put an example ip here
8+
9+
$dnsbl = new DNSbl;
10+
if($dnsbl->lookup($ip)){
11+
echo "This ip is found";
12+
} else {
13+
echo "This ip is not found in the DNSbl.";
14+
}
15+
16+
// Lookup a Reason
17+
$reason = $dnsbl->dnsbl_type_check($ip);
18+
if($reason){
19+
echo "IP is banned for ".$reason;
20+
}
21+
22+
// Try doing a multi-lookup
23+
24+
$lookups = array(
25+
'dnsbl.dronebl.org',
26+
'dnsbl.efnetrbl.org',
27+
'http.dnsbl.sorbs.net',
28+
'xbl.spamhaus.org'
29+
);
30+
$dnsbl->dnsbls($lookups);
31+
print_r($dnsbl->multi_check($ip));
32+
33+
?>

0 commit comments

Comments
 (0)