OSDN Git Service

54de9ff94d1f2d8c3207c177e650997f815dc851
[pukiwiki/pukiwiki_sandbox.git] / spam.php
1 <?php
2 // $Id: spam.php,v 1.26 2006/11/23 01:16:13 henoheno Exp $
3 // Copyright (C) 2006 PukiWiki Developers Team
4 // License: GPL v2 or (at your option) any later version
5
6 // Functions for Concept-work of spam-uri metrics
7
8 // Return an array of URIs in the $string
9 // [OK] http://nasty.example.org#nasty_string
10 // [OK] http://nasty.example.org:80/foo/xxx#nasty_string/bar
11 // [OK] ftp://nasty.example.org:80/dfsdfs
12 // [OK] ftp://cnn.example.com&story=breaking_news@10.0.0.1/top_story.htm (from RFC3986)
13 function uri_pickup($string = '', $normalize = TRUE,
14         $preserve_rawuri = FALSE, $preserve_chunk = TRUE)
15 {
16         // Not available for: IDN(ignored)
17         $array = array();
18         preg_match_all(
19                 // scheme://userinfo@host:port/path/or/pathinfo/maybefile.and?query=string#fragment
20                 // Refer RFC3986 (Regex below is not strict)
21                 '#(\b[a-z][a-z0-9.+-]{1,8})://' .       // 1: Scheme
22                 '(?:' .
23                         '([^\s<>"\'\[\]/\#?@]*)' .              // 2: Userinfo (Username)
24                 '@)?' .
25                 '(' .
26                         // 3: Host
27                         '\[[0-9a-f:.]+\]' . '|' .                               // IPv6([colon-hex and dot]): RFC2732
28                         '(?:[0-9]{1-3}\.){3}[0-9]{1-3}' . '|' . // IPv4(dot-decimal): 001.22.3.44
29                         '[^\s<>"\'\[\]:/\#?]+' .                                // FQDN: foo.example.org
30                 ')' .
31                 '(?::([0-9]*))?' .                                      // 4: Port
32                 '((?:/+[^\s<>"\'\[\]/\#]+)*/+)?' .      // 5: Directory path or path-info
33                 '([^\s<>"\'\[\]\#]+)?' .                        // 6: File and query string
34                 '(?:\#([a-z0-9._~%!$&\'()*+,;=:@-]*))?' .       // 7: Fragment
35                 '#i',
36                  $string, $array, PREG_SET_ORDER | PREG_OFFSET_CAPTURE
37         );
38         //var_dump(recursive_map('htmlspecialchars', $array));
39
40         // Shrink $array
41         static $parts = array(
42                 1 => 'scheme', 2 => 'userinfo', 3 => 'host', 4 => 'port',
43                 5 => 'path', 6 => 'file', 7 => 'fragment'
44         );
45         $default = array('');
46         foreach(array_keys($array) as $uri) {
47                 array_rename_keys($array[$uri], $parts, TRUE, $default);
48                 $offset = $array[$uri]['scheme'][1]; // Scheme's offset
49
50                 foreach(array_keys($array[$uri]) as $part) {
51                         // Remove offsets for each part
52                         $array[$uri][$part] = & $array[$uri][$part][0];
53                 }
54
55                 if ($normalize) {
56                         $array[$uri]['scheme'] = scheme_normalize($array[$uri]['scheme']);
57                         $array[$uri]['host']   = strtolower($array[$uri]['host']);
58                         $array[$uri]['port']   = port_normalize($array[$uri]['port'], $array[$uri]['scheme'], FALSE);
59                         $array[$uri]['path']   = path_normalize($array[$uri]['path']);
60
61                         //$array[$uri]['uri']    = uri_array_implode($array[$uri]);
62                         if ($preserve_rawuri) $array[$uri]['rawuri'] = & $array[$uri][0];
63                 } else {
64                         $array[$uri]['uri'] = & $array[$uri][0]; // Raw
65                 }
66                 unset($array[$uri][0]); // Matched string itself
67                 if (! $preserve_chunk) {
68                         unset(
69                                 $array[$uri]['scheme'],
70                                 $array[$uri]['userinfo'],
71                                 $array[$uri]['host'],
72                                 $array[$uri]['port'],
73                                 $array[$uri]['path'],
74                                 $array[$uri]['file'],
75                                 $array[$uri]['fragment']
76                         );
77                 }
78
79                 $array[$uri]['offset'] = $offset;
80                 $array[$uri]['area']   = 0;
81         }
82
83         return $array;
84 }
85
86 // Domain exposure callback (See spam_uri_pickup_preprocess())
87 // http://victim.example.org/?foo+site:nasty.example.com+bar
88 // => http://nasty.example.com/?refer=victim.example.org
89 function _preg_replace_callback_domain_exposure($matches = array())
90 {
91         $result = '';
92
93         // Preserve the victim URI as a complicity or ...
94         if (isset($matches[5])) {
95                 $result =
96                         $matches[1] . '://' .   // scheme
97                         $matches[2] . '/' .             // victim.example.org
98                         $matches[3];                    // The rest of all (before victim)
99         }
100
101         // Flipped URI
102         $result = 
103                 $matches[1] . '://' .   // scheme
104                 $matches[4] .                   // nasty.example.com
105                 '/refer=' . strtolower($matches[2]) . '/' .     // victim.example.org
106                 ' ' . $result;
107
108         return $result;
109 }
110
111 // Preprocess: rawurldecode() and adding space(s) to detect/count some URIs _if possible_
112 // NOTE: It's maybe danger to var_dump(result). [e.g. 'javascript:']
113 // [OK] http://victim.example.org/go?http%3A%2F%2Fnasty.example.org
114 // [OK] http://victim.example.org/http://nasty.example.org
115 function spam_uri_pickup_preprocess($string = '')
116 {
117         if (! is_string($string)) return '';
118
119         $string = rawurldecode($string);
120
121         // Domain exposure (See _preg_replace_callback_domain_exposure())
122         $string = preg_replace_callback(
123                 array(
124                         // Something Google: http://www.google.com/supported_domains
125                         '#(http)://([a-z0-9.]+\.google\.[a-z]{2,3}(?:\.[a-z]{2})?)/' .
126                         '([a-z0-9?=&.%_+-]+)' .         // ?query=foo+
127                         '\bsite:([a-z0-9.%_-]+)' .      // site:nasty.example.com
128                         '()' .  // Preserve?
129                         '#i',
130                 ),
131                 '_preg_replace_callback_domain_exposure',
132                 $string
133         );
134
135         // Scheme exposure (schemescheme => scheme scheme)
136         $string = preg_replace(
137                 array(
138                         '#(?:https?|ftp):/#',
139                         '#\b[a-z][a-z0-9.+-]{1,8}://#i',
140                         '#[a-z][a-z0-9.+-]{1,8}://#i'
141                 ),
142                 ' $0',
143                 $string
144         );
145
146         return $string;
147 }
148
149 // TODO: Area selection (Check BBCode only, check anchor only, check ...)
150 // Main function of spam-uri pickup
151 function spam_uri_pickup($string = '')
152 {
153         $string = spam_uri_pickup_preprocess($string);
154
155         $array  = uri_pickup($string);
156
157         // Area elevation for '(especially external)link' intension
158         if (! empty($array)) {
159                 // Anchor tags by preg_match_all()
160                 // [OK] <a href="http://nasty.example.com">visit http://nasty.example.com/</a>
161                 // [OK] <a href=\'http://nasty.example.com/\' >discount foobar</a> 
162                 // [NG] <a href="http://ng.example.com">visit http://ng.example.com _not_ended_
163                 // [NG] <a href=  >Good site!</a> <a href= "#" >test</a>
164                 $areas = array();
165                 preg_match_all('#<a\b[^>]*href[^>]*>.*?</a\b[^>]*(>)#i',
166                          $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
167                 //var_dump(recursive_map('htmlspecialchars', $areas));
168                 foreach(array_keys($areas) as $area) {
169                         $areas[$area] =  array(
170                                 $areas[$area][0][1], // Area start (<a href>)
171                                 $areas[$area][1][1], // Area end   (</a>)
172                         );
173                 }
174                 area_measure($areas, $array);
175
176                 // phpBB's "BBCode" by preg_match_all()
177                 // [url]http://nasty.example.com/[/url]
178                 // [link]http://nasty.example.com/[/link]
179                 // [url=http://nasty.example.com]visit http://nasty.example.com/[/url]
180                 // [link http://nasty.example.com/]buy something[/link]
181                 // ?? [url=][/url]
182                 $areas = array();
183                 preg_match_all('#\[(url|link)\b[^\]]*\].*?\[/\1\b[^\]]*(\])#i',
184                          $string, $areas, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
185                 //var_dump(recursive_map('htmlspecialchars', $areas));
186                 foreach(array_keys($areas) as $area) {
187                         $areas[$area] = array(
188                                 $areas[$area][0][1], // Area start ([url])
189                                 $areas[$area][2][1], // Area end   ([/url])
190                         );
191                 }
192                 area_measure($areas, $array);
193
194                 // Various Wiki syntax
195                 // [text_or_uri>text_or_uri]
196                 // [text_or_uri:text_or_uri]
197                 // [text_or_uri|text_or_uri]
198                 // [text_or_uri->text_or_uri]
199                 // [text_or_uri text_or_uri] // MediaWiki
200                 // MediaWiki: [http://nasty.example.com/ visit http://nasty.example.com/]
201
202                 // Remove 'offset's for area_measure()
203                 //foreach(array_keys($array) as $key)
204                 //      unset($array[$key]['offset']);
205         }
206
207         return $array;
208 }
209
210 // $array['something'] => $array['wanted']
211 function array_rename_keys(& $array, $keys = array('from' => 'to'), $force = FALSE, $default = '')
212 {
213         if (! is_array($array) || ! is_array($keys))
214                 return FALSE;
215
216         // Nondestructive test
217         if (! $force)
218                 foreach(array_keys($keys) as $from)
219                         if (! isset($array[$from]))
220                                 return FALSE;
221
222         foreach($keys as $from => $to) {
223                 if ($from === $to) continue;
224                 if (! $force || isset($array[$from])) {
225                         $array[$to] = & $array[$from];
226                         unset($array[$from]);
227                 } else  {
228                         $array[$to] = $default;
229                 }
230         }
231
232         return TRUE;
233 }
234
235 // If in doubt, it's a little doubtful
236 function area_measure($areas, & $array, $belief = -1, $a_key = 'area', $o_key = 'offset')
237 {
238         if (! is_array($areas) || ! is_array($array)) return;
239
240         $areas_keys = array_keys($areas);
241         foreach(array_keys($array) as $u_index) {
242                 $offset = isset($array[$u_index][$o_key]) ?
243                         intval($array[$u_index][$o_key]) : 0;
244                 foreach($areas_keys as $a_index) {
245                         if (isset($array[$u_index][$a_key])) {
246                                 $offset_s = intval($areas[$a_index][0]);
247                                 $offset_e = intval($areas[$a_index][1]);
248                                 // [Area => inside <= Area]
249                                 if ($offset_s < $offset && $offset < $offset_e) {
250                                         $array[$u_index][$a_key] += $belief;
251                                 }
252                         }
253                 }
254         }
255 }
256
257
258 // ---------------------
259 // Part Two
260
261 // Scheme normalization: Rename the schemes
262 // snntp://example.org =>  nntps://example.org
263 // NOTE: Keep the static list simple. See also port_normalize().
264 function scheme_normalize($scheme = '')
265 {
266         static $aliases = array(
267                 // alias => normalized
268                 'pop'   => 'pop3',
269                 'news'  => 'nntp',
270                 'imap4' => 'imap',
271                 'snntp' => 'nntps',
272                 'snews' => 'nntps',
273                 'spop3' => 'pop3s',
274                 'pops'  => 'pop3s',
275         );
276
277         $scheme = strtolower(trim($scheme));
278         if (isset($aliases[$scheme])) $scheme = $aliases[$scheme];
279
280         return $scheme;
281 }
282
283 // Port normalization: Suppress the (redundant) default port
284 // HTTP://example.org:80/ => http://example.org/
285 // HTTP://example.org:8080/ => http://example.org:8080/
286 // HTTPS://example.org:443/ => https://example.org/
287 function port_normalize($port, $scheme, $scheme_normalize = TRUE)
288 {
289         // Schemes that users _maybe_ want to add protocol-handlers
290         // to their web browsers. (and attackers _maybe_ want to use ...)
291         // Reference: http://www.iana.org/assignments/port-numbers
292         static $array = array(
293                 // scheme => default port
294                 'ftp'     =>    21,
295                 'ssh'     =>    22,
296                 'telnet'  =>    23,
297                 'smtp'    =>    25,
298                 'tftp'    =>    69,
299                 'gopher'  =>    70,
300                 'finger'  =>    79,
301                 'http'    =>    80,
302                 'pop3'    =>   110,
303                 'sftp'    =>   115,
304                 'nntp'    =>   119,
305                 'imap'    =>   143,
306                 'irc'     =>   194,
307                 'wais'    =>   210,
308                 'https'   =>   443,
309                 'nntps'   =>   563,
310                 'rsync'   =>   873,
311                 'ftps'    =>   990,
312                 'telnets' =>   992,
313                 'imaps'   =>   993,
314                 'ircs'    =>   994,
315                 'pop3s'   =>   995,
316                 'mysql'   =>  3306,
317         );
318
319         $port = trim($port);
320         if ($port === '') return $port;
321
322         if ($scheme_normalize) $scheme = scheme_normalize($scheme);
323         if (isset($array[$scheme]) && $port == $array[$scheme])
324                 $port = ''; // Ignore the defaults
325
326         return $port;
327 }
328
329 // Path normalization
330 // http://example.org => http://example.org/
331 // http://example.org#hoge => http://example.org/#hoge
332 // http://example.org/path/a/b/./c////./d => http://example.org/path/a/b/c/d
333 // http://example.org/path/../../a/../back => http://example.org/back
334 function path_normalize($path = '', $divider = '/', $addroot = TRUE)
335 {
336         if (! is_string($path) || $path == '') {
337                 $path = $addroot ? $divider : '';
338         } else {
339                 $path = trim($path);
340                 $last = ($path[strlen($path) - 1] == $divider) ? $divider : '';
341                 $array = explode($divider, $path);
342
343                 // Remove paddings
344                 foreach(array_keys($array) as $key) {
345                         if ($array[$key] == '' || $array[$key] == '.')
346                                  unset($array[$key]);
347                 }
348                 // Back-track
349                 $tmp = array();
350                 foreach($array as $value) {
351                         if ($value == '..') {
352                                 array_pop($tmp);
353                         } else {
354                                 array_push($tmp, $value);
355                         }
356                 }
357                 $array = & $tmp;
358
359                 $path = $addroot ? $divider : '';
360                 if (! empty($array)) $path .= implode($divider, $array) . $last;
361         }
362
363         return $path;
364 }
365
366 // An URI array => An URI (See uri_pickup())
367 function uri_array_implode($uri = array())
368 {
369         if (empty($uri) || ! is_array($uri)) return NULL;
370         
371         $tmp = array();
372         if (isset($uri['scheme']) && $uri['scheme'] !== '') {
373                 $tmp[] = & $uri['scheme'];
374                 $tmp[] = '://';
375         }
376         if (isset($uri['userinfo']) && $uri['userinfo'] !== '') {
377                 $tmp[] = & $uri['userinfo'];
378                 $tmp[] = '@';
379         }
380         if (isset($uri['host']) && $uri['host'] !== '') {
381                 $tmp[] = & $uri['host'];
382         }
383         if (isset($uri['port']) && $uri['port'] !== '') {
384                 $tmp[] = ':';
385                 $tmp[] = & $uri['port'];
386         }
387         if (isset($uri['path']) && $uri['path'] !== '') {
388                 $tmp[] = & $uri['path'];
389         }
390         if (isset($uri['file']) && $uri['file'] !== '') {
391                 $tmp[] = & $uri['file'];
392         }
393         if (isset($uri['fragment']) && $uri['fragment'] !== '') {
394                 $tmp[] = '#';
395                 $tmp[] = & $uri['fragment'];
396         }
397
398         return implode('', $tmp);
399 }
400
401 // ---------------------
402 // Part One : Checker
403
404
405 // TODO: globbing for IP address or something
406 // TODO: Ignore list
407 // TODO: require_or_include_once(another file)
408 function is_badhost($host = '')
409 {
410         static $blocklist_regex;
411
412         if (! isset($blocklist_regex)) {
413                 $blocklist = array(
414                         '.blogspot.com',
415                 );
416                 foreach ($blocklist as $part) {
417                         $blocklist_regex[] = '#\b' . preg_quote($part, '#') . '$#';
418                 }
419         }
420
421         foreach ($blocklist_regex as $regex) {
422                 if (preg_match($regex, $host)) {
423                         return TRUE;
424                 }
425         }
426
427         return FALSE;
428 }
429
430 // TODO return TRUE or FALSE!
431 // Simple/fast spam check
432 function is_uri_spam($target = '')
433 {
434         static $blocklist = array(
435                 '.blogspot.com',
436         );
437         static $blocklist_regex;
438
439         if (! isset($blocklist_regex)) {
440                 foreach ($blocklist as $part) {
441                         $blocklist_regex[] = '#\b' . preg_quote($part, '#') . '$#';
442                 }
443         }
444
445         $is_spam = FALSE;
446         $urinum = 0;
447
448         if (is_array($target)) {
449                 foreach($target as $str) {
450                         // Recurse
451                         list($is_spam, $_urinum) = is_uri_spam($str);
452                         $urinum += $_urinum;
453                         if ($is_spam) break;
454                 }
455         } else {
456                 $pickups = spam_uri_pickup($target);
457                 $urinum += count($pickups);
458                 if (! empty($pickups)) {
459                         // Some users want to post some URLs, but ...
460                         if ($urinum > 8) {
461                                 $is_spam = TRUE;        // Too many!
462                         } else {
463                                 foreach($pickups as $pickup) {
464                                         if ($pickup['area'] < 0) {
465                                                 $is_spam = TRUE;
466                                                 break;
467                                         }
468                                 }
469                         }
470
471                         foreach ($pickups as $pickup) {
472                                 if (is_badhost($pickup['host'])) {
473                                         $is_spam = TRUE;
474                                         break;
475                                 }
476                         }
477                 }
478         }
479
480         return array($is_spam, $urinum);
481 }
482
483 // ---------------------
484
485 // Check User-Agent (not testing yet)
486 function is_invalid_useragent($ua_name = '' /*, $ua_vars = ''*/ )
487 {
488         return $ua_name === '';
489 }
490
491 // ---------------------
492
493 // TODO: Separate check-part(s) and mail part
494 // TODO: Multi-metrics (uri, host, user-agent, ...)
495 // TODO: Mail to administrator with more measurement data?
496 // Simple/fast spam filter ($target: 'a string' or an array())
497 function pkwk_spamfilter($action, $page, $target = array('title' => ''))
498 {
499         $is_spam = FALSE;
500
501         //$is_spam =  is_invalid_useragent('NOTYET');
502         if ($is_spam) {
503                 $action .= ' (Invalid User-Agent)';
504         } else {
505                 list($is_spam) = is_uri_spam($target);
506         }
507
508         if ($is_spam) {
509                 // Mail to administrator(s)
510                 global $notify, $notify_subject;
511                 if ($notify) {
512                         $footer['ACTION'] = $action;
513                         $footer['PAGE']   = '[blocked] ' . $page;
514                         $footer['URI']    = get_script_uri() . '?' . rawurlencode($page);
515                         $footer['USER_AGENT']  = TRUE;
516                         $footer['REMOTE_ADDR'] = TRUE;
517                         pkwk_mail_notify($notify_subject,  var_export($target, TRUE), $footer);
518                         unset($footer);
519                 }
520         }
521
522         if ($is_spam) spam_exit();
523 }
524
525 // ---------------------
526
527 // Common bahavior for blocking
528 // NOTE: Call this function from various blocking feature, to disgueise the reason 'why blocked'
529 function spam_exit()
530 {
531         die("\n");
532 }
533
534 ?>