-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlblfixer_cve_2023_30839.php
266 lines (231 loc) · 6.92 KB
/
lblfixer_cve_2023_30839.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* 2023 LabelGrup Networks SL
*
* NOTICE OF LICENSE
*
* READ ATTACHED LICENSE.TXT
*
* @author Manel Alonso <[email protected]>, <[email protected]>
* @copyright 2023 LabelGrup Networks SL
* @license LICENSE.TXT
*/
if (!defined('_PS_VERSION_')) {
exit;
}
class Lblfixer_cve_2023_30839 extends Module
{
private const LBL_PATCHES_DIR = '/patches/';
private const LBL_BACKUP_DIR = '/backup/';
private const LBL_PATCH_EXTENSION = '.patch';
private const LBL_DIR_EXTENSION = '.dir';
public function __construct()
{
$this->name = 'lblfixer_cve_2023_30839';
$this->tab = 'front_office_features';
$this->version = '1.1.1';
$this->author = 'LabelGrup Networks SL, Manel Alonso';
$this->need_instance = 0;
$this->displayName = $this->l('LabelGrup.com FIX CVE-2023 Pack (for PrestaShop <1.7.8.9)');
$this->description = $this->l('Fixes CVE-2023-30545, CVE-2023-30838, CVE-2023-30839 vulnerabilities.');
$this->ps_versions_compliancy = array('min' => '1.7.0', 'max' => '1.7.8.8');
$this->confirmUninstall = $this->l('Your shop will be vulnerable to 3 vulnerabilities.') .
$this->l('Are you sure you want to uninstall this addon?');
parent::__construct();
}
public function install()
{
$this->patchCVE();
return parent::install();
}
public function uninstall()
{
$this->unpatchCVE();
return parent::uninstall();
}
/**
* Get the path to the file to patch
* @return string
*/
private function getFilePath()
{
return _PS_ROOT_DIR_;
}
/**
* Apply patches
* @return bool
*/
private function patchCVE()
{
if ($this->detectAlreadyPatched()) {
return true;
}
return $this->patchFiles();
}
/**
* Remove patches
* @return bool
*/
private function unpatchCVE()
{
return $this->unpatchFiles();
}
/**
* Detect if the patch is already applied
* @return bool
*/
private function detectAlreadyPatched()
{
// For each file to patch, check if the patch is already applied
$files_to_patch = $this->getFilesToPatch();
foreach ($files_to_patch as $file_to_patch) {
$base_path = $this->getFilePath();
$patch_file = dirname(__FILE__) . self::LBL_PATCHES_DIR . $file_to_patch . self::LBL_PATCH_EXTENSION;
$dir_patch_file = $this->getPathForPatch($patch_file);
if (strlen($dir_patch_file) > 0) {
$base_path = $base_path . $dir_patch_file;
}
$path = $base_path . $file_to_patch;
if ($this->detectAlreadyPatchedFile($path, 'Patch for CVE-')) {
return true;
}
}
return false;
}
/**
* Detect if the patch is already applied for a file
* @return bool
*/
private function detectAlreadyPatchedFile($file, $pattern)
{
if (file_exists($file)) {
$content = Tools::file_get_contents($file);
if (strpos($content, $pattern) !== false) {
return true;
}
}
return false;
}
/**
* Get the list of files to patch
* @return array
*/
private function getPatchFiles()
{
return glob(dirname(__FILE__) . self::LBL_PATCHES_DIR . '*' . self::LBL_PATCH_EXTENSION);
}
/**
* Get the list of files to patch
* @return array
*/
private function getFilesToPatch()
{
$file_pathes = $this->getPatchFiles();
foreach ($file_pathes as $file_path) {
$file_name = basename($file_path);
$file_name = str_replace(self::LBL_PATCH_EXTENSION, '', $file_name);
$files[] = $file_name;
}
return $files;
}
/**
* Parse a patch file
* @param string $file_path Path to the patch file
* @return array
*/
private function parsePatchFile($file_path)
{
$patches = explode('¤', Tools::file_get_contents($file_path));
return [$patches];
}
/**
* Patches all files
* @return bool
*/
private function patchFiles()
{
$files_to_patch = $this->getFilesToPatch();
foreach ($files_to_patch as $file_to_patch) {
$base_path = $this->getFilePath();
$patch_file = dirname(__FILE__) . self::LBL_PATCHES_DIR . $file_to_patch . self::LBL_PATCH_EXTENSION;
$dir_patch_file = $this->getPathForPatch($patch_file);
if (strlen($dir_patch_file) > 0) {
$base_path = $base_path . $dir_patch_file;
}
$path = $base_path . $file_to_patch;
if (!$this->patchFile($path, $patch_file)) {
return false;
}
}
return true;
}
/**
* Patches a file
* @param string $path Path to the file to patch
* @param string $patch_file Path to the patch file
* @return bool
*/
private function patchFile($path, $patch_file)
{
if (!$this->backupFile($path)) {
return false;
}
$content = Tools::file_get_contents($path);
$patches_for_file = $this->parsePatchFile($patch_file);
foreach ($patches_for_file as $patch) {
$original = $patch[0];
$replacement = $patch[1];
$content = str_replace($original, $replacement, $content);
}
if (!@file_put_contents($path, $content)) {
return false;
}
return true;
}
/**
* Backup the original file
* @param string $path Path to the file to patch
* @return bool
*/
private function backupFile($path)
{
$filename = basename($path);
if (!@copy($path, dirname(__FILE__) . self::LBL_BACKUP_DIR . $filename)) {
return false;
}
return true;
}
/**
* Restore the original files
* @return bool
*/
private function unpatchFiles()
{
$files_to_patch = $this->getFilesToPatch();
$base_path = $this->getFilePath();
foreach ($files_to_patch as $file_to_patch) {
$path = $base_path . $file_to_patch;
$backup_file = __DIR__ . self::LBL_BACKUP_DIR . $file_to_patch;
if (@copy($backup_file, $path)) {
@unlink($backup_file);
}
}
return true;
}
/**
* Gets the path for a patch
* @param string $patch_file Path to the patch file
* @return string
*/
private function getPathForPatch($patch_file)
{
$first_line = '';
$dir_file = str_replace(self::LBL_PATCH_EXTENSION, self::LBL_DIR_EXTENSION, $patch_file);
if (file_exists($dir_file)) {
$fdir = fopen($dir_file, 'r');
$first_line = fgets($fdir);
fclose($fdir);
}
return $first_line;
}
}