-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendmail
executable file
·144 lines (132 loc) · 3.46 KB
/
sendmail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
require "pp"
require "optparse"
require 'socket'
$servername = Socket.gethostname
$dry_run = false
$port = 25
$print_raw = false
optpars = OptionParser.new { |opts|
YELLOW="\033[33m"
NORMAL="\033[0m"
opts.banner = <<~USAGE
Usage: #{File.basename($0)} [options] <server> [<mail_from> [<rcpt_to> [mailfile]]]
Ex.:
#{YELLOW}1. Check if Mail works (+IP reverse lookup, + IP Blacklist)#{NORMAL}
#{File.basename($0)} <server> someone@#{$servername} someone@server examples/valid.eml
#{YELLOW}2. Check for open relay#{NORMAL}
#{File.basename($0)} <server> someone@#{$servername} name.lastname@your.external.domain.com examples/valid.eml
#{YELLOW}3. Check if spf is configured correctly:#{NORMAL}
#{File.basename($0)} <server> someone@spf-fail.mgm-sp.team someone@server examples/valid.eml
#{YELLOW}4. Check if From-Address can be bullshit#{NORMAL}
#{File.basename($0)} <server> someone@#{$servername} someone@server examples/invalid-from.eml
#{YELLOW}5. Check if DMARC is checked#{NORMAL}
#{File.basename($0)} <server> someone@#{$servername} someone@server examples/dmarc-reject.eml
#{YELLOW}6. Check if DKIM is checked#{NORMAL}
#{File.basename($0)} <server> someone@#{$servername} someone@server examples/dkim-reject.eml
#{YELLOW}7. Check if receipient has a virus scanner in place#{NORMAL}
#{File.basename($0)} <server> someone@#{$servername} someone@server examples/eicar.eml
USAGE
opts.separator "Options:"
opts.on('--helo <servername>', "Use <servername> in HELO instead of #{$servername}") {|string|
$servername = string
}
opts.on('--port <port>', "Use <port> instead of #{$port}") {|integer|
$port = integer.to_i
}
opts.on('--print-raw', 'print raw characters like \\n in output') {|bool|
$print_raw = bool
}
opts.on('--dry-run', "do not execute commands") {|bool|
$dry_run = bool
}
opts.on("-h", "--help", "show this message") {
puts opts
exit
}
}
begin
optpars.parse!
rescue => e
puts optpars
raise e
end
class DummyTCPSocket
def DummyTCPSocket::open(server, port)
puts "openssl s_client -starttls smtp -servername #{server} -connect #{server}:#{port} -crlf -quiet"
DummyTCPSocket.new
end
def gets
"250 DRY-RUN-OK"
end
def puts s
end
def close
end
end
class SMTP
def initialize(server)
@server = server
if $dry_run
@s = DummyTCPSocket.open(@server, $port)
else
@s = TCPSocket.open(@server, $port)
end
puts "$ nc #{@server} #{$port}"
banner = @s.gets
puts banner
if banner.scan(/^(\d\d\d)(.*)$/).flatten[0].to_i >=300
exit
end
if send_wait("HELO #{$servername}")[0] != 250
exit
end
end
def send_wait(cmd)
puts cmd
send("#{cmd}\r\n")
result = @s.gets
puts result
ret = result.scan(/^(\d\d\d)(.*)$/).flatten
ret[0] = ret[0].to_i
ret
end
def send(cmd)
@s.puts(cmd)
end
def close
@s.close
end
def renew
@s.close
initialize(@server)
end
end
if ARGV[0]
server = ARGV[0]
else
puts optpars
exit
end
s = SMTP.new(server)
replace = {
"DATE" => Time.now.strftime("%a, %d %b %Y %T %z")
}
exit unless ARGV[1]
replace["FROM"] = ARGV[1]
exit if (s.send_wait("MAIL FROM: <#{ARGV[1]}>")[0] >= 400)
exit unless ARGV[2]
replace["TO"] = ARGV[2]
exit if (s.send_wait("RCPT TO: <#{ARGV[2]}>")[0] >=400)
exit unless ARGV[3]
exit if (s.send_wait("DATA")[0] >=400)
File.open(ARGV[3]).each{|line|
replace.keys.each{|k|
line.gsub!("INSERT-#{k}-VARIABLE",replace[k])
}
puts($print_raw ? line.inspect[1..-2] : line)
s.send(line)
}
s.send_wait(".")
s.send_wait("QUIT")
s.close