web安全自動化運維
你有可能也會遇到這種情況,公司沒有任何安全設備,也沒有資金采購,應付層層攻擊,那么作為公司唯有一個安全工程師,如何來自動化保障公司的web安全呢?
下面,結合我的一些經驗,說說一些實際操作。由于全部是手工,維護web安全來做到實時過濾安全攻擊,那必須在web服務器前方做一個代理,或者在代碼層有一個代理層,實時的檢測過來的請求,再傳給應用,這涉及到編寫web應用防火墻呢,相對而言,對一個獨立安全工程師比較復雜,簡單一點的呢?
其實,退而求其次,我當時做法分兩步走,第一步我是做自動化巡檢。第一次服務器的安全巡檢,網絡上有一些checklist,這就不列舉了,不是本文重點,對每個核心服務器一開始做一次全面詳細的checklist檢查,修復后,基本能做到基準安全了。
接下來,就是要每日巡檢了。那么巡檢的主題是什么呢?關鍵文件的變更。我當時使用的ruby,核心代碼改編于網上:
#ruby比較兩個文件def cmpfile(source_file,tmp_file,security_type) num=0
f1=File.open(source_file) f2=File.open(tmp_file)if !File.zero?("source_file") and !File.zero?("tmp_file") then num1,f1array=getFileLine(f1) num2,f2array=getFileLine(f2)
#ensure the numberof loop if num1>num2 num=num1 else num=num2 end
for i in(0..num-1) mesg1="Exception:Maybe file" mesg2="was not enough lines. Cant find the data when check line"
if f1array[i] != nil and f2array[i] != nil if f1array[i] != f2array[i] $rrp.write("--#{security_type}--num:"+i.to_s+""+"source_file: "+f1array[i].to_s+""+"new_file: "+f2array[i].to_s) end else if f1array[i] = nil $rrp.write("--#{security_type}--num:"+i.to_s+" "+mesg1+'1'+ mesg2+(i+1).to_s) break else if f2array[i] = nil $rrp.write("--#{security_type}--num:"+i.to_s+" "+mesg1+'2'+ mesg2+(i+1).to_s) break else break end end end endend f1.close f2.closeend
def getFileLine(f) farray=[] num=0 f.each do |fi| num+=1 farray+=[fi.strip]end return num,farrayend
思路即是,把所有需要監控的東西,保留一個最初的原始文件,存于一個專門的文件夾內,每次巡檢,通過如下:
#執行命令 提取源信息def cmd_source(cmd,source_path_file)cont=`#{cmd}`if !File.exist?(source_path_file)File.new(source_path_file,"w").write(cont)endend#執行命令 提取臨時信息def cmd_tmp(cmd,tmp_path_file)cont=`#{cmd}`File.new(tmp_path_file,"w").write(cont)end
這樣,每次手工檢測執行命令的結果都存在專門文件里,跟原始文件進行對比(內容對比函數見上)。于是,再稍微優化整理一下,一個自動化的檢測腳步就執行了。
具體可以以一周為期限或碰到特殊情況變更原始文件,將新的標準文件換為原始文件進行對比。做定時任務,每5分鐘左右執行一次檢測,那么一旦出現文件變更,掛馬等則立刻能在生成的變更報告文件里檢測出來。
前期個人這樣搞了1個月,工作輕松很多,后來公司有一個哥們用ruby比較熟,于是公司需求做一個ruby的風險監控系統,那么,下面就是本文重點了,這個風險監控系統關鍵點就是把實施檢測的信息,發送給后端,顯示。我們用的消息平臺是amqp。
先說一下整體思路,然后下面見核心代碼解釋。風險監控系統,可以將手工檢測生成的報文信息與日志里重要的關鍵信息進行提取整理,發送給后端,后端根據得到的數據進行圖形或表格等不同展現。
這分為三部分。信息的來源:原始日志與前期自動化檢測生成的信息;信息的分析:對分析進行整理,取出含有關鍵字的特殊信息進行分類分級,放在不同管道里,發給后臺;信息的展現:后臺先將發來的信息存于數據庫,再根據需要取出數據庫進行圖形或表格形式展現。
其實這個進行詳細改編和優化,就是一個完整的soc系統了,目前國內的soc系統也不外如此,卻要買幾十萬甚至上百萬,甚鄙視之。其實soc核心的關鍵是規則關聯和設備聯動,就是在咱們信息分析這一部分,定義大量規則進行各種日志關聯分析,這個我在自己的程序里沒做,當時就只搞apache、resin日志分析,都是些簡單的,整個系統花了一兩個星期跟另一個工程師配合上線,運行正常,后種種情況,擱置,但是給一些孤獨工程師提供一下借鑒思路,還是能省一些事情,希望能給一些人有所幫助。
下面看代碼,不多說:
#! /usr/local/ruby/bin/ruby=beginauthor:kn1ghtcdescribe:that's true=endrequire 'rubygems'require 'amqp'#ruby是一時心血來潮搞了一下,其實還是不大懂,剛看完一本書就寫了這個,所以有很多山寨用法,大牛勿笑,服務器上存的日志按時間每天自動追加到下面幾個文件,#咱們先定義一下幾個變量,取源文件路徑$con_source_path_ssl="../../access_ssl_#{Time.now.strftime("%Y%m%d")}.log"$con_source_path_access="/../access_#{Time.now.strftime("%Y%m%d")}.log"$con_source_path_error="/../error_#{Time.now.strftime("%Y%m%d")}.log"#定義幾個變量,存一些規則過濾后的文件內容,這里圖簡便,人為分配了四個等級和方向,讀者可自行優化規則和分類$tmp_scan=[]$tmp_attack=[]$tmp_app=[]$tmp_abnormal=[]#服務器上日志格式有自己生成的規則,沒關系,我們按我們的需要標準化成我們的格式,便于后面分析規則應用與存儲展現#log_formatdef message_format(str_line,str_type,str_risk,str_format) @str_time="" @str_ip="" @str_data="" @str_result="" @str_format=str_format @str_line=str_line
if @str_format=="error" @str_time=/[a-zA-Z]{3}.[a-zA-Z]{3}.\d{2}.\d{2}:\d{2}:\d{2}.\d{4}/.match(@str_line) @str_ip=/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.match(@str_line) @str_data=@str_line.gsub(/^\[[a-zA-Z]{3}.[a-zA-Z]{3}.\d{2}.\d{2}:\d{2}:\d{2}.\d{4}\].\[[a-zA-Z]{3,5}\].\[[a-zA-Z]{6}.\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]/,"").strip else @str_time=/\d{2}\/[a-zA-Z]{3}\/\d{4}:\d{2}:\d{2}:\d{2}/.match(@str_line) @str_ip=/^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}/.match(@str_line) @str_data=@str_line.gsub(/^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}.....\[\d{2}\/[a-zA-Z]{3}\/\d{4}:\d{2}:\d{2}:\d{2}.\+\d{4}\]/,"").strip end @str_result=@str_ip.to_s+"=>"+@str_data.to_s+"=>"+@str_time.to_s+"=>"+str_type+"=>"+str_risk+""end#取出日志里的每一行,放進下面的函數,設定規則,匹配規則的放進相應數組里#line_rule_matchdef line_rule(rule_line,rule_log_type) @rule_str_line=rule_line @rule_log_type=rule_log_type @new_line="" @rule_data=[]
if @rule_str_line=~/error/ $tmp_abnormal< else @new_line=message_format(@rule_str_line,"unknown","unknown",@rule_log_type).split(/=>/)[1] @rule_data=@new_line.split(/"/) if @rule_data.size>6 $tmp_scan< else if @rule_data.size>1 if @rule_data[3]=~/script>| $tmp_attack< #做了一些狀態號檢測 elsif @rule_data[1]=~/'|alert|script>|cat|union|order by|;|\.\.\/|and|--/ or @rule_data[2].lstrip.split(/\s/)[0]=~/400|403|404|500/ $tmp_scan< end
end
end end
end#log_idsdef invade_log(source_path_file,log_type) @path_source=source_path_file @log_type=log_type i=0 @line_num_tmp=-1#山寨想法,當時各種服務器性能限制和原因,所有為了實時取日志內容(web日志是實施增加的),服務器上設置定時任務,每隔1分鐘運行一次腳本#每1分鐘運行咱們的腳本,腳本便從建立的特殊文件夾里找特殊文件,文件里生成這次檢測原文件的行號,對比行號,從新行號開始往下掃描,原后存行號 if @log_type=="ssl" File.open("/tmp/num_ssl","r") do |file| @line_num_tmp=file.gets.to_i end elsif @log_type=="access" File.open("/tmp/num_access","r") do |file1| @line_num_tmp=file1.gets.to_i end else File.open("/tmp/num_error","r") do |file2| @line_num_tmp=file2.gets.to_i end endFile.open(@path_source) do |files| files.each_line do |line| begin line.force_encoding("gbk") if i>@line_num_tmp line_rule(line,@log_type) else i=i+1 next end i=i+1 rescue i=i+1 $tmp_app< end endend @line_num_tmp=i @file_log=File.open("/tmp/num_#{@log_type}","w") @file_log.write(@line_num_tmp) @file_log.closeend#行號文件,解決實施檢測的問題#time_question#implement_methoddef implement_meth(source_path_file,log_type) @im_path_source=source_path_file @im_log_type=log_type @filename_old=""
if File.exist?(@im_path_source) File.open("/tmp/time_#{@im_log_type}") do |file| @filename_old=file.gets.to_s.strip end if @im_path_source!=@filename_old @im_file_log=File.open("/tmp/num_#{@im_log_type}","w") @im_file_log.write("-1") @im_file_log.close @im_file_log=File.open("/tmp/time_#{@im_log_type}","w") @im_file_log.write(@im_path_source) @im_file_log.close end invade_log(@im_path_source,@im_log_type) endend#mq消息的應用,定義信道相關信息,這里基本格式都是這樣,很好修改為自己的#send_message_method_scandef send_to_exchange_scan(message) exchange=MQ.direct('guofubao') exchange.publish message,:key=>'scan'end#send_message_method_atackdef send_to_exchange_attack(message) exchange=MQ.direct('guofubao') exchange.publish message,:key=>'attack'end#send_message_method_appdef send_to_exchange_app(message) exchange=MQ.direct('guofubao') exchange.publish message,:key=>'app'end#send_message_method_abnormaldef send_to_exchange_abnormal(message) exchange=MQ.direct('guofubao') exchange.publish message,:key=>'abnormal'end
#test_data#tmp_test="127.0.0.1=>kn1ghtc_test=>2012:12:31=>unknown=>unknown"+""#implementimplement_meth($con_source_path_access,"access")implement_meth($con_source_path_error,"error")implement_meth($con_source_path_ssl,"ssl")#conn#應用消息發送和方法體AMQP.start :host => '127.0.0.1', :port => 5672 do event_loop=Thread.new do EM.run do EM.add_timer(1) do EM.stop end end end send_to_exchange_abnormal $tmp_abnormal.join().to_s send_to_exchange_app $tmp_app.join().to_s send_to_exchange_attack $tmp_attack.join().to_s send_to_exchange_scan $tmp_scan.join().to_s
event_loop.joinend
結束:個人比較懶,寫的比較倉促,主體思路都在上面,后面系統因為其它原因下線,幾個月前寫的,現在也只記得思路,覺得有用的看官自己縷一縷吧,大牛就飄過。