Username: 
Password: 
Restrict session to IP 
Questions  |  score: 5  |  5.84 6.79 7.74 |  Solved By 82 People  |  98861 views  |  since Jul 25, 2011 - 19:59:55

Smile (Exploit, PHP)

Smile!
We are working hard on a better WeChall, and ask for your help.
In particular we want new smileys being added to the bb_decoder.
To add cool smileys, we have coded up a small form to submit them.

Would be cool if you could add a few smileys and replacing rules for us!

Oh ... again you are given the sourcecode.
There are two files: smile.php (highlighted)
And: Livin_Smile.php (highlighted)
smile.php
GeSHi`ed PHP code
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
109
110111
112
113
114
115116
117
118
119
120121
122
123
124
125126
127
128
129
130131
132
133
134
135136
137
138
139
140141
142
143
144
145146
147
148
149
150151
152
153
154
155156
157
158
159
160161
162
163
164
165166
167
168
169
170171
172
173
174
175176
177
178
179
180181
182
183
184
185186
187
188
189
190191
192
193
194
195196
197
198
199
200201
202
203
204
205206
207
208
209
210211
212
213
214
215216
217
218
219
220221
222
223
224
225226
<?php
chdir('../../../');
define('GWF_PAGE_TITLE', 'Smile');
require_once('challenge/html_head.php');
if (false === ($chall = WC_Challenge::getByTitle(GWF_PAGE_TITLE))){
        $chall = WC_Challenge::dummyChallenge(GWF_PAGE_TITLE, 4, 'challenge/livinskull/smile/index.php', false);
}
$chall->showHeader();
 # Table and Helper :)
require_once 'challenge/livinskull/smile/LIVIN_Smile.php';
 
# Your solution is here :)
define('LIVINSKULL_SMILEY_SOLUTION', LIVIN_Smile::getSolution()); 
 
/**
 * The smiley form :)
 * @author gizmore */
class LivinForm
{
        private $chall;
                public function __construct(WC_Challenge $chall)
        {
                $this->chall = $chall;
        }
                # Sanitize pattern a bit
        public function validate_pattern($m, $arg)
        {
                if ($arg === '')
                {                        return $this->chall->lang('err_pattern');
                }
                if ($arg[0] !== '/')
                {
                        $_POST['pattern'] = "/{$arg}/";                }
                return false;
        }
        
        # Always valid        public function validate_filename($m, $arg)
        {
                return false;
        }
                public function getForm(WC_Challenge $chall)
        {
                $data = array(
                        'pattern' => array(GWF_Form::STRING, '', $chall->lang('th_pattern'), $chall->lang('tt_pattern')),
                        'filename' => array(GWF_Form::STRING, $this->getRandomFilename(), $chall->lang('th_filename'), $chall->lang('tt_filename')),                        'image' => array(GWF_Form::FILE_OPT, '', $chall->lang('th_upload')),
                        'upload' => array(GWF_Form::SUBMIT, $chall->lang('btn_upload')),
                        'add' => array(GWF_Form::SUBMIT, $chall->lang('btn_add')),
                );
                return new GWF_Form($this, $data);        }
        
        public function getRandomFilename()
        {
                $directory = 'challenge/livinskull/smile/smiles';                if (false === ($dir = scandir($directory)))
                {
                        echo GWF_HTML::err('ERR_READ_FILE', array($directory));
                        return '';
                }                foreach ($dir as $i => $file)
                {
                        if ( ($file === '.') || ($file === '..') )
                        {
                                unset($dir[$i]);                        }
                }
                if (count($dir) === 0)
                {
                        return '';                }
                $file = $dir[array_rand($dir)];
                return "<img src=\"/$directory/$file\" />";
        }
                public function onUpload(WC_Challenge $chall)
        {
                $module = Module_WeChall::instance();
                $form = $this->getForm($chall);
                if (false === ($file = $form->getVar('image')))                {
                        return GWF_HTML::error('Smile', array($chall->lang('err_no_image')));
                }
                
                if (!GWF_Upload::isImageFile($file))                {
                        return GWF_HTML::error('Smile', array($chall->lang('err_no_image')));
                }
                
                if (false === GWF_Upload::resizeImage($file, 64, 64, 16, 16))                {
                        return GWF_HTML::error('Smile', array($chall->lang('err_no_image')));
                }
                
                $whitelist = array(                        '.jpg',
                        '.jpeg',
                        '.gif',
                        '.png',
                );                $filename = $file['name'];
                $allowed = false;
                foreach ($whitelist as $allow)
                {
                        if (Common::endsWith($filename, $allow))                        {
                                $allowed = true;
                                break;
                        }
                }                
                if (strpos($filename, '.php') !== false)
                {
                        $allowed = false;
                }                
                if (!preg_match('/^[\x00-\x7f]+$/D', $filename))
                {
                        return GWF_HTML::error('Smile Path', array($chall->lang('err_ascii')));
                }                
                
                if (!$allowed)
                {
                        return GWF_HTML::error('Smile', array($chall->lang('err_no_image')));                }
                
                $fullpath = "challenge/livinskull/smile/smiles/{$filename}";
                $efp = htmlspecialchars($fullpath);
                if (false === ($file = GWF_Upload::moveTo($file, $fullpath)))                {
                        return GWF_HTML::err('ERR_WRITE_FILE', array($efp));
                }
                
                $efp = htmlspecialchars($fullpath);                $rule = htmlspecialchars("<img src=\"/{$efp}\" />");
                
                return GWF_HTML::message('Smile', $chall->lang('msg_uploaded', array($rule)));
        }
                public function onAdd(WC_Challenge $chall)
        {
                $module = Module_WeChall::instance();
                $form = $this->getForm($chall);
                if (false !== ($error = $form->validate($module)))                {
                        return $error;
                }
                
                $pattern = $form->getVar('pattern');                $path = $form->getVar('filename');
                
//              if (!preg_match('/^[\x00-\x7f]+$/D', $pattern))
//              {
//                      return GWF_HTML::error('Smile Pattern', array($chall->lang('err_ascii')));//              }
 
                if (!preg_match('/^[\x00-\x7f]+$/D', $path))
                {
                        return GWF_HTML::error('Smile Path', array($chall->lang('err_ascii')));                }
                
                # Show a sample output for the new smiley :)
                if (!LIVIN_Smile::testSmiley($chall, $pattern, $path))
                {                        return GWF_HTML::error('Smile', array($chall->lang('err_test')));
                }
                
                # If it looks valid we even add it globally :)
                if (!LIVIN_Smile::looksHarmless($path))                {
                        return GWF_HTML::error('Smile', array($chall->lang('err_xss')));
                }
                if (!LIVIN_Smile::imageExists($path))
                {                        return GWF_HTML::error('Smile', array($chall->lang('err_path')));
                }
 
                # Like this :)
                LIVIN_Smile::onAddSmiley($pattern, $path);                return GWF_HTML::message('Smile', $chall->lang('msg_rule_added'));
        }
}
 
##################### ACTION! :) ###
##################
# Weaken security a bit.
GWF_Debug::setDieOnError(false);
 $form = new LivinForm($chall);
 
if (isset($_POST['add']))
{
        # This will stop you from exploiting too much! :p        require_once 'challenge/livinskull/smile/secure.php';
        echo $form->onAdd($chall);
}
 
elseif (isset($_POST['upload'])){
        # This will stop you from exploiting too much! :o
        require_once 'challenge/livinskull/smile/secure.php';
        echo $form->onUpload($chall);
} 
# Show the form :)
echo $form->getForm($chall)->templateY($chall->lang('ft_add'));
 
# Show all smileys =)echo LIVIN_Smile::showAllSmiles($chall);
 
echo $chall->copyrightFooter();
 
require_once('challenge/html_foot.php'); 
Your solution for Smile
Answer
© 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 and 2024 by livinskull and Gizmore