CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/proto/mms/model/message.rb
Views: 11766
1
# -*- coding: binary -*-
2
require 'rex/mime/message'
3
4
module Rex
5
module Proto
6
module Mms
7
module Model
8
class Message
9
10
# @!attribute message
11
# @return [String] The text message
12
attr_accessor :message
13
14
# @!attribute content_type
15
# @return [Fixnum] The content type of the attachment
16
attr_accessor :content_type
17
18
# @!attribute attachment
19
# @return [String] The loaded attachment converted to Base64
20
attr_accessor :attachment
21
22
# @!attribute from
23
# @return [String] The from field in the email
24
attr_accessor :from
25
26
# @!attribute to
27
# @return [String] The to field in the email
28
attr_accessor :to
29
30
# @!attribute subject
31
# @return [String] The subject of the email
32
attr_accessor :subject
33
34
# @!attribute attachment_name
35
# @return [String] The attachment base name extracted from :attachment
36
attr_accessor :attachment_name
37
38
39
# Initializes the SMTP object.
40
#
41
# @param [Hash] opts
42
# @option opts [String] :from
43
# @option opts [String] :to
44
# @option opts [String] :message
45
# @option opts [String] :content_type
46
# @option opts [String] :attachment_path
47
#
48
# @return [Rex::Proto::Mms::Model::Message]
49
def initialize(opts={})
50
self.from = opts[:from]
51
self.to = opts[:to]
52
self.message = opts[:message]
53
self.subject = opts[:subject]
54
self.content_type = opts[:content_type]
55
if opts[:attachment_path]
56
self.attachment = load_file_to_base64(opts[:attachment_path])
57
self.attachment_name = File.basename(opts[:attachment_path])
58
end
59
end
60
61
62
# Returns the raw MMS message
63
#
64
# @return [String]
65
def to_s
66
generate_mms_message
67
end
68
69
70
private
71
72
73
# Returns the loaded file in Base64 format
74
#
75
# @return [String] Base64 data
76
def load_file_to_base64(path)
77
buf = File.read(path)
78
(Rex::Text.encode_base64(buf).scan(/.{,76}/).flatten * "\n").strip
79
end
80
81
82
# Returns the raw MMS message
83
#
84
# @return [String]
85
def generate_mms_message
86
text = Rex::MIME::Message.new
87
text.add_part(self.message, 'text/plain; charset=UTF-8', nil)
88
body = Rex::MIME::Message.new
89
body.add_part(text.to_s, "multipart/alternative; boundary=#{text.bound}", nil)
90
if self.attachment
91
body.add_part(self.attachment, "#{content_type}; name=\"#{attachment_name}\"", 'base64', "attachment; filename=\"#{attachment_name}\"")
92
end
93
94
mms = "MIME-Version: 1.0\n"
95
mms << "From: #{self.from}\n"
96
mms << "To: #{self.to}\n"
97
mms << "Subject: #{self.subject}\n"
98
mms << "Content-Type: multipart/mixed; boundary=#{body.bound}\n"
99
mms << "\n"
100
mms << body.to_s.gsub(/\-\-\r\n\r\n\-\-_/, "--\n--_")
101
102
mms
103
end
104
105
end
106
end
107
end
108
end
109
end
110
111