<?php
-// $Id: spam.php,v 1.13 2006/11/05 03:36:40 henoheno Exp $
+// $Id: spam.php,v 1.26 2006/11/23 01:16:13 henoheno Exp $
// Copyright (C) 2006 PukiWiki Developers Team
// License: GPL v2 or (at your option) any later version
// Functions for Concept-work of spam-uri metrics
-// Return an array of normalized/parsed URIs in the $string
+// 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
-// [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
-// [OK] http://victim.example.org/gphttp://nasty.example.org
-function spam_pickup($string = '')
+// [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)
{
- // Preprocess: urldecode() and adding space(s)
- $string = preg_replace(
- array(
- '#(?:https?|ftp):/#',
- '#\b[a-z][a-z0-9.+-]{1,8}://#i',
- '#[a-z][a-z0-9.+-]{1,8}://#i'
- ), ' $0', urldecode($string));
-
- // URI pickup: 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 (with normalization)
foreach(array_keys($array[$uri]) as $part) {
- $array[$uri][$part] =
- strtolower($array[$uri][$part][0]);
+ // Remove offsets for each part
+ $array[$uri][$part] = & $array[$uri][$part][0];
+ }
+
+ if ($normalize) {
+ $array[$uri]['scheme'] = scheme_normalize($array[$uri]['scheme']);
+ $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 {
+ $array[$uri]['uri'] = & $array[$uri][0]; // Raw
}
- //$array[$uri]['path'] = path_normalize($array[$uri]['path']);
+ 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;
}
+ 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
+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 '';
+
+ $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
+ );
+
+ // Scheme exposure (schemescheme => scheme scheme)
+ $string = preg_replace(
+ array(
+ '#(?:https?|ftp):/#',
+ '#\b[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 ...)
+// Main function of spam-uri pickup
+function spam_uri_pickup($string = '')
+{
+ $string = spam_uri_pickup_preprocess($string);
+
+ $array = uri_pickup($string);
+
// Area elevation for '(especially external)link' intension
if (! empty($array)) {
// Anchor tags by preg_match_all()
//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);
- // Various Wiki syntax
- // [text_or_uri>text_or_uri]
- // [text_or_uri:text_or_uri]
- // [text_or_uri|text_or_uri]
- // [text_or_uri->text_or_uri]
- // [text_or_uri text_or_uri] // MediaWiki
- // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
-
// phpBB's "BBCode" by preg_match_all()
// [url]http://nasty.example.com/[/url]
// [link]http://nasty.example.com/[/link]
//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);
+ // Various Wiki syntax
+ // [text_or_uri>text_or_uri]
+ // [text_or_uri:text_or_uri]
+ // [text_or_uri|text_or_uri]
+ // [text_or_uri->text_or_uri]
+ // [text_or_uri text_or_uri] // MediaWiki
+ // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
+
// Remove 'offset's for area_measure()
- foreach(array_keys($array) as $key)
- unset($array[$key]['offset']);
+ //foreach(array_keys($array) as $key)
+ // unset($array[$key]['offset']);
}
return $array;
// ---------------------
// Part Two
+// Scheme normalization: Rename the schemes
+// snntp://example.org => nntps://example.org
+// NOTE: Keep the static list simple. See also port_normalize().
+function scheme_normalize($scheme = '')
+{
+ static $aliases = array(
+ // alias => normalized
+ 'pop' => 'pop3',
+ 'news' => 'nntp',
+ 'imap4' => 'imap',
+ 'snntp' => 'nntps',
+ 'snews' => 'nntps',
+ 'spop3' => 'pop3s',
+ 'pops' => 'pop3s',
+ );
+
+ $scheme = strtolower(trim($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 == '') {
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
+
+// TODO: globbing for IP address or something
+// TODO: Ignore list
+// TODO: require_or_include_once(another file)
+function is_badhost($host = '')
+{
+ static $blocklist_regex;
+
+ if (! isset($blocklist_regex)) {
+ $blocklist = array(
+ '.blogspot.com',
+ );
+ foreach ($blocklist as $part) {
+ $blocklist_regex[] = '#\b' . preg_quote($part, '#') . '$#';
+ }
+ }
+
+ foreach ($blocklist_regex as $regex) {
+ if (preg_match($regex, $host)) {
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+// TODO return TRUE or FALSE!
// Simple/fast spam check
function is_uri_spam($target = '')
{
+ static $blocklist = array(
+ '.blogspot.com',
+ );
+ static $blocklist_regex;
+
+ if (! isset($blocklist_regex)) {
+ foreach ($blocklist as $part) {
+ $blocklist_regex[] = '#\b' . preg_quote($part, '#') . '$#';
+ }
+ }
+
$is_spam = FALSE;
$urinum = 0;
if ($is_spam) break;
}
} else {
- $pickups = spam_pickup($target);
+ $pickups = spam_uri_pickup($target);
$urinum += count($pickups);
if (! empty($pickups)) {
// Some users want to post some URLs, but ...
}
}
}
+
+ foreach ($pickups as $pickup) {
+ if (is_badhost($pickup['host'])) {
+ $is_spam = TRUE;
+ break;
+ }
+ }
}
}
return array($is_spam, $urinum);
}
-// Mail to administrator with more measurement data?
-// Simple/fast spam filter (for one text field)
+// ---------------------
+
+// Check User-Agent (not testing yet)
+function is_invalid_useragent($ua_name = '' /*, $ua_vars = ''*/ )
+{
+ return $ua_name === '';
+}
+
+// ---------------------
+
+// TODO: Separate check-part(s) and mail part
+// TODO: Multi-metrics (uri, host, user-agent, ...)
+// 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' => ''))
{
$is_spam = FALSE;
- list($is_spam) = is_uri_spam($target);
+ //$is_spam = is_invalid_useragent('NOTYET');
if ($is_spam) {
+ $action .= ' (Invalid User-Agent)';
+ } else {
+ list($is_spam) = is_uri_spam($target);
+ }
+
+ if ($is_spam) {
+ // Mail to administrator(s)
global $notify, $notify_subject;
if ($notify) {
$footer['ACTION'] = $action;
if ($is_spam) spam_exit();
}
+// ---------------------
+
// Common bahavior for blocking
// NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
function spam_exit()