This repository has been archived by the owner on Jun 14, 2019. It is now read-only.
/
KittenAuth.php
779 lines (621 loc) · 20.7 KB
/
KittenAuth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
<?php
/**
* MediaWiki KittenAuth extension
* Add a confirmation form to certain pages where users have to pick
* an image of a cat out of a list of images to prove they're not bots
* @version 1.0
* @author Litso (Main author)
*/
$wgExtensionCredits['other'][] = array(
'name' => 'KittenAuth',
'author' => 'Litso',
'descriptionmsg' => 'kittenauth-desc',
'url' => 'http://www.mediawiki.org/wiki/Extension:KittenAuth',
'version' => '0.9'
);
$wgExtensionMessagesFiles['KittenAuth'] = dirname( __FILE__ )."/KittenAuth.i18n.php";
KittenAuth::setup();
class KittenAuth {
private static $instance = null;
private $trigger = '';
private $action = '';
private $notkittens = array();
private $badLoginAttempts = 0;
/*** the following variables can be set/overridden in LocalSettings.php ***/
/**
* An array of images that are kittens (filenames only, e.g. array('1.gif', 'pic.jpg', 'image.gif') )
* MUST be set to at least one image to properly function, else a broken image will appear
*
* The plugin comes with default images but bots may pick up on these quickly, so it is advised to
* change these on installation
*/
public $kittens = array('2.png', '5.png', '7.png', '11.png', '14.png', '19.png', '22.png', '25.png');
//default is the plugin's 'images' folder as set in the __construct() function
//can be overwritten though
public $kittenDir;
//allow users with confirmed email to skip kittenauth
public $skipConfirmedEmail = false;
//max bad attempts to login before triggering kittenauth
//@TODO: make this work
public $maxBadLoginAttempts = 3;
//external urls that don't need to be kittenauth'd
public $CaptchaWhitelist = false;
//IP's that don't have to be kittenauth'd
public $IPWhitelist = false;
//run this function from LocalSettings with an array to set skipped usergroups
//example: KittenAuth::setSkippedUserGroups(array( 'user' => true, 'sysop' => false) );
public function setSkippedUserGroups($settings) {
global $wgGroupPermissions;
if(is_array($settings)) {
foreach($settings as $usergroup => $skip)
$wgGroupPermissions[$usergroup]['skipkittens'] = $skip;
}
}
//run this function from LocalSettings with an array to set kittenauth triggers
//example: KittenAuth::setKittenTriggers(array( 'edit' => false, 'addurl' => true) );
public function setKittenTriggers($settings) {
global $wgKittenTriggers;
if(is_array($settings)) {
foreach($settings as $trigger => $value)
$wgKittenTriggers[$trigger] = $value;
}
}
/**
* Initialise the KittenAuth plugin.
* @return KittenAuth the plugin object, which you can use to set settings.
*/
public static function setup() {
// check for wiki
if (!defined('MEDIAWIKI')) {
throw new Exception('This is an extension to the MediaWiki software and cannot be used standalone.');
}
// create plugin
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function __construct() {
$this->kittenDir = dirname(__FILE__);
/***************
* Permissions *
***************/
global $wgGroupPermissions, $wgKittenTriggers;
// default usergroups that don't have to pick a kitten
$wgGroupPermissions['*' ]['skipkittens'] = false;
$wgGroupPermissions['user' ]['skipkittens'] = true;
$wgGroupPermissions['autoconfirmed']['skipkittens'] = true;
$wgGroupPermissions['bot' ]['skipkittens'] = true; // registered bots
$wgGroupPermissions['sysop' ]['skipkittens'] = true;
$wgAvailableRights[] = 'skipkittens';
// default actions that require kittenauth to run
$wgKittenTriggers = array();
$wgKittenTriggers['edit'] = false; // Would check on every edit
$wgKittenTriggers['create'] = true; // Check on page creation.
$wgKittenTriggers['addurl'] = true; // Check on edits that add URLs, even if 'edit' trigger is set to false
$wgKittenTriggers['createaccount'] = true; // Special:Userlogin&type=signup
$wgKittenTriggers['badlogin'] = false; // Special:Userlogin after failure @TODO: make this work
/*********
* Add hooks *
*********/
global $wgHooks;
if ( defined( 'MW_SUPPORTS_EDITFILTERMERGED' ) ) {
$wgHooks['EditFilterMerged'][] = $this;
} else {
$wgHooks['EditFilter'][] = $this;
}
$wgHooks['UserCreateForm'][] = $this;
$wgHooks['AbortNewAccount'][] = $this;
$wgHooks['LoginAuthenticateAudit'][] = $this;
$wgHooks['UserLoginForm'][] = $this;
$wgHooks['AbortLogin'][] = $this;
}
/*********
* Hooks *
*********/
/**
* The main callback run on edit attempts.
* @param EditPage $editPage
* @param string $newtext
* @param string $section
* @param bool $merged
* @return bool true to continue saving, false to abort and show KittenAuth
*/
public function onEditFilter( $editPage, $newtext, $section, $merged = false ) {
wfDebug("KittenAuth: onEditFilter hook activated (merged = $merged) \n");
if ( defined( 'MW_API' ) ) { wfDebug("MW_API true \n");
# API mode
# The Kitten was already checked and approved
return true;
}
if ( !$this->doConfirmEdit( $editPage, $newtext, $section, $merged ) ) { wfDebug("doConfirmEdit false \n");
$editPage->showEditForm( array( &$this, 'editCallback' ) );
return false;
}
return true;
}
/**
* A more efficient edit filter callback based on the text after section merging
* @param EditPage $editPage
* @param string $newtext
*/
public function onEditFilterMerged( $editPage, $newtext, $section, $merged = false ) {
return $this->onEditFilter( $editPage, $newtext, false, true );
}
/**
* Inject kittenauth in user create formn
* @fixme if multiple thingies insert a header, could break
* @param SimpleTemplate $template
* @return bool true to keep running callbacks
*/
public function onUserCreateForm( &$template ) {
wfDebug("KittenAuth: onUserCreateForm hook activated \n");
global $wgKittenTriggers, $wgOut, $wgUser;
if ( $wgKittenTriggers['createaccount'] ) {
if ( $wgUser->isAllowed( 'skipkittens' ) ) {
wfDebug( "KittenAuth: user group allows skipping kittenauth on account creation \n" );
return true;
}
$this->addCSS(&$wgOut);
$template->set( 'header',
"<div class='kittens'>" .
$wgOut->parse( $this->getMessage( 'createaccount' ) ) .
$this->getForm() .
"</div> \n" );
}
return true;
}
/**
* Hook for user creation form submissions.
* @param User $u
* @param string $message
* @return bool true to continue, false to abort user creation
*/
function onAbortNewAccount( $u, &$message ) {
global $wgKittenTriggers, $wgUser;
if ( $wgKittenTriggers['createaccount'] ) {
if ( $wgUser->isAllowed( 'skipkittens' ) ) {
wfDebug( "KittenAuth: user group allows skipping captcha on account creation \n" );
return true;
}
if ( $this->isIPWhitelisted() )
return true;
$this->trigger = "new account '" . $u->getName() . "'";
if ( !$this->passKittenAuth() ) {
$message = wfMsg( 'kittenauth-createaccount-fail' );
return false;
}
}
return true;
}
/**
* Inject a KittenAuth into the user login form after a failed
* password attempt as a speedbump for mass attacks.
* @fixme if multiple thingies insert a header, could break
* @param SimpleTemplate $template
* @return bool true to keep running callbacks
*/
function onUserLoginForm( &$template ) {
wfDebug("KittenAuth: onUserLoginForm hook activated \n");
if ( $this->isBadLoginTriggered()) {
global $wgOut;
$this->addCSS(&$wgOut);
$template->set( 'header',
"<div class='kittens'>" .
$wgOut->parse( $this->getMessage( 'badlogin' ) ) .
$this->getForm() .
"</div> \n" );
}
return true;
}
/**
* Hook for user login form submissions.
* @param User $u
* @param string $message
* @return bool true to continue, false to abort user creation
*/
function onAbortLogin( $u, $pass, &$retval ) {
if ( $this->isBadLoginTriggered() ) {
if ( $this->isIPWhitelisted() )
return true;
$this->trigger = "post-badlogin login '" . $u->getName() . "'";
if ( !$this->passKittenAuth() ) {
$message = wfMsg( 'kittenauth-badlogin-fail' );
// Emulate a bad-password return to confuse the shit out of attackers
$retval = LoginForm::WRONG_PASS;
return false;
}
}
return true;
}
/**
* When a bad login attempt is made, increment an expiring counter
* in the memcache cloud. Later checks for this may trigger a
* captcha display to prevent too many hits from the same place.
* @param User $user
* @param string $password
* @param int $retval authentication return value
* @return bool true to keep running callbacks
*/
function onLoginAuthenticateAudit( $user, $password, $retval ) {
global $wgKittenTriggers, $wgMemc;
if ( $retval == LoginForm::WRONG_PASS && $wgKittenTriggers['badlogin'] ) {
$key = wfMemcKey( 'kittenauth', 'badlogin', 'ip', wfGetIP() );
$count = $wgMemc->get( $key );
if ( !$count ) {
$wgMemc->add( $key, 0, 5*60 );
}
$count = $wgMemc->incr( $key );
}
return true;
}
/*************
* Functions *
*************/
private function getNotKittens() {
$folder = opendir($this->kittenDir . "/images"); // Use 'opendir(".")' if the PHP file is in the same folder as your images. Or set a relative path 'opendir("../path/to/folder")'.
$pic_types = array("jpg", "jpeg", "gif", "png");
$notkittens = array();
while ($file = readdir ($folder)) {
if(in_array(substr(strtolower($file), strrpos($file,".") + 1),$pic_types) && !in_array($file, $this->kittens))
{
array_push($notkittens,$file);
}
}
closedir($folder);
return $notkittens;
}
/**
* Backend function for onEditFilter() and onEditFilterMerged()
* @return bool false if the CAPTCHA is rejected, true otherwise
*/
private function doConfirmEdit( $editPage, $newtext, $section, $merged = false ) {
if ( $this->shouldCheck( $editPage, $newtext, $section, $merged ) ) {
return $this->passKittenAuth();
} else {
wfDebug( "KittenAuth: no need to show captcha. \n" );
return true;
}
}
/**
* @param EditPage $editPage
* @param string $newtext
* @param string $section
* @return bool true if the captcha should run
*/
function shouldCheck( &$editPage, $newtext, $section, $merged = false ) {
wfDebug("KittenAuth: function shouldCheck() started \n");
global $wgKittenTriggers;
$this->trigger = '';
$title = $editPage->mArticle->getTitle();
global $wgUser;
if ( $wgUser->isAllowed( 'skipkittens' ) ) {
wfDebug( "KittenAuth: user group allows skipping captcha \n" );
return false;
}
if ( $this->isIPWhitelisted() )
return false;
global $wgEmailAuthentication;
if ( $wgEmailAuthentication && $this->skipConfirmedEmail && $wgUser->isEmailConfirmed() ) {
wfDebug( "KittenAuth: user has confirmed mail, skipping captcha \n" );
return false;
}
if ( $wgKittenTriggers['edit'] ) {
// Check on all edits
global $wgUser;
$this->trigger = sprintf( "edit trigger by '%s' at [[%s]]",
$wgUser->getName(),
$title->getPrefixedText() );
$this->action = 'edit';
wfDebug( "KittenAuth: checking all edits... \n" );
return true;
}
if ( $wgKittenTriggers['create'] && !$editPage->mTitle->exists() ) {
// Check if creating a page
global $wgUser;
$this->trigger = sprintf( "Create trigger by '%s' at [[%s]]",
$wgUser->getName(),
$title->getPrefixedText() );
$this->action = 'create';
wfDebug( "KittenAuth: checking on page creation... \n" );
return true;
}
if ( $wgKittenTriggers['addurl'] ) {
wfDebug("KittenAuth: checking for added urls \n");
// Only check edits that add URLs
if ( $merged ) {
wfDebug("KittenAuth: \$merged = true \n");
// Get links from the database
$oldLinks = $this->getLinksFromTracker( $title );
// Share a parse operation with Article::doEdit()
$editInfo = $editPage->mArticle->prepareTextForEdit( $newtext );
$newLinks = array_keys( $editInfo->output->getExternalLinks() );
} else {
wfDebug("KittenAuth: \$merged = false \n");
// Get link changes in the slowest way known to man
$oldtext = $this->loadText( $editPage, $section );
$oldLinks = $this->findLinks( $editPage, $oldtext );
$newLinks = $this->findLinks( $editPage, $newtext );
}
$unknownLinks = array_filter( $newLinks, array( &$this, 'filterLink' ) );
$addedLinks = array_diff( $unknownLinks, $oldLinks );
$numLinks = count( $addedLinks );
if ( $numLinks > 0 ) {
wfDebug("KittenAuth: urls were added \n");
global $wgUser;
$this->trigger = sprintf( "%dx url trigger by '%s' at [[%s]]: %s",
$numLinks,
$wgUser->getName(),
$title->getPrefixedText(),
implode( ", ", $addedLinks ) );
$this->action = 'addurl';
return true;
}
}
return false;
}
/***** KittenAuth verification functions *****/
/**
* Given a required captcha run, test form input for correct
* input on the open session.
* @return bool if passed, false if failed or new session
*/
function passKittenAuth() {
$token = $this->getToken();
if(isset($_POST['kitten']) && isset($_POST['kAuth'])) {
$kitten = $_POST['kitten'];
$kAuth = $_POST['kAuth'];
//check if the right image was selected
if( md5($kitten . $token) == $kAuth) {
wfDebug("Passed kitten validation! \n");
return true;
}
}
wfDebug("Failed kitten validation \n");
return false;
}
/**
* Get a token to hash the kitten key with
* Make it harder for bots to figure out the right answer
*
* @return bool
* @access private
*/
private function getToken() {
global $wgArticle, $wgTitle;
if(isset($wgArticle->mTitle->mArticleID))
return $wgArticle->mTitle->mArticleID;
else {
return $wgTitle->mUrlform;
}
}
/**
* Check if the IP is allowed to skip captchas
*/
function isIPWhitelisted() {
if ( $this->IPWhitelist ) {
$ip = wfGetIp();
foreach ( $this->IPWhitelist as $range ) {
if ( IP::isInRange( $ip, $range ) ) {
return true;
}
}
}
return false;
}
/***** KittenAuth form insertion functions *****/
/**
* Insert the captcha prompt into an edit form.
* @param OutputPage $out
*/
function editCallback( &$out ) {
$out->addWikiText( $this->getMessage( $this->action ) );
$out->addHTML( $this->getForm() );
$this->addCSS(&$out);
}
/**
* Show a message asking the user to enter a captcha on edit
* The result will be treated as wiki text
*
* @param $action Action being performed
* @return string
*/
function getMessage( $action ) {
$name = 'kittenauth-' . $action;
$text = wfMsg( $name );
if(empty($this->kittens)) {
$text .= "<br />'''" . wfMsg( 'kittenauth-nokittens' ) . "'''";
}
# Obtain a more tailored message, if possible, otherwise, fall back to
# the default for edits
return wfEmptyMsg( $name, $text ) ? wfMsg( 'kittenauth-edit' ) : $text;
}
/**
* Insert a captcha prompt into the edit form.
* This sample implementation generates a simple arithmetic operation;
* it would be easy to defeat by machine.
*
* Override this!
*
* @return string HTML
*/
function getForm() {
global $wgScriptPath;
$token = $this->getToken();
$this->notkittens = $this->getNotKittens();
//get 1 kitten
$randomKitten = $this->kittens[rand(1,count($this->kittens)) - 1];
//get non-kitten images
$randomImages = array();
$rand = array_rand($this->notkittens, 4); //4 random keys
foreach($rand as $key) //get the values
$randomImages[] = $this->notkittens[$key];
//combine and shuffle the images to display a random list later
$combined = $randomImages;
$combined[] = $randomKitten;
shuffle($combined);
//make make into form
$out = '<div id="KittenAuth">';
foreach($combined as $image) {
$out .= "
<label>
<img src='$wgScriptPath/extensions/KittenAuth/images/$image' height='50' width='50' />
<input name='kitten' type='radio' value='$image' tabindex='1' />
</label>";
}
//add hidden element with md5 hash
$out .= Xml::element(
'input', array(
'type' => 'hidden',
'name' => 'kAuth',
'id' => 'kAuth',
'value' => md5($randomKitten . $token)
) );
$out .= "</div>";
return $out;
}
private function addCSS(&$out) {
$out->addScript( '
<style type="text/css">
/* clear the float without adding an extra div */
#KittenAuth:after {
content: "\0020";
display: block;
height: 0;
clear: both;
visibility: hidden;
overflow:hidden;
}
#KittenAuth label {
float: left;
margin-bottom: .5em;
}
#KittenAuth img {
display: block;
cursor: pointer;
margin: 3px;
}
#KittenAuth input[type=radio] {
width: 50px;
}
</style>' );
}
/***** External URL functions *****/
/**
* Load external links from the externallinks table
*/
function getLinksFromTracker( $title ) {
$dbr =& wfGetDB( DB_SLAVE );
$id = $title->getArticleId(); // should be zero queries
$res = $dbr->select( 'externallinks', array( 'el_to' ),
array( 'el_from' => $id ), __METHOD__ );
$links = array();
while ( $row = $dbr->fetchObject( $res ) ) {
$links[] = $row->el_to;
}
return $links;
}
/**
* Retrieve the current version of the page or section being edited...
* @param EditPage $editPage
* @param string $section
* @return string
* @access private
*/
function loadText( $editPage, $section ) {
$rev = Revision::newFromTitle( $editPage->mTitle );
if ( is_null( $rev ) ) {
return "";
} else {
$text = $rev->getText();
if ( $section != '' ) {
return Article::getSection( $text, $section );
} else {
return $text;
}
}
}
/**
* Extract a list of all recognized HTTP links in the text.
* @param string $text
* @return array of strings
*/
function findLinks( &$editpage, $text ) {
global $wgParser, $wgUser;
$options = new ParserOptions();
$text = $wgParser->preSaveTransform( $text, $editpage->mTitle, $wgUser, $options );
$out = $wgParser->parse( $text, $editpage->mTitle, $options );
return array_keys( $out->getExternalLinks() );
}
/**
* Filter callback function for URL whitelisting
* @param string url to check
* @return bool true if unknown, false if whitelisted
* @access private
*/
function filterLink( $url ) {
$source = wfMsgForContent( 'captcha-addurl-whitelist' );
$whitelist = wfEmptyMsg( 'captcha-addurl-whitelist', $source )
? false
: $this->buildRegexes( explode( "\n", $source ) );
$cwl = $this->CaptchaWhitelist !== false ? preg_match( $this->CaptchaWhitelist, $url ) : false;
$wl = $whitelist !== false ? preg_match( $whitelist, $url ) : false;
return !( $cwl || $wl );
}
/**
* Build regex from whitelist
* @param string lines from [[MediaWiki:Captcha-addurl-whitelist]]
* @return string Regex or bool false if whitelist is empty
* @access private
*/
function buildRegexes( $lines ) {
# Code duplicated from the SpamBlacklist extension (r19197)
# Strip comments and whitespace, then remove blanks
$lines = array_filter( array_map( 'trim', preg_replace( '/#.*$/', '', $lines ) ) );
# No lines, don't make a regex which will match everything
if ( count( $lines ) == 0 ) {
wfDebug( "No lines\n" );
return false;
} else {
# Make regex
# It's faster using the S modifier even though it will usually only be run once
// $regex = 'http://+[a-z0-9_\-.]*(' . implode( '|', $lines ) . ')';
// return '/' . str_replace( '/', '\/', preg_replace('|\\\*/|', '/', $regex) ) . '/Si';
$regexes = '';
$regexStart = '/^https?:\/\/+[a-z0-9_\-.]*(';
$regexEnd = ')/Si';
$regexMax = 4096;
$build = false;
foreach ( $lines as $line ) {
// FIXME: not very robust size check, but should work. :)
if ( $build === false ) {
$build = $line;
} elseif ( strlen( $build ) + strlen( $line ) > $regexMax ) {
$regexes .= $regexStart .
str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
$regexEnd;
$build = $line;
} else {
$build .= '|' . $line;
}
}
if ( $build !== false ) {
$regexes .= $regexStart .
str_replace( '/', '\/', preg_replace( '|\\\*/|', '/', $build ) ) .
$regexEnd;
}
return $regexes;
}
}
/***** Login functions *****/
/**
* Check if a bad login has already been registered for this
* IP address. If so, require a kitten.
* @return bool
* @access private
*/
function isBadLoginTriggered() {
global $wgMemc;
return intval( $wgMemc->get( wfMemcKey( 'kittenauth', 'badlogin', 'ip', wfGetIP() ) ) ) >= $this->maxBadLoginAttempts;
}
}