$7.49 .com domain with free InstantPage Website Builder$1.99 Web Hosting   捷飞网络官方淘宝店   Godaddy 优惠码
返回列表 发帖
分享到:




[安全] 如何用PortSentry 防止恶意扫描 保护Linux服务器

在遭遇Linux服务器被外部IP恶意扫描端口的时候,一般系统管理员会部署一些防入侵的环境,比如snort,不过snort部署起来比较复杂,而有时候我们只是需要防止恶意扫描而已。这个时候,可以用PortSentry这个工具来进行非常简单的实现。虽然PortSentry被Cisco收购后不再开发,但丝毫不影响此软件的强大功能。
( o9 a1 S0 U$ B    我在检查一台CentOS5.5服务器安全环境时,发现很多IP在恶意扫描此服务器的端口。本来想部署snort防入侵环境的,后来发现snort环境部署非常复杂,而以上的恶意扫描完全可以用PortSentry来实现。PortSentry是入侵检测工具中配置最简单、效果最直接的工具之一。PortSentry是Abacus工程的一个组成部分。Abacus工程的目标是建立一个基于主机的网络入侵检测系统,可以从http://www.psonic.com得到关于Abacus工程更为详细的信息。虽然PortSentry被Cisco收购后不再开发,但丝毫不影响此软件的强大功能。PortSentry可以实时检测几乎所有类型的网络扫描,并对扫描行为做出反应。一旦发现可疑的行为,PortSentry可以采取如下一些特定措施来加强防范:
& F7 Q- u" i8 z7 m    ◆给出虚假的路由信息,把所有的信息流都重定向到一个不存在的主机;' a+ h- O2 g( f( P5 E3 l9 w: M
    ◆自动将对服务器进行端口扫描的主机加到TCP-Wrappers的/etc/hosts.deny文件中去,我个人比较喜欢这种方式,因为线上许多环境并非都能打开iptables,这个选项也是PortSentry默认的功能;- }; D+ I( a0 J' A- S1 J  V
    ◆利用Netfilter机制,用包过滤程序,比如iptables和ipchain等,把所有非法数据包(来自对服务器进行端口扫描的主机)都过滤掉;7 i+ Y3 O9 L+ @$ ]* s9 U
    ◆通过syslog()函数给出一个目志消息,甚至可以返回给扫描者一段警告信息。1 p4 g! `1 @) B' B1 P
    一、PortSentry的安装5 Y$ A4 b3 F- q& M: N
    下面详细介绍PortSentry工具的安装和配置方法。: ~  g$ n) o8 c3 B
    1.从http://sourceforge.net/projects/sentrytools/下载软件的最新版portsentry-1.2.tar.gz,用root用户执行如下命令进行安装:0 X( T  O9 `) w7 F" ]- C
    #tarzxvfportsentry-1.2.tar.gz
4 u) u2 E: l0 w4 g( W4 w9 t    #cdportsentry-1.2_beta" f8 f" c- c/ r6 Z2 W
    #make4 U# `$ d) B- b4 p- ^
    #makeinstall
$ R5 x+ ~, t/ ]8 J9 x0 J$ R    进行到这步时发现报错,系统生成不了protsentry执行文件,我们查看Makefile文件时发现,make后面根据操作系统的不同有许多选项。( r! M9 J" k4 f' ~- ^" \& {1 e1 n
    所以我们重新执行此步操作,将目录删除重新解压缩
1 ~3 s; _/ c6 H- Y8 \    然后我们执行makelinux,发现系统仍然报错,如下:& d3 f' t6 L9 ]8 v: W* u
    SYSTYPE=linux* m( g" q! [( s6 T4 j
    Making./portsentry.c:1585:error:missingterminating"character: Q( a0 S$ n; D8 M
    ./portsentry.c:1595:error:expected?.?.before?.?.token
8 W' o" [8 ]+ j: ^6 @, i, j    make:***[linux]Error1解决方法:
) H6 x7 O% q8 H) f) t    我们打开portsentry.c文件,在1590行左右,我们将带有Copyright1997-2003字样的那行调整为一行即可,文字有白线标记的那行代码应调整为一行5 W- p& G% O& P) i
    调整后我们再执行makelinux&&makeinstall后,PortSentry顺利安装成功,其安装路径为/usr/local/psionic/portsentry,如下所示表示成功安装此软件:
" |) y2 @1 f# P! T+ K    Edit/usr/local/psionic/portsentry/portsentry.confandchange
+ o  F* ]' y0 G! O/ x$ T9 ?4 u6 {    yoursettingsifyouhaven'talready.(route,etc)7 Q3 @; c7 A7 d; ]; Q
    WARNING:Thisversionandabovenowuseanew
# y* y% g9 A- G$ j. X. V    directorystructureforstoringtheprogram$ x) \. {: D& W9 N' y/ k' \( o
    andconfigfiles(/usr/local/psionic/portsentry)。
) c  `# k+ o" }) Y8 P9 A, S    Pleasemakesureyoudeletetheoldfileswhen2 |* S" w: q5 ?1 w+ j! g( x
    thetestingofthisinstalliscomplete.
, O- z( v3 A. K7 }- F" y3 ^9 n    cc-O-Wall-DLINUX-DSUPPORT_STEALTH-o./portsentry./portsentry.c\
/ V+ T8 x8 v8 Q; @8 O    ./portsentry_io.c./portsentry_util.c
/ {. \6 D2 [' O: L* }    ./portsentry.c:Infunction?.ortSentryModeTCP?.
; N9 c0 J0 |7 z; J2 a* |    ./portsentry.c:1187:warning:pointertargetsinpassingargument3of?.ccept?.differinsignedness9 q( f+ i6 w- M* G* f
    ./portsentry.c:Infunction?.ortSentryModeUDP?.* k9 ?; I' L" s, b
    ./portsentry.c:1384:warning:pointertargetsinpassingargument6of?.ecvfrom?.differinsignedness8 w7 Y, e0 ~1 g8 O4 L, C
    ./portsentry.c:Infunction?.sage?.
7 I8 k1 r2 w" \5 {$ ]  s    ./portsentry.c:1584:error:missingterminating"character
% g3 G" [4 Q2 p; F5 z/ Z    ./portsentry.c:1585:error:?.ourceforget?.undeclared(firstuseinthisfunction)& w& E, s# n% D
    ./portsentry.c:1585:error:(Eachundeclaredidentifierisreportedonlyonce
( q! q8 o2 q5 {1 Q    ./portsentry.c:1585:error:foreachfunctionitappearsin.)" ~+ `" w% [* o
    ./portsentry.c:1585:error:expected?.?.before?.ot?
6 O+ A% E2 |/ l6 ~1 z7 f1 ^4 x" P, D    ./portsentry.c:1585:error:stray?.?.inprogram
$ q* K' t# @6 w7 r- ~6 u9 M( u7 r    二、PortSentry的配置1 ]1 u; I% F, e! ?5 J4 N
    1.修改配置文件portsentry.conf
% b; L8 G0 ?& i4 H    通过PortSentry进行入侵检测,首先需要为它定制一份需要监视的端口清单,以及相应的阻止对策。然后启动后台进程对这些端口进行检测,一旦发现有人扫描这些端口,就启动相应的对策进行阻拦。
. N9 ~+ P3 \4 O0 q  b    (1)设置端口清单
- c9 K. k. g( ]% U2 t& o* Q6 ]    下面给出portsentry.conf中关于端口的默认配置情况:
( _* z$ h- a; K: }    #Un-commenttheseifyouarereallyanal;; t& B& [$ n! z/ v* K9 ?6 K
    #TCP_PORTS="1,7,9,11,15,70,79,80,109,110,111,119,138,139,143,512,513,514,515,540,636,1080,1424,2000,2001,[]
$ @' h$ z# l$ n' W$ C    #UDP_PORTS="1,7,9,66,67,68,69,111,137,138,161,162,474,513,517,518,635,640,641,666,700,2049,31335,27444,34555,[]
& D! a! g$ |5 U- o# Q' ~2 Z: }    #Usetheseifyoujustwanttobeaware:3 [: m# s7 }& V' `! ]# {4 B. K
    TCP_PORTS="1,11,15,79,111,119,143,540,635,1080,1524,2000,5742,6667,12345,12346,20034,27665,31337,32771,32772,[]& X. V) P, G) c6 B3 K
    UDP_PORTS="1,7,9,69,161,162,513,635,640,641,700,37444,34555,31335,32770,32771,32772,32773,32774,31337,54321"7 ?/ W/ Q! z- M, j/ U
    #Usetheseforjusebare-bones
/ s3 O5 Z# e# Q& @# l' x4 s& X    #TCP_PORTS="1,11,15,110,111,143,540,635,180,1524,2000,12345,12346,20034,32771,32772,32773,32774,49724,54320"4 f4 [% M6 d- ~/ t: v' R
    #UDP_PORTS="1,7,9,69,161,162,513,640,700,32770,32771,32772,32773,32774,31337,54321"
) i8 X% z$ ]' q! U" p    可以有选择地去掉前面的注释来启用默认配置,也可以根据自己的实际情况定制一份新的清单,格式和原来的一样即可。端口列表要根据具体情况而定,假如服务器为Web服务器,那么Web端口就不需要监视。反之,如果是FTP服务器,那么监视Web端口也是有必要的。
, h1 x' ]7 G3 _- s    (2)portsentry.conf里的相关文件  ^; ^! A4 d/ B/ s% e
    在portsentry.conf中自动配置了许多文件,我们看下它们有哪些用途:
3 c  z3 F+ O7 \+ @: _    #此文件记录允许合法扫描服务器的主机地址$ i7 X; S6 G8 i: @, F9 q: p2 c
    IGNORE_FILE="/usr/local/psionic/portsentry/portsentry.ignore"
1 _3 s( V+ ?- |; @$ E, O+ d9 j    #此文件中保留了以往所有入侵主机的IP历史记录, Q- r! {" a0 d/ ~8 B7 B
    HISTROY_FILE="/usr/lcal/psionic/portsentry/portsentry.history"4 _% f6 `8 \- n+ Z
    #此文件中是已经被阻止连接的主机IP记录7 x' J+ T7 [( v' X3 h, E
    BLOCKED_FILE="/usr/local/psionic/portsentry/portsentry.blocked"/ i' F9 I0 L& C* r  J" X+ p
    (3)设置路由重定向
6 e& t' ], x  i* q9 l. D( [9 ^    通过配置portsentry.conf文件,可以设置一条虚拟路由记录,把数据包重定向到一个未知的主机。使之无法获取信息。相应配置代码如下:9 S0 j. G3 [; e" y- L3 [8 z
    #Generic; r; C! y7 Q9 Y1 }
    #KILL_ROUTE="/sbin/routeadd$TARGET跭。444.555.666"0 N* @2 f. R! x! h9 T' u8 e; c% h
    #GenericLinux1 z( _0 U/ U7 N# d0 G
    KILL_ROUTE="/sbin/routeadd-host$TARGET$gw333.444.555.6666 r7 x4 ]  N9 F9 }' F
    针对不同的平台有不同的路由命令,在配置文件中选择适台自己平台的命令即可。我的服务器是CentOS5.5x86_64,以上语法适合Linux平台的机器;PortSentry非常人性化,下面都有系统对应的配置文件,我们只需要依样操作即可。
/ J, ?9 R$ R" _- X9 C    (4)我们还可以利用Linux中的iptables命令,可以切断攻击主机的连接:% ?" {5 v4 c+ V% S, T" r% V
    KILL_ROUTE="/usr/local/sbin/iptables-IINPUT-s$TARGET$-jDROP"+ S! [1 y9 f- q: w
    也可以直接把攻击者的IP记录到/etc/hosts.deny文件中,利用TCP_Wrappers保护机制来防止攻击:0 E  S1 Y' r  D/ k. L
    KILL_HOSTS_DENY="ALLTARGET$#Portsentryblocked"- R  \( e! M  n+ X- `5 c
    系统默认是利用TCP_Wrappers来切断与主机之间的连接.
# i& n1 T# L" G3 k9 `9 M(5)定制警告信息+ ^) J5 M4 E- z8 y1 |  Z4 h
    我们也可以定制一条警告信息,警告攻击者。不过,手册上建议不要使用该选项,因为这样做可能会暴露主机的IDS系统。
! j* i& b' O) W+ G  [    PORT_BANNER="**UNAUTHORIZEDACCESSPROHIBITED***YOURCONNECTIONATTEMPTHASBEENLOGGED.GOAWAY."
! M/ r( s  u( o- I) U( y1 F  I/ B    修改完毕后,改变文件的权限。以保证其安全性:' ^% {7 [- n6 g$ o) R; r% f
    chmod600/usr/local/psionic/portsentry/portsentry.conf2 E. r" s& l" V5 g2 ?- \# f
    2.配置portsentry.ignore文件+ G! x. I8 k' l! ^) \! x
    /usr/psionic/portsentry/portsentry.ignore文件中设置了希望PortSentry忽略的主机IP,即允许合法扫描的主机地址下面是配置情况:; A4 c& H, h/ C; h
    #Puthostsinhereyouneverwantblocked,ThisincludestheIPaddresses
3 f1 h  ]$ B* \" x( @    #ofalllocalinterfacesontheprotectedhost(i.evirtualhost,mult-home)
9 e. ^( W- o/ k) r- w) g' y. U    #keep127.0.0.1and0.0.0.0tokeeppeoplefromplayinggames.) `% ]2 W' V, H' D
    127.0.0.1/324 i, m' G0 `1 y
    0.0.0.03 L8 x; `( o2 {5 U
    #Excludealllocalinterfaces$ }4 v- L' B) u9 Q1 L1 @" z- o
    192.168.1.1039 p& F, `6 _  \' C( u4 z4 G
    192.168.1.1023 Q$ w" W: q  x
    127.0.0.17 d+ Z5 H5 m) h% v$ ~
    记得带上本机地址,以防万一。6 ^; l% C- O+ e. B0 e6 G
    修改完成后同样需要改变文件默认的权限:
5 h0 ^+ j/ r6 F4 z# I+ F+ v' n4 I    chmod600/usr/local/psionic/portsentry/portsentry.ignore
. K* u) v0 x5 C" D    三、启动检测模式
6 l5 w' T( }8 Q. N+ V% J    最后介绍一下PortSentry的启动检测模式。对应TCP和UDF两种协议方式,PortSentry分别有三种启动模式,即基本、秘密和高级秘密扫描检测模式,合计6个模式。
; V4 k0 q1 K, ~: V9 B    ◆portsentry-tcp,TCP的基本端口绑定模式;/ f9 S' O7 l1 ~% `: H
    ◆portsentry-udp,UDP的基本端口绑定模式;
; s* f' T8 ~7 X' H: W  ]    ◆portsentry-stcp,TCP的秘密扫描检测模式;# Y  ~/ S* x4 x  ?' K- u0 A& Y
    ◆portsentry-sudp,UDP的秘密扫描检测模式;
: A- f! J. g9 u    ◆portsentry-atcp,TCP的高级秘密扫描检测模式;8 @9 w+ U! h9 u, l& {- d
    ◆portsentry-audp,UDP的高级秘密扫描检测模式。2 }2 g+ R, E8 I3 h
    一般情况下,建议使用秘密扫描检测模式或高级秘密扫描检测模式。8 A. ^" a5 m6 t! e7 ]
    使用高级秘密扫描检测模式(AdvancedStealthScanDetectionMode),PortSentry会自动检查服务器上正在运行的端口,然后把这些端口从配置文件中移去,只监控其它的端口。这样会加快对端口扫描的反应速度,并且只占用很少的CPU时间,这种模式非常智能,我比较喜欢用2 f6 f$ C+ H) X: h1 Y! V! @
    启动PortSentry的命令如下:
7 O. Y* c, T. m! H* ?7 J    #/usr/psionic/portsentry/portsentry-atcp
1 m" E3 z- c7 z+ G3 z1 y    可以把启动命令加到“/etc/rc.d/rc.local”脚本文件中,果想让它和其它后台进程一样可以随时启动、停止并查看进程状态,可以去这样当重新启动计算机的时候PortSentry就会自动运行。
- h& ~+ t5 Q  c    四、测试
  n: z" N& ^* T/ q" U    我们在192.168.1.102上启动PortSentry后,先暂时清掉portsentry.ignore里的文件,然后在另一台192.168.1.104的机器上启动扫描命令nmap-sS192.168.1.102。稍等片刻,就会发现/etc/hosts.deny里会出现ALL:192.168.1.104的字样,证明此软件配置都是生效的。: x2 m3 F4 e8 M. x& P3 w' p7 }4 Y6 @
    为了证明其有效性,我拿自己的线上的一台LVS机器布署了portsentry1.2,查看日志发现
, W: Z. E8 F( M. q+ T    [root@localhostportsentry_beta]#tail/var/log/messages  ]3 m6 t1 p5 W- B( x) a" I
    Jun613:11:07localhostportsentry[2555]:attackalert:TCPSYN/Normalscanfromhost:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89toTCPport:807 @; x2 ?7 R0 k9 @7 N
    Jun613:11:07localhostportsentry[2555]:attackalert:Host65.9.251.89hasbeenblockedviawrapperswithstring:"ALL:65.9.251.89"+ R1 V# A) _1 }# S! P% S5 X
    Jun613:11:07localhostportsentry[2555]:attackalert:TCPSYN/Normalscanfromhost:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89toTCPport:80# J" `! P2 b! d4 r. S
    Jun613:11:07localhostportsentry[2555]:attackalert:Host:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89isalreadyblockedIgnoring
0 r. d% E$ _" x    Jun613:11:08localhostportsentry[2555]:attackalert:TCPSYN/Normalscanfromhost:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89toTCPport:80& l" m7 _0 T$ _! D
    Jun613:11:08localhostportsentry[2555]:attackalert:Host:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89isalreadyblockedIgnoring* f4 r( p: L7 v& b* E
    Jun613:19:57localhostportsentry[2555]:attackalert:TCPSYN/Normalscanfromhost:ns38534.ovh.net/91.121.14.153toTCPport:804 X, ~. K; P) C; j! D2 e$ v& G
    Jun613:19:57localhostportsentry[2555]:attackalert:Host91.121.14.153hasbeenblockedviawrapperswithstring:"ALL:91.121.14.153"1 J" p0 r# g+ }5 N
    Jun613:35:44localhostportsentry[2555]:attackalert:TCPSYN/Normalscanfromhost:61.156.31.43/61.156.31.43toTCPport:809 \" `" S2 I8 p0 ?; D. W
    Jun613:35:44localhostportsentry[2555]:attackalert:Host61.156.31.43hasbeenblockedviawrapperswithstring:"ALL:61.156.31.43"
5 I) H; |4 Y( |0 q2 N    检查了下/etc/hosts.deny,发现如下恶意IP:
$ m( V7 q- I- P7 _2 W$ k    ALL:113.57.224.3: Z# ]+ K2 W/ r1 M$ m, o7 o
    ALL:124.238.249.246
& }7 M7 I( T6 a6 l3 t    ALL:65.9.251.89# m* ~8 ?+ E+ n1 J
    ALL:91.121.14.153
1 j1 @: y0 w) q& c$ q    ALL:61.156.31.43
# h  y6 f. b! w$ z- s+ p' D7 N' R6 k* {; C- R$ @+ Z4 K
本文参考: http://linux.chinaitlab.com/server/858720.html

返回列表
Namecheap
Namecheap.com - Cheap domain name registration, renewal and transfers - Free SSL Certificates - Web Hosting
互联网安全