OSDN Git Service

Stop $asap, or do them all?
[pukiwiki/pukiwiki_sandbox.git] / spam.php
index d8f8fb0..b3e7128 100644 (file)
--- a/spam.php
+++ b/spam.php
@@ -1,5 +1,5 @@
 <?php
-// $Id: spam.php,v 1.17 2006/11/12 14:09:51 henoheno Exp $
+// $Id: spam.php,v 1.36 2006/11/25 13:55:34 henoheno Exp $
 // Copyright (C) 2006 PukiWiki Developers Team
 // License: GPL v2 or (at your option) any later version
 
@@ -7,49 +7,80 @@
 
 // Return an array of URIs in the $string
 // [OK] http://nasty.example.org#nasty_string
-// [OK] http://nasty.example.org/foo/xxx#nasty_string/bar
-// [OK] ftp://dfshodfs:80/dfsdfs
-function uri_pickup($string = '', $normalize = TRUE)
+// [OK] http://nasty.example.org:80/foo/xxx#nasty_string/bar
+// [OK] ftp://nasty.example.org:80/dfsdfs
+// [OK] ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm (from RFC3986)
+function uri_pickup($string = '', $normalize = TRUE,
+       $preserve_rawuri = FALSE, $preserve_chunk = TRUE)
 {
-       // Not available for: user@password, IDN, Fragment(=ignored)
+       // Not available for: IDN(ignored)
        $array = array();
        preg_match_all(
-               // Refer RFC3986
+               // scheme://userinfo@host:port/path/or/pathinfo/maybefile.and?query=string#fragment
+               // Refer RFC3986 (Regex below is not strict)
                '#(\b[a-z][a-z0-9.+-]{1,8})://' .       // 1: Scheme
+               '(?:' .
+                       '([^\s<>"\'\[\]/\#?@]*)' .              // 2: Userinfo (Username)
+               '@)?' .
                '(' .
-                       // 2: Host
+                       // 3: Host
                        '\[[0-9a-f:.]+\]' . '|' .                               // IPv6([colon-hex and dot]): RFC2732
                        '(?:[0-9]{1-3}\.){3}[0-9]{1-3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
                        '[^\s<>"\'\[\]:/\#?]+' .                                // FQDN: foo.example.org
                ')' .
-               '(?::([a-z0-9]{2,}))?' .                        // 3: Port
-               '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' .      // 4: Directory path or path-info
-               '([^\s<>"\'\[\]\#]+)?' .                        // 5: File and query string
-                                                                                       // #: Fragment(ignored)
+               '(?::([0-9]*))?' .                                      // 4: Port
+               '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' .      // 5: Directory path or path-info
+               '([^\s<>"\'\[\]\#]+)?' .                        // 6: File and query string
+               '(?:\#([a-z0-9._~%!$&\'()*+,;=:@-]*))?' .       // 7: Fragment
                '#i',
-                $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
+                $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE
+       );
        //var_dump(recursive_map('htmlspecialchars', $array));
 
        // Shrink $array
-       $parts = array(1 => 'scheme', 2 => 'host', 3 => 'port',
-               4 => 'path', 5 => 'file');
+       static $parts = array(
+               1 => 'scheme', 2 => 'userinfo', 3 => 'host', 4 => 'port',
+               5 => 'path', 6 => 'file', 7 => 'fragment'
+       );
        $default = array('');
        foreach(array_keys($array) as $uri) {
-               unset($array[$uri][0]); // Matched string itself
                array_rename_keys($array[$uri], $parts, TRUE, $default);
                $offset = $array[$uri]['scheme'][1]; // Scheme's offset
 
-               // Remove offsets for each part
+               foreach(array_keys($array[$uri]) as $part) {
+                       // Remove offsets for each part
+                       $array[$uri][$part] = & $array[$uri][$part][0];
+               }
+
                if ($normalize) {
-                       foreach(array_keys($array[$uri]) as $part) {
-                               $array[$uri][$part] = strtolower($array[$uri][$part][0]);
-                       }
-                       $array[$uri]['path'] = path_normalize($array[$uri]['path']);
+                       $array[$uri]['scheme'] = scheme_normalize($array[$uri]['scheme']);
+                       //if ($array[$uri]['scheme'] === '') {
+                       //      // Ignore
+                       //      unset ($array[$uri]);
+                       //      continue;
+                       //}
+                       
+                       $array[$uri]['host']   = strtolower($array[$uri]['host']);
+                       $array[$uri]['port']   = port_normalize($array[$uri]['port'], $array[$uri]['scheme'], FALSE);
+                       $array[$uri]['path']   = path_normalize($array[$uri]['path']);
+                       //$array[$uri]['uri']    = uri_array_implode($array[$uri]);
+                       if ($preserve_rawuri) $array[$uri]['rawuri'] = & $array[$uri][0];
                } else {
-                       foreach(array_keys($array[$uri]) as $part) {
-                               $array[$uri][$part] = & $array[$uri][$part][0];
-                       }
+                       $array[$uri]['uri'] = & $array[$uri][0]; // Raw
                }
+               unset($array[$uri][0]); // Matched string itself
+               if (! $preserve_chunk) {
+                       unset(
+                               $array[$uri]['scheme'],
+                               $array[$uri]['userinfo'],
+                               $array[$uri]['host'],
+                               $array[$uri]['port'],
+                               $array[$uri]['path'],
+                               $array[$uri]['file'],
+                               $array[$uri]['fragment']
+                       );
+               }
+
                $array[$uri]['offset'] = $offset;
                $array[$uri]['area']   = 0;
        }
@@ -57,25 +88,69 @@ function uri_pickup($string = '', $normalize = TRUE)
        return $array;
 }
 
+// Domain exposure callback (See spam_uri_pickup_preprocess())
+// http://victim.example.org/?foo+site:nasty.example.com+bar
+// => http://nasty.example.com/?refer=victim.example.org
+// NOTE: 'refer=' is not so good for (at this time).
+// Consider about using IP address of the victim, try to avoid that.
+function _preg_replace_callback_domain_exposure($matches = array())
+{
+       $result = '';
+
+       // Preserve the victim URI as a complicity or ...
+       if (isset($matches[5])) {
+               $result =
+                       $matches[1] . '://' .   // scheme
+                       $matches[2] . '/' .             // victim.example.org
+                       $matches[3];                    // The rest of all (before victim)
+       }
+
+       // Flipped URI
+       $result = 
+               $matches[1] . '://' .   // scheme
+               $matches[4] .                   // nasty.example.com
+               '/?refer=' . strtolower($matches[2]) .  // victim.example.org
+               ' ' . $result;
+
+       return $result;
+}
+
 // Preprocess: rawurldecode() and adding space(s) to detect/count some URIs _if possible_
 // NOTE: It's maybe danger to var_dump(result). [e.g. 'javascript:']
 // [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
 // [OK] http://victim.example.org/http://nasty.example.org
 function spam_uri_pickup_preprocess($string = '')
 {
-       if (is_string($string)) {
-               return preg_replace(
-                       array(
-                               '#(?:https?|ftp):/#',
-                               '#\b[a-z][a-z0-9.+-]{1,8}://#i',
-                               '#[a-z][a-z0-9.+-]{1,8}://#i'
-                       ),
-                       ' $0',
-                       rawurldecode($string)
-                       );
-       } else {
-               return '';
-       }
+       if (! is_string($string)) return '';
+
+       $string = rawurldecode($string);
+
+       // Domain exposure (See _preg_replace_callback_domain_exposure())
+       $string = preg_replace_callback(
+               array(
+                       // Something Google: http://www.google.com/supported_domains
+                       '#(http)://([a-z0-9.]+\.google\.[a-z]{2,3}(?:\.[a-z]{2})?)/' .
+                       '([a-z0-9?=&.%_+-]+)' .         // ?query=foo+
+                       '\bsite:([a-z0-9.%_-]+)' .      // site:nasty.example.com
+                       '()' .  // Preserve?
+                       '#i',
+               ),
+               '_preg_replace_callback_domain_exposure',
+               $string
+       );
+
+       // URI exposure (uriuri => uri uri)
+       $string = preg_replace(
+               array(
+                       '#(?<! )(?:https?|ftp):/#',
+               //      '#[a-z][a-z0-9.+-]{1,8}://#i',
+               //      '#[a-z][a-z0-9.+-]{1,8}://#i'
+               ),
+               ' $0',
+               $string
+       );
+
+       return $string;
 }
 
 // TODO: Area selection (Check BBCode only, check anchor only, check ...)
@@ -99,8 +174,8 @@ function spam_uri_pickup($string = '')
                //var_dump(recursive_map('htmlspecialchars', $areas));
                foreach(array_keys($areas) as $area) {
                        $areas[$area] =  array(
-                               $areas[$area][0][1], // [0][1] = Area start (<a href>)
-                               $areas[$area][1][1], // [1][1] = Area end   (</a>)
+                               $areas[$area][0][1], // Area start (<a href>)
+                               $areas[$area][1][1], // Area end   (</a>)
                        );
                }
                area_measure($areas, $array);
@@ -117,8 +192,8 @@ function spam_uri_pickup($string = '')
                //var_dump(recursive_map('htmlspecialchars', $areas));
                foreach(array_keys($areas) as $area) {
                        $areas[$area] = array(
-                               $areas[$area][0][1], // [0][1] = Area start ([url])
-                               $areas[$area][2][1], // [4][1] = Area end   ([/url])
+                               $areas[$area][0][1], // Area start ([url])
+                               $areas[$area][2][1], // Area end   ([/url])
                        );
                }
                area_measure($areas, $array);
@@ -190,11 +265,92 @@ function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key =
 // ---------------------
 // Part Two
 
+// Scheme normalization: Renaming the schemes
+// snntp://example.org =>  nntps://example.org
+// NOTE: Keep the static lists simple. See also port_normalize().
+function scheme_normalize($scheme = '', $considerd_harmfull = TRUE)
+{
+       // Abbreviations considerable they don't have link intension
+       static $abbrevs = array(
+               'ttp'   => 'http',
+               'ttps'  => 'https',
+       );
+
+       // Alias => normalized
+       static $aliases = array(
+               'pop'   => 'pop3',
+               'news'  => 'nntp',
+               'imap4' => 'imap',
+               'snntp' => 'nntps',
+               'snews' => 'nntps',
+               'spop3' => 'pop3s',
+               'pops'  => 'pop3s',
+       );
+
+       $scheme = strtolower(trim($scheme));
+       if (isset($abbrevs[$scheme])) {
+               if ($considerd_harmfull) {
+                       $scheme = $abbrevs[$scheme];
+               } else {
+                       $scheme = '';
+               }
+       }
+       if (isset($aliases[$scheme])) $scheme = $aliases[$scheme];
+
+       return $scheme;
+}
+
+// Port normalization: Suppress the (redundant) default port
+// HTTP://example.org:80/ => http://example.org/
+// HTTP://example.org:8080/ => http://example.org:8080/
+// HTTPS://example.org:443/ => https://example.org/
+function port_normalize($port, $scheme, $scheme_normalize = TRUE)
+{
+       // Schemes that users _maybe_ want to add protocol-handlers
+       // to their web browsers. (and attackers _maybe_ want to use ...)
+       // Reference: http://www.iana.org/assignments/port-numbers
+       static $array = array(
+               // scheme => default port
+               'ftp'     =>    21,
+               'ssh'     =>    22,
+               'telnet'  =>    23,
+               'smtp'    =>    25,
+               'tftp'    =>    69,
+               'gopher'  =>    70,
+               'finger'  =>    79,
+               'http'    =>    80,
+               'pop3'    =>   110,
+               'sftp'    =>   115,
+               'nntp'    =>   119,
+               'imap'    =>   143,
+               'irc'     =>   194,
+               'wais'    =>   210,
+               'https'   =>   443,
+               'nntps'   =>   563,
+               'rsync'   =>   873,
+               'ftps'    =>   990,
+               'telnets' =>   992,
+               'imaps'   =>   993,
+               'ircs'    =>   994,
+               'pop3s'   =>   995,
+               'mysql'   =>  3306,
+       );
+
+       $port = trim($port);
+       if ($port === '') return $port;
+
+       if ($scheme_normalize) $scheme = scheme_normalize($scheme);
+       if (isset($array[$scheme]) && $port == $array[$scheme])
+               $port = ''; // Ignore the defaults
+
+       return $port;
+}
+
 // Path normalization
-// example.org => example.org/
-// example.org#hoge -> example.org/#hoge
-// example.org/path/a/b/./c////./d -> example.org/path/a/b/c/d
-// example.org/path/../../a/../back
+// http://example.org => http://example.org/
+// http://example.org#hoge => http://example.org/#hoge
+// http://example.org/path/a/b/./c////./d => http://example.org/path/a/b/c/d
+// http://example.org/path/../../a/../back => http://example.org/back
 function path_normalize($path = '', $divider = '/', $addroot = TRUE)
 {
        if (! is_string($path) || $path == '') {
@@ -227,56 +383,226 @@ function path_normalize($path = '', $divider = '/', $addroot = TRUE)
        return $path;
 }
 
-// Input: '/a/b'
-// Output: array('' => array('a' => array('b' => NULL)))
-function array_tree($string, $delimiter = '/', $reverse = FALSE)
+// An URI array => An URI (See uri_pickup())
+function uri_array_implode($uri = array())
 {
-       // Create a branch
-       $tree = NULL;
-       $tmps = explode($delimiter, $string);
-       if (! $reverse) $tmps = array_reverse($tmps);
-       foreach ($tmps as $tmp) {
-               $tree = array($tmp => $tree);
+       if (empty($uri) || ! is_array($uri)) return NULL;
+       
+       $tmp = array();
+       if (isset($uri['scheme']) && $uri['scheme'] !== '') {
+               $tmp[] = & $uri['scheme'];
+               $tmp[] = '://';
+       }
+       if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
+               $tmp[] = & $uri['userinfo'];
+               $tmp[] = '@';
+       }
+       if (isset($uri['host']) && $uri['host'] !== '') {
+               $tmp[] = & $uri['host'];
+       }
+       if (isset($uri['port']) && $uri['port'] !== '') {
+               $tmp[] = ':';
+               $tmp[] = & $uri['port'];
+       }
+       if (isset($uri['path']) && $uri['path'] !== '') {
+               $tmp[] = & $uri['path'];
+       }
+       if (isset($uri['file']) && $uri['file'] !== '') {
+               $tmp[] = & $uri['file'];
+       }
+       if (isset($uri['fragment']) && $uri['fragment'] !== '') {
+               $tmp[] = '#';
+               $tmp[] = & $uri['fragment'];
        }
-       return $tree;
-}
 
+       return implode('', $tmp);
+}
 
 // ---------------------
 // Part One : Checker
 
+function generate_glob_regex($string = '', $divider = '/')
+{
+       static $from = array(
+                       0 => '*',
+                       1 => '?',
+                       2 => '\[',
+                       3 => '\]',
+                       4 => '[',
+                       5 => ']',
+               );
+       static $mid = array(
+                       0 => '_AST_',
+                       1 => '_QUE_',
+                       2 => '_eRBR_',
+                       3 => '_eLBR_',
+                       4 => '_RBR_',
+                       5 => '_LBR_',
+               );
+       static $to = array(
+                       0 => '.*',
+                       1 => '.',
+                       2 => '\[',
+                       3 => '\]',
+                       4 => '[',
+                       5 => ']',
+               );
+
+       $string = str_replace($from, $mid, $string); // Hide
+       $string = preg_quote($string, $divider);
+       $string = str_replace($mid, $to, $string);   // Unhide
+
+       return $string;
+}
+
+// TODO: Ignore list
+// TODO: require_or_include_once(another file)
+function is_badhost($hosts = '', $asap = TRUE)
+{
+       static $blocklist_regex;
+
+       if (! isset($blocklist_regex)) {
+               $blocklist_regex = array();
+               $blocklist = array(
+                       // Deny all uri
+                       //'*',
+
+                       // IP address or ...
+                       //'10.20.*.*',  // 10.20.example.com also matches
+                       //'\[1\]',
+                       
+                       // Too much malicious sub-domains
+                       '*.blogspot.com',
+
+                       // 2006-11 dev
+                       'wwwtahoo.com',
+
+                       // 2006-11 dev
+                       '*.infogami.com',
+
+                       // 2006/11/19 17:50 dev
+                       '*.google0site.org',
+                       '*.bigpricesearch.org',
+                       '*.osfind.org',
+                       '*.bablomira.biz',
+               );
+               foreach ($blocklist as $part) {
+                       $blocklist_regex[] = '#^' . generate_glob_regex($part, '#') . '$#i';
+               }
+       }
+
+       $result = 0;
+       if (! is_array($hosts)) $hosts = array($hosts);
+       foreach($hosts as $host) {
+               if (! is_string($host)) $host = '';
+               foreach ($blocklist_regex as $regex) {
+                       if (preg_match($regex, $host)) {
+                               ++$result;
+                               if ($asap) {
+                                       return $result;
+                               } else {
+                                       break; // Check next host
+                               }
+                       }
+               }
+       }
+
+       return $result;
+}
+
+// TODO return TRUE or FALSE!
 // Simple/fast spam check
-function is_uri_spam($target = '')
+function check_uri_spam($target = '', $method = array(), $asap = TRUE)
 {
-       $is_spam = FALSE;
-       $urinum = 0;
+       $is_spam  = FALSE;
+       $progress = array(
+               'quantity' => 0,
+               'area'     => 0,
+               'non_uniq' => 0,
+               'badhost'  => 0,
+               );
+
+       if (! is_array($method) || empty($method)) {
+               // Default
+               $method = array(
+                       'quantity' => 8,                // Allow N URIs
+                       'area'     => TRUE,
+                       'non_uniq' => 3,                // Allow N times dupe
+                       'badhost'  => TRUE,
+               );
+       }
 
        if (is_array($target)) {
                foreach($target as $str) {
                        // Recurse
-                       list($is_spam, $_urinum) = is_uri_spam($str);
-                       $urinum += $_urinum;
-                       if ($is_spam) break;
+                       list($is_spam, $_progress) = check_uri_spam($str, $method);
+                       $progress['quantity'] += $_progress['quantity'];
+                       $progress['non_uniq'] += $_progress['non_uniq'];
+                       if ($asap || $is_spam) break;
                }
        } else {
                $pickups = spam_uri_pickup($target);
-               $urinum += count($pickups);
+               $progress['quantity'] += count($pickups);
+
                if (! empty($pickups)) {
-                       // Some users want to post some URLs, but ...
-                       if ($urinum > 8) {
-                               $is_spam = TRUE;        // Too many!
-                       } else {
+
+                       // URI quantity
+                       if ((! $is_spam || ! $asap) && isset($method['quantity']) &&
+                               $progress['quantity'] > $method['quantity']) {
+                               $is_spam = TRUE;
+                       }
+                       //var_dump($method['quantity'], $is_spam);
+
+                       // Using invalid area
+                       if ((! $is_spam || ! $asap) && isset($method['area'])) {
                                foreach($pickups as $pickup) {
                                        if ($pickup['area'] < 0) {
+                                               ++$progress['area'];
                                                $is_spam = TRUE;
-                                               break;
+                                               if ($asap) break;
+                                       }
+                               }
+                       }
+                       //var_dump($method['area'], $is_spam);
+
+                       // URI uniqueness (and removing non-uniques)
+                       if ((! $is_spam || ! $asap) && isset($method['non_uniq'])) {
+                               $uris = array();
+                               foreach ($pickups as $key => $pickup) {
+                                       $uris[$key] = uri_array_implode($pickup);
+                               }
+                               $count = count($uris);
+                               $uris = array_unique($uris);
+                               $progress['non_uniq'] += $count - count($uris);
+                               if ($progress['non_uniq'] > $method['non_uniq']) {
+                                       $is_spam = TRUE;
+                               }
+                               if (! $asap || ! $is_spam) {
+                                       foreach (array_diff(array_keys($pickups),
+                                               array_keys($uris)) as $remove) {
+                                               unset($pickups[$remove]);
                                        }
                                }
+                               unset($uris);
+                               //var_dump($uris, $pickups);
+                       }
+                       //var_dump($method['non_uniq'], $is_spam);
+
+                       // Bad host
+                       if ((! $is_spam || ! $asap) && isset($method['badhost'])) {
+                               $hosts = array();
+                               foreach ($pickups as $pickup) {
+                                       $hosts[] = & $pickup['host'];
+                               }
+                               $count = is_badhost(array_unique($hosts), $asap);
+                               $progress['badhost'] += $count;
+                               if ($count !== 0) $is_spam = TRUE;
                        }
+                       //var_dump($method['badhost'], $is_spam);
                }
        }
 
-       return array($is_spam, $urinum);
+       return array($is_spam, $progress);
 }
 
 // ---------------------
@@ -289,10 +615,10 @@ function is_invalid_useragent($ua_name = '' /*, $ua_vars = ''*/ )
 
 // ---------------------
 
-// TODO: Multi-metrics (uri, host, user-agent, ...)
+// TODO: Separate check-part(s) and mail part
 // TODO: Mail to administrator with more measurement data?
 // Simple/fast spam filter ($target: 'a string' or an array())
-function pkwk_spamfilter($action, $page, $target = array('title' => ''))
+function pkwk_spamfilter($action, $page, $target = array('title' => ''), $method = array())
 {
        $is_spam = FALSE;
 
@@ -300,7 +626,7 @@ function pkwk_spamfilter($action, $page, $target = array('title' => ''))
        if ($is_spam) {
                $action .= ' (Invalid User-Agent)';
        } else {
-               list($is_spam) = is_uri_spam($target);
+               list($is_spam) = check_uri_spam($target, $method);
        }
 
        if ($is_spam) {