基于GeoIP构建私有IP位置信息库

geoip简介

GeoIP是MaxMind公司维护的一个广泛使用IP位置数据库,能够将IP地址映射到具体位置,包括国家/地区,ISP和城市等。可用于以基于地理位置的目标广告、Web日志和统计信息、垃圾邮件检测、访问控制等方案。 MaxMind提供的API可以与C,Java和PHP集成在一起,以访问GeoIP数据库,简化开发工作。

构建私有IP位置信息库

通常GeoIP可以基本满足需求,如需要更精确的信息,可以购买商业的IP地理数据库如国内IPIP库。但对于大中型信息内网,主要需求变成根据IP地址定位到具体的资产、业务区域等,最好的情况是将私有的地址信息整合进GeoIP库,可不对应用端进行修改。

准备工作

在扩展之前,需要提前准备好增加的IP地址范围及对应显示的字段。如

  • 信息系统IP(桌面、服务器、网络设备)
  • 物联网、信息采集终端 确定好后,需要形成IP-信息的对应关系表,建议cvs格式

下载并安装GeoIPLite Database

GeoIP有免费和收费两种,通常免费版已经可以满足需求

wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/GeoLiteCityv6.dat.gz
gunzip GeoLiteCity.dat.gz
gunzip GeoLiteCityv6.dat.gz

// 创建软链接

ln -s /usr/share/GeoIP/GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
ln -s /usr/share/GeoIP/GeoLiteCityv6.dat /usr/share/GeoIP/GeoIPCityv6.dat

创建私有数据库

如果仅查询内部私有地址,可以创建新的类似GeoIP库(即MaxMindDB地址库格式)。下方脚本使用一个ipdata.csv文件(首行不留标题),每行记录的是一个IP段的开始IP,结束IP,归属国家、省、市、街道、运营商、其他注释,可根据实际情况、业务层级等建立相应的数据库。

use MaxMind::DB::Writer::Tree;


my %types = (
    org => 'utf8_string',
    desc => 'utf8_string',
);

my $tree = MaxMind::DB::Writer::Tree->new(
    ip_version            => 4,
    record_size           => 24,
    database_type         => 'My-IP-Data',
    languages             => ['zh-CN'],
    description           => { zh-CN => 'test mmdb' },
    map_key_type_callback => sub { $types{ $_[0] } },
);

open my $rfh, "<", $ARGV[0];
while (my $line = <$rfh>) {
    chomp $line;
    my @fields = split "," , $line;

    $tree->insert_network(
    $fields[0],{
     org => $fields[1],
         desc => $fields[2],
        },
    );
   
}

open my $fh, '>:raw', 'ip-data.mmdb';
$tree->write_tree($fh);

执行脚本

程序使用 MaxMind::DB::Writer 模块,请先通过 cpan 安装:

yum install -y perl-libs perl-corelist

debian:
apt-get install perl-modules make autoconf

wget https://cpanmin.us -O /usr/sbin/cpanm
chmod +x /usr/sbin/cpanm
cpanm MaxMind::DB::Writer

wget https://raw.githubusercontent.com/CNSRE/rsyslog-maxminddb/master/util/gen_mmdb.pl
perl util/gen_mmdb.pl ipdata.csv

注: 需要有 make cmake autoconf等编译链

perl常见问题

  1. Can’t locate ExtUtils/Manifest.pm in @INC (you may need to install the ExtUtils::Manifest module)

缺少perl-ExtUtils-Manifest

yum install perl-devel

  1. Installing the dependencies failed: Module ‘Test::More’ is not installed

yum install automake

私有地址段

保存完成后发现脚本默认不保存私有地址,需要设置Net::Works::Network模块中私有地址范围:

修改/usr/local/share/perl/5.24.1/Net/Works/Network.pm

{
    my @reserved_4 = qw(
        0.0.0.0/8
        10.0.0.0/8
        100.64.0.0/10
        127.0.0.0/8
        
        

使用instmodsh查找Tree.pm的位置 /usr/local/lib/x86_64-linux-gnu/perl/5.24.1/MaxMind/DB/Writer/Tree.pm

has remove_reserved_networks => (
    is      => 'ro',
    isa     => 'Bool',
    default => 1,
);

default => '0',

default => '',

测试

可以使用类似命令测试查询新建的数据库

apt-get install mmdb-bin

/usr/local/bin/mmdblookup --file /mnt/data/geolite2/GeoLite2-City.mmdb --ip 112.225.35.70   

相关工具

创建 使用perl脚本 Perl MMDB database writer

读取 可以采用多种语言读取工具 若geoip格式,可以采用读取ip工具


参考:


最后修改于 2021-12-16