| 1 | # A plugin for automatically adding evil hosts to IPBanList based on MT throttling mechanism. |
|---|
| 2 | # |
|---|
| 3 | # $Id$ |
|---|
| 4 | # |
|---|
| 5 | # This software is provided as-is. You may use it for commercial or |
|---|
| 6 | # personal use. If you distribute it, please keep this notice intact. |
|---|
| 7 | # |
|---|
| 8 | # Copyright (c) 2006 Hirotaka Ogawa |
|---|
| 9 | # |
|---|
| 10 | package MT::Plugin::AutoIPBan; |
|---|
| 11 | |
|---|
| 12 | use strict; |
|---|
| 13 | use MT; |
|---|
| 14 | use MT::Template::Context; |
|---|
| 15 | use base 'MT::Plugin'; |
|---|
| 16 | |
|---|
| 17 | use vars qw($VERSION); |
|---|
| 18 | |
|---|
| 19 | sub BEGIN { |
|---|
| 20 | $VERSION = 0.02; |
|---|
| 21 | my $plugin = __PACKAGE__->new({ |
|---|
| 22 | name => 'AutoIPBan', |
|---|
| 23 | description => 'This plugin enables MT to add evil hosts into IPBanList based on OneHourMaxPings and OneDayMaxPings', |
|---|
| 24 | doc_link => 'http://as-is.net/wiki/AutoIPBan_Plugin', |
|---|
| 25 | author_name => 'Hirotaka Ogawa', |
|---|
| 26 | author_link => 'http://profile.typekey.com/ogawa/', |
|---|
| 27 | version => $VERSION |
|---|
| 28 | }); |
|---|
| 29 | MT->add_plugin($plugin); |
|---|
| 30 | MT->add_callback('TBPingThrottleFilter', 1, $plugin, \&tbping_auto_ipban); |
|---|
| 31 | MT::Template::Context->add_container_tag(IPBanList => \&ipbanlist); |
|---|
| 32 | MT::Template::Context->add_tag(IPBanListIP => \&ipbanlist_ip); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | sub init_app { |
|---|
| 36 | my ($plugin, $app) = @_; |
|---|
| 37 | return unless $app->isa('MT::App::CMS'); |
|---|
| 38 | $app->add_itemset_action({ |
|---|
| 39 | type => 'ping', |
|---|
| 40 | key => 'add_to_ipbanlist_ping', |
|---|
| 41 | label => 'Add To IPBanList', |
|---|
| 42 | code => \&add_to_ipbanlist_ping |
|---|
| 43 | }); |
|---|
| 44 | $app->add_itemset_action({ |
|---|
| 45 | type => 'comment', |
|---|
| 46 | key => 'add_to_ipbanlist_comment', |
|---|
| 47 | label => 'Add To IPBanList', |
|---|
| 48 | code => \&add_to_ipbanlist_comment |
|---|
| 49 | }); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | use MT::Util qw(offset_time_list); |
|---|
| 53 | use MT::TBPing; |
|---|
| 54 | use MT::IPBanList; |
|---|
| 55 | |
|---|
| 56 | sub tbping_auto_ipban { |
|---|
| 57 | my ($eh, $app, $tbping) = @_; |
|---|
| 58 | my $ip = $app->remote_ip; |
|---|
| 59 | my $blog_id = $tbping->blog_id; |
|---|
| 60 | my $now = time; |
|---|
| 61 | |
|---|
| 62 | my @ts = offset_time_list($now - 3600, $blog_id); |
|---|
| 63 | my $from = sprintf("%04d%02d%02d%02d%02d%02d", |
|---|
| 64 | $ts[5]+1900, $ts[4]+1, @ts[3,2,1,0]); |
|---|
| 65 | my $count = MT::TBPing->count({ blog_id => $blog_id, |
|---|
| 66 | ip => $ip, |
|---|
| 67 | created_on => [$from] }, |
|---|
| 68 | { range => { created_on => 1 } }); |
|---|
| 69 | if ($count >= $app->config('OneHourMaxPings')) { |
|---|
| 70 | _add_to_ipbanlist($blog_id, $ip); |
|---|
| 71 | return 0; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | @ts = offset_time_list($now - 86400, $blog_id); |
|---|
| 75 | $from = sprintf("%04d%02d%02d%02d%02d%02d", |
|---|
| 76 | $ts[5]+1900, $ts[4]+1, @ts[3,2,1,0]); |
|---|
| 77 | $count = MT::TBPing->count({ blog_id => $blog_id, |
|---|
| 78 | ip => $ip, |
|---|
| 79 | created_on => [$from] }, |
|---|
| 80 | { range => { created_on => 1 } }); |
|---|
| 81 | if ($count >= $app->config('OneDayMaxPings')) { |
|---|
| 82 | _add_to_ipbanlist($blog_id, $ip); |
|---|
| 83 | return 0; |
|---|
| 84 | } |
|---|
| 85 | 1; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | sub add_to_ipbanlist_ping { |
|---|
| 89 | my ($app) = @_; |
|---|
| 90 | my @ids = $app->param('id') |
|---|
| 91 | or return $app->error("Need pings to add to IPBanList"); |
|---|
| 92 | for my $id (@ids) { |
|---|
| 93 | my $ping = MT::TBPing->load($id, { cache_ok => 1 }); |
|---|
| 94 | _add_to_ipbanlist($ping->blog_id, $ping->ip); |
|---|
| 95 | } |
|---|
| 96 | $app->call_return; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | sub add_to_ipbanlist_comment { |
|---|
| 100 | my ($app) = @_; |
|---|
| 101 | my @ids = $app->param('id') |
|---|
| 102 | or return $app->error("Need comments to add to IPBanList"); |
|---|
| 103 | for my $id (@ids) { |
|---|
| 104 | my $comment = MT::Comment->load($id, { cache_ok => 1 }); |
|---|
| 105 | _add_to_ipbanlist($comment->blog_id, $comment->ip); |
|---|
| 106 | } |
|---|
| 107 | $app->call_return; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | sub _add_to_ipbanlist { |
|---|
| 111 | my ($blog_id, $ip) = @_; |
|---|
| 112 | unless (MT::IPBanList->load({ blog_id => $blog_id, ip => $ip })) { |
|---|
| 113 | my $ban = MT::IPBanList->new; |
|---|
| 114 | $ban->blog_id($blog_id); |
|---|
| 115 | $ban->ip($ip); |
|---|
| 116 | $ban->save or die $ban->errstr; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | sub ipbanlist { |
|---|
| 121 | my ($ctx, $args) = @_; |
|---|
| 122 | my @blog_ids = defined $args->{blog_id} ? |
|---|
| 123 | split /\s*,\s*/, $args->{blog_id} : [ $ctx->stash('blog_id') ]; |
|---|
| 124 | my %ips; |
|---|
| 125 | for my $blog_id (@blog_ids) { |
|---|
| 126 | my @list = MT::IPBanList->load({ blog_id => $blog_id }); |
|---|
| 127 | %ips = map { $_->ip => 1 } @list; |
|---|
| 128 | } |
|---|
| 129 | my @res; |
|---|
| 130 | my $builder = $ctx->stash('builder'); |
|---|
| 131 | my $tokens = $ctx->stash('tokens'); |
|---|
| 132 | for my $ip (keys %ips) { |
|---|
| 133 | local $ctx->{__stash}{'ipbanlist_ip'} = $ip; |
|---|
| 134 | defined(my $out = $builder->build($ctx, $tokens)) |
|---|
| 135 | or return $ctx->error($ctx->errstr); |
|---|
| 136 | push @res, $out; |
|---|
| 137 | } |
|---|
| 138 | my $glue = $args->{glue} || ''; |
|---|
| 139 | join $glue, @res; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | sub ipbanlist_ip { |
|---|
| 143 | $_[0]->stash('ipbanlist_ip') || ''; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | 1; |
|---|