Email 动作
Email 动作
简介
本章节介绍 Email 动作的两个特性:
- 邮件内容可以指定为前序 CLI 动作执行的输出结果,或前序事件日志触发器中对应的日志内容。在此基础上,也可以指定日志中的特定字段,或是过滤符合某些条件的日志。
- 自定义邮件的格式。
自定义邮件内容
网络需求
用户想要在 FortiGate 的管理员登录或等出时,通过邮件通知登录和登出的系统事件日志信息,但只想得到系统事件日志中的源 IP、目的 IP、和登录/登出信息,过滤掉其他的日志内容。
配置步骤
进入 FortiGate 的“Security Fabric → 自动化 → 触发器”页面,点击“新建”按钮。

在弹出的页面的“其他条件”分类中点击“FortiOS 事件日志”。

配置触发器名称,事件选择管理员登录成功、失败、以及登出三种类型,点击确认下发配置。


config system automation-trigger edit "Login_and_Logout" set event-type event-log set logid 32002 32001 32003 next end进入 FortiGate 的“Security Fabric → 自动化 → 操作”页面,点击“新建”按钮。

在弹出的页面的“通知”分类中点击“Email”。

配置动作名称,Email 的目的,以及邮件标题,在“Body”配置中,默认配置为
%%log%%,也就是前序日志触发器的完整日志内容,这里我们可以点击“Body”右侧的“%”按钮,会弹出一些其他的变量示例。
config system automation-action edit "Backup_config_to_FTP" set action-type cli-script set script "exe backup config ftp AutoBackup_%%date%%.conf 192.168.100.110 backup 111111" set accprofile "super_admin" next end- 最小间隔:如果 Trigger 频繁触发了,Action 执行的最小间隔。
- 源:指定邮件通知的发件人,默认为 DoNotReply@notification.fortinet.net。
- 目的:配置邮箱的收件人,可以配置多个收件人。
- Body:邮件内容。
- 更换信息:可以更换为自定义内容和格式。
按照上步的变量示例,来自定义发送邮件的内容,点击“确认”按钮下发配置。
重要
内容包括三个日志字段的信息:登录/登出源 IP:
%%log.srcip%%(表示从日志中的srcip字段取值)、登录/登出目的 IP:%%log.dstip%%(表示从日志中的dstip字段取值)、登录/登出信息:%%log.msg%%"(表示从日志中的msg字段取值)
config system automation-action edit "Login_and_Logout" set action-type email set email-to "bbai@fortinet.com" set email-subject "管理员登录/登出通知" set message "登录/登出源IP:%%log.srcip%% 登录/登出目的IP:%%log.dstip%% 登录/登出信息:%%log.msg%%\"" next end进入 FortiGate 的“Security Fabric → 自动化 → 工作流”页面,点击“新建”按钮。

配置工作流(Stitch 的)名称,在触发中选择步骤 3 创建的触发器,在 Action 中选择步骤 7 创建的动作,点击“确认”下发配置。

config system automation-stitch edit "Login_and_Logout" set trigger "Login_and_Logout" config actions edit 1 set action "Login_and_Logout" set required enable next end next end
配置验证
触发 FortiGate 上的登录或登出日志,如下所示一次管理员登录失败的日志。
date=2024-03-20 time=17:56:17 eventtime=1710928577859214379 tz="+0800" logid="0100032002" type="event" subtype="system" level="alert" vd="root" logdesc="Admin login failed" sn="0" user="administrator" ui="https(192.168.100.178)" method="https" srcip=192.168.100.178 dstip=192.168.100.99 action="login" status="failed" reason="passwd_invalid" msg="Administrator administrator login failed from https(192.168.100.178) because of invalid password"触发自动化程序,相关自动化日志如下。
date=2024-03-20 time=17:56:18 eventtime=1710928577859816539 tz="+0800" logid="0100046600" type="event" subtype="system" level="notice" vd="root" logdesc="Automation stitch triggered" stitch="Login_and_Logout" trigger="Login_and_Logout" stitchaction="Login_and_Logout" from="log" msg="stitch:Login_and_Logout is triggered."收件人邮箱收到了告警邮件,邮件内容为自定义的日志字段内容,发件人为默认的 DoNotReply@notification.fortinet.net。

对应的自动化触发 Debug 信息。
FortiGate # diagnose debug application autod -1 FortiGate # diagnose debug enable ...... __action_email_hdl()-181: email action (Login_and_Logout) is called. from: to:bbai@fortinet.com; subject:管理员登录/登出通知 ......发送邮件的 Debug 信息如下。
FortiGate # diagnose debug application alertmail -1 FortiGate # diagnose debug enable Arrived msg(type 9, 251 bytes):bbai@fortinet.com; 管理员登录/登出通知 登录/登出源IP:192.168.100.178 登录/登出目的IP:192.168.100.99 登录/登出信息:Administrator administrator login failed from https(192.168.100.178) because of invalid password" mail_info: from:notification.fortinet.net user:DoNotReply@notification.fortinet.net mail_info: reverse path:DoNotReply@notification.fortinet.net user name:DoNotReply to[0]:bbai@fortinet.com <==_init_mail_info create session resolve notification.fortinet.net to 1 IP ==> send mail connecting to 208.91.114.151 port 465 send mail 0xa1b6d80 session 0xa1bc7e0 session_io_event: creating ssl structure for session 0xa1bc7e0 create_ssl: 0x7fa5d8a000 sessionn 0xa1bc7e0, SSL connected session: 0xa1bc7e0, rsp_state: greeting, code: 220 session: 0xa1bc7e0, rsp_state: ehlo, code: 250 session: 0xa1bc7e0, rsp_state: mail, code: 250 session: 0xa1bc7e0, rsp_state: rcpt, code: 250 session: 0xa1bc7e0, rsp_state: data, code: 354 === send: 登录/登出源IP:192.168.100.178 登录/登出目的IP:192.168.100.99 登录/登出信息:Administrator administrator login failed from https(192.168.100.178) because of invalid password" session: 0xa1bc7e0, rsp_state: data2, code: 250 session: 0xa1bc7e0, rsp_state: quit, code: 221 session finined _session_on_destroy <== send mail success, m = 0xa1b6d80 s = 0xa1bc7e0
自定义邮件格式
网络需求
在上个章节的需求基础上,修改发送邮件的样式,添加一个告警图片。
配置步骤
进入 FortiGate 的“系统管理 → 替换信息组”页面,点击“新建”按钮。

配置替换信息组名称,“组类型”选择“安全”,点击“确认”按钮下发配置。

右键点击新建的替换信息组,点击“编辑”按钮(或双击编辑)。

搜索关键字“自动邮件”,找到“自动邮件告警”条目,右键或双击编辑该条目。
重要
替换信息组中也包含其他功能模块页面的替换信息,当用户需要替换多个功能模块的页面信息时,可以使用此功能。

在 HTML 编辑页面,将想要添加的告警图片 URL 添加到
message-container部分,如下图所示(<img src="https://www.fortinet.com/content/dam/fortinet/images/icons/benefits/icon-wireless-fortinet-ips.svg" alt="Fortinet Logo"/>),左侧的预览可以正常显示该图片,点击“备份配置”按钮保存配置。
config system replacemsg-group edit "Replace_Group" set comment '' set group-type utm config automation edit "automation-email" set buffer "<!DOCTYPE html> <html lang=\"en\"> <head> <meta charset=\"UTF-8\"> <meta http-equiv=\"X-UA-Compatible\" content=\"IE=8; IE=EDGE\"> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <link href=\"https://fonts.googleapis.com/css?family=Roboto&display=swap\" rel=\"stylesheet\"> <style> body { height: 100%; font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; display: flex; align-items: center; justify-content: center; } .message-container{ margin: 0 auto; max-width: 580px; } .email-body { line-height: 1.5em; } </style> </head> <body> <div class=\"message-container\"> <img src=\"https://www.fortinet.com/content/dam/fortinet/images/icons/benefits/icon-wireless-fortinet-ips.svg\" alt=\"Fortinet Logo\"/> <h1> Security Fabric Automation </h1> <h3> %%AUTOMATION_FGT_SERIAL%%: %%AUTOMATION_STITCH_NAME%% </h3> <div class=\"email-body\"> %%AUTOMATION_EMAIL_BODY%% </div> </div> </body> </html> " set header http set format html next end next end进入 FortiGate 的“Security Fabric → 自动化 → 操作”页面,编辑上个章节创建的动作“Login_and_Logout”。开启“更换信息”选项,然后开启“自定义消息”选项,并选择上步创建的替换信息组。注意由于调用了替换信息组(HTML 格式),所以需要在“Body”中每行加入 HTML 换行符
<br>。点击“确认”按钮下发配置。重要
如果仅需要替换此邮件动作的格式,则不需要步骤 1~5 创建替换信息组,只在此页面开启“更换信息”选项并点击编辑按钮编辑 HTML 格式即可。

config system automation-action edit "Login_and_Logout" set action-type email set email-to "bbai@fortinet.com" set email-subject "管理员登录/登出通知" set message "登录/登出源IP:%%log.srcip%%<br> 登录/登出目的IP:%%log.dstip%%<br> 登录/登出信息:%%log.msg%%\"" set replacement-message enable set replacemsg-group "Replace_Group" next end
配置验证
触发 FortiGate 上的登录或登出日志,查看收件人收到的邮件格式按照替换信息组中的配置的样式显示。
