metasploit-framework/lib/rex/mime/message.rb

145 lines
3.4 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
module Rex
module MIME
class Message
2013-08-30 21:28:33 +00:00
require 'rex/mime/header'
require 'rex/mime/part'
require 'rex/mime/encoding'
2013-08-30 21:28:33 +00:00
require 'rex/text'
include Rex::MIME::Encoding
2013-08-30 21:28:33 +00:00
attr_accessor :header, :parts, :bound, :content
2013-08-30 21:28:33 +00:00
def initialize(data=nil)
self.header = Rex::MIME::Header.new
self.parts = []
self.bound = "_Part_#{rand(1024)}_#{rand(0xffffffff)}_#{rand(0xffffffff)}"
self.content = ''
if data
head,body = data.split(/\r?\n\r?\n/, 2)
self.header.parse(head)
ctype = self.header.find('Content-Type')
2014-11-07 04:48:34 +00:00
if ctype && ctype[1] && ctype[1] =~ /multipart\/mixed;\s*boundary="?([A-Za-z0-9'\(\)\+\_,\-\.\/:=\?^\s]+)"?/
2013-08-30 21:28:33 +00:00
self.bound = $1
chunks = body.to_s.split(/--#{self.bound}(--)?\r?\n/)
self.content = chunks.shift.to_s.gsub(/\s+$/, '')
2014-11-07 04:48:34 +00:00
self.content << "\r\n" unless self.content.empty?
2013-08-30 21:28:33 +00:00
chunks.each do |chunk|
break if chunk == "--"
head,body = chunk.split(/\r?\n\r?\n/, 2)
part = Rex::MIME::Part.new
part.header.parse(head)
part.content = body.gsub(/\s+$/, '')
self.parts << part
end
else
self.content = body.to_s.gsub(/\s+$/, '') + "\r\n"
end
end
end
def to
(self.header.find('To') || [nil, nil])[1]
end
def to=(val)
self.header.set("To", val)
end
def from=(val)
self.header.set("From", val)
end
def from
(self.header.find('From') || [nil, nil])[1]
end
def subject=(val)
self.header.set("Subject", val)
end
def subject
(self.header.find('Subject') || [nil, nil])[1]
end
def mime_defaults
self.header.set("MIME-Version", "1.0")
self.header.set("Content-Type", "multipart/mixed; boundary=\"#{self.bound}\"")
self.header.set("Subject", '') # placeholder
self.header.set("Date", Time.now.strftime("%a,%e %b %Y %H:%M:%S %z"))
self.header.set("Message-ID",
"<"+
Rex::Text.rand_text_alphanumeric(rand(20)+40)+
"@"+
Rex::Text.rand_text_alpha(rand(20)+3)+
">"
)
self.header.set("From", '') # placeholder
self.header.set("To", '') # placeholder
end
def add_part(data='', content_type='text/plain', transfer_encoding="8bit", content_disposition=nil)
part = Rex::MIME::Part.new
2014-11-07 04:48:34 +00:00
if content_disposition
2013-08-30 21:28:33 +00:00
part.header.set("Content-Disposition", content_disposition)
end
2014-11-07 04:48:34 +00:00
part.header.set("Content-Type", content_type) if content_type
2013-08-30 21:28:33 +00:00
2014-11-07 04:48:34 +00:00
if transfer_encoding
2013-08-30 21:28:33 +00:00
part.header.set("Content-Transfer-Encoding", transfer_encoding)
end
part.content = data
self.parts << part
part
end
def add_part_attachment(data, name)
self.add_part(
Rex::Text.encode_base64(data, "\r\n"),
"application/octet-stream; name=\"#{name}\"",
"base64",
"attachment; filename=\"#{name}\""
)
end
def add_part_inline_attachment(data, name)
self.add_part(
Rex::Text.encode_base64(data, "\r\n"),
"application/octet-stream; name=\"#{name}\"",
"base64",
"inline; filename=\"#{name}\""
)
end
2014-02-11 04:23:23 +00:00
def to_s
header_string = self.header.to_s
2014-11-07 04:48:34 +00:00
msg = header_string.empty? ? '' : force_crlf(self.header.to_s + "\r\n")
msg << force_crlf(self.content + "\r\n") unless self.content.blank?
2013-08-30 21:28:33 +00:00
self.parts.each do |part|
2014-02-11 04:23:23 +00:00
msg << force_crlf("--" + self.bound + "\r\n")
msg << part.to_s
2014-02-01 00:37:01 +00:00
end
2014-11-07 04:48:34 +00:00
msg << force_crlf("--" + self.bound + "--\r\n") if self.parts.length > 0
2014-02-01 00:37:01 +00:00
2014-02-03 21:59:17 +00:00
msg
2014-02-01 00:37:01 +00:00
end
end
end
end