IT/server

[ubuntu]_iptable geoip 설정

Ne...Pa~~~~~ 2014. 3. 3. 12:13

iptable GEOIP 적용


기준 ubuntu 12.04


설치


sudo aptitude install xtables-addons-common libtext-csv-xs-perl


sudo mkdir /usr/share/xt_geoip 


sudo cp update.sh /usr/share/xt_geoip


sudo cp xt_geoip_build.pl /usr/share/xt_geoip


sudo /usr/share/xt_geoip/update.sh




GEOIP 지속적 업데이트(update.sh) 주기적으로 geolite사이트에서 업로드된 파일 다운로드

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
#/bin/sh
 
cd $(dirname $0) || exit 1;
BASE_DIR=$(pwd);
DL_DIR=$BASE_DIR/dl
 
mkdir -p $DL_DIR >/dev/null 2>&1
cd $DL_DIR || exit 1;
 
# Get data (only if modified)
wget -N http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz || exit 1;
wget -N http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip || exit 1;
 
if [ -f GeoIPv6.csv.gz ]; then
rm GeoIPv6.csv >/dev/null 2>&1
gzip -dc GeoIPv6.csv.gz > GeoIPv6.csv || exit 1;
fi
 
if [ -f GeoIPCountryCSV.zip ]; then
rm GeoIPCountryWhois.csv >/dev/null 2>&1
unzip GeoIPCountryCSV.zip >/dev/null || exit 1;
fi
 
echo "Building ..."
cat *.csv | perl $BASE_DIR/xt_geoip_build.pl -D $DL_DIR || exit 1;
rm *.csv
 
echo "Copying to $BASE_DIR ..."
rm -rf $BASE_DIR/LE && mv -f $DL_DIR/LE $BASE_DIR/
rm -rf $BASE_DIR/BE && mv -f $DL_DIR/BE $BASE_DIR/
echo "Ready."
 




xt_geoip_build.pl - 실제 폴더 및 파일 생성


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
#!/usr/bin/perl
#
# Converter for MaxMind CSV database to binary, for xt_geoip
# Copyright Aⓒ Jan Engelhardt <jengelh@medozas.de>, 2008-2011
#
# Use -b argument to create big-endian tables.
#
use Getopt::Long;
use IO::Handle;
use Text::CSV_XS; # or trade for Text::CSV
use strict;
 
my $csv = Text::CSV_XS->new({
allow_whitespace => 1,
binary => 1,
eol => $/,
}); # or Text::CSV
my $target_dir = ".";
 
&Getopt::Long::Configure(qw(bundling));
&GetOptions(
"D=s" => \$target_dir,
);
 
if (!-d $target_dir) {
print STDERR "Target directory $target_dir does not exist.\n";
exit 1;
}
foreach (qw(LE BE)) {
my $dir = "$target_dir/$_";
if (!-e $dir && !mkdir($dir)) {
print STDERR "Could not mkdir $dir: $!\n";
exit 1;
}
}
 
&dump(&collect());
 
sub collect
{
my %country;
 
while (my $row = $csv->getline(*ARGV)) {
if (!defined($country{$row->[4]})) {
$country{$row->[4]} = {
name => $row->[5],
pool_v4 => [],
pool_v6 => [],
};
}
my $c = $country{$row->[4]};
if ($row->[0] =~ /:/) {
push(@{$c->{pool_v6}},
[&ip6_pack($row->[0]), &ip6_pack($row->[1])]);
} else {
push(@{$c->{pool_v4}}, [$row->[2], $row->[3]]);
}
if ($. % 4096 == 0) {
print STDERR "\r\e[2K$. entries";
}
}
 
print STDERR "\r\e[2K$. entries total\n";
return \%country;
}
 
sub dump
{
my $country = shift @_;
 
foreach my $iso_code (sort keys %$country) {
&dump_one($iso_code, $country->{$iso_code});
}
}
 
sub dump_one
{
my($iso_code, $country) = @_;
my($file, $fh_le, $fh_be);
 
printf "%5u IPv6 ranges for %s %s\n",
scalar(@{$country->{pool_v6}}),
$iso_code, $country->{name};
 
$file = "$target_dir/LE/".uc($iso_code).".iv6";
if (!open($fh_le, "> $file")) {
print STDERR "Error opening $file: $!\n";
exit 1;
}
$file = "$target_dir/BE/".uc($iso_code).".iv6";
if (!open($fh_be, "> $file")) {
print STDERR "Error opening $file: $!\n";
exit 1;
}
foreach my $range (@{$country->{pool_v6}}) {
print $fh_be $range->[0], $range->[1];
print $fh_le &ip6_swap($range->[0]), &ip6_swap($range->[1]);
}
close $fh_le;
close $fh_be;
 
printf "%5u IPv4 ranges for %s %s\n",
scalar(@{$country->{pool_v4}}),
$iso_code, $country->{name};
 
$file = "$target_dir/LE/".uc($iso_code).".iv4";
if (!open($fh_le, "> $file")) {
print STDERR "Error opening $file: $!\n";
exit 1;
}
$file = "$target_dir/BE/".uc($iso_code).".iv4";
if (!open($fh_be, "> $file")) {
print STDERR "Error opening $file: $!\n";
exit 1;
}
foreach my $range (@{$country->{pool_v4}}) {
print $fh_le pack("VV", $range->[0], $range->[1]);
print $fh_be pack("NN", $range->[0], $range->[1]);
}
close $fh_le;
close $fh_be;
}
 
sub ip6_pack
{
my $addr = shift @_;
$addr =~ s{::}{:!:};
my @addr = split(/:/, $addr);
my @e = (0) x 8;
foreach (@addr) {
if ($_ eq "!") {
$_ = join(':', @e[0..(8-scalar(@addr))]);
}
}
@addr = split(/:/, join(':', @addr));
$_ = hex($_) foreach @addr;
return pack("n*", @addr);
}
 
sub ip6_swap
{
return pack("V*", unpack("N*", shift @_));
}


세팅 완료


xt_geoip_build.pl 파일 구동 시키면 폴더 및 내용이 생성 된다


파일 생성 후 


 cat /proc/net/ip_tables_matches 


geoip

state

udplite

udp

tcp

icmp


확인


이제 iptable에 적용 시키면 완료


쉽게 모든 이더넷에 한국 아이피 외 전부 허용 안함!!!!!!! 하면


iptables -A INPUT -i eth+ -m geoip ! --source-country KR -j DROP


이렇게 드롭시키면 댄다.