Simple plugin to protect server against recently created fake flooder

  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
/**
* Simple plugin to protect server against recently created fake flooder.
*
* Home post:
* http://c-s.net.ua/forum/index.php?act=findpost&pid=638816
*
* Last update:
* 8/7/2014
*
* Credits:
* - Zetex for 'IP converter' stocks
*/
/* Copyright © 2014 Safety1st
BanIP Fakes is free software;
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <amxmodx>
#define PLUGIN "BanIP Fakes"
#define VERSION "0.2"
#define AUTHOR "Safety1st (modified)"
/*---------------EDIT ME------------------*/
#define MAX_SAME_IP 1 // how many players allowed with the same IP address
#define BAN_DURATION -1
new gszKickMsg[] = "Repetition IP"
#define MAX_PLAYERS 32
//#define WHITELIST_SIZE 4 // EXACTLY as rows quantity below; uncomment to enable whitelist
#if defined WHITELIST_SIZE
new const gszWhiteList[WHITELIST_SIZE][] = {
"127.0.0.0/8", // loopback interface (usually assigned IP is 127.0.0.1)
"192.168.0.0/24", // 192.168.0.0/24 subnet, IPs range 192.168.0.0 ... 192.168.0.255
"10.3.3.2/24", // 10.3.3.0/24 subnet, we could use any of its IPs here
}
#endif
/*----------------------------------------*/
#define DEBUG // uncomment to enable some messages
new gszPlayerIP[MAX_PLAYERS + 1][16]
new Trie:gtPlayerIPs
#if defined WHITELIST_SIZE
enum _:WhitelistData {
NET_IP,
NET_MASK
}
new Array:gaWhitelist
#endif
#define FIRST_PLAYER 1
#define SINGLE_PLAYER 1
public plugin_init() {
register_plugin( PLUGIN, VERSION, AUTHOR )
register_cvar( "banipfakes_version", VERSION, FCVAR_SERVER|FCVAR_SPONLY|FCVAR_UNLOGGED )
gtPlayerIPs = TrieCreate()
#if defined WHITELIST_SIZE
new iData[WhitelistData]
gaWhitelist = ArrayCreate(WhitelistData)
for( new i; i < WHITELIST_SIZE; i++ ) {
net_to_long( gszWhiteList[i], iData[NET_IP], iData[NET_MASK] )
ArrayPushArray( gaWhitelist, iData )
}
#endif
}
public client_putinserver(id) {
new szPlayerIP[16]
get_user_ip( id, szPlayerIP, charsmax(szPlayerIP), 1 /* without port */ )
#if defined WHITELIST_SIZE
new iData[WhitelistData]
for( new i; i < WHITELIST_SIZE; i++ ) {
ArrayGetArray( gaWhitelist, i, iData )
if( iData[NET_IP] == ip_to_long(szPlayerIP) & iData[NET_MASK] ) {
#if defined DEBUG
server_print( "White IP detected: id %d, IP %s", id, szPlayerIP )
#endif
return
}
}
#endif
new iQuantity = FIRST_PLAYER
if( TrieGetCell( gtPlayerIPs, szPlayerIP, iQuantity ) ) {
if( ++iQuantity > MAX_SAME_IP ) {
server_cmd( "kick #%d %s; wait; addip %d %s", get_user_userid(id), gszKickMsg, BAN_DURATION, szPlayerIP )
static szBanMsg[] = "IP %s has been banned for %d minutes"
log_amx( szBanMsg, szPlayerIP, BAN_DURATION )
}
}
TrieSetCell( gtPlayerIPs, szPlayerIP, iQuantity )
copy( gszPlayerIP[id], charsmax( gszPlayerIP[] ), szPlayerIP )
}
public client_disconnect(id) {
if( !gszPlayerIP[id][0] )
// whitelisted player or not fully initialized one (it could happen nearly a map change)
return
new iQuantity
TrieGetCell( gtPlayerIPs, gszPlayerIP[id], iQuantity )
if( iQuantity == SINGLE_PLAYER )
TrieDeleteKey( gtPlayerIPs, gszPlayerIP[id] )
else
TrieSetCell( gtPlayerIPs, gszPlayerIP[id], --iQuantity )
gszPlayerIP[id][0] = EOS
}
/*-- Modified and simplified 'IP converter stocks' by Zetex --*/
// Gets net and mask as LONG from subnet.
stock net_to_long( net_string[], &net, &mask ) {
new i, ip[16]
i = copyc( ip, charsmax(ip), net_string, '/' )
mask = i ? cidr_to_long( net_string[i + 1] ) : 0xFFFFFFFF /* mask /32, IP itself */
net = ip_to_long(ip) & mask
}
// Converts mask to LONG. Returns unsigned long.
stock cidr_to_long( mask_string[] ) {
new mask = str_to_num(mask_string)
new result = (1 << 31) >> (mask - 1)
return result
}
// Converts IP to LONG. Returns unsigned long.
stock ip_to_long( ip_string[] ) {
new right[16], part[4], octet, ip = 0
strtok( ip_string, part, 3, right, 15, '.' )
for( new i = 0; i < 4; i++ ) {
octet = str_to_num(part)
ip += octet
if( i == 3 )
break
strtok( right, part, 3, right, 15, '.' )
ip = ip << 8
}
return ip
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1049\\ f0\\ fs16 \n\\ par }
*/