PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Reverse-Shells/Php Shells/weevely3-master/modules/file/_tar/EasyTar.class.php

https://gitlab.com/surajraghuvanshi/Privlage-Esclator
PHP | 209 lines | 167 code | 10 blank | 32 comment | 34 complexity | c4a4932a8e0c8a5dfec5cab35eff5736 MD5 | raw file
  1. /**-------------------------------------------------
  2. | EasyTar.class V0.8 - by Alban LOPEZ
  3. | Copyright (c) 2007 Alban LOPEZ
  4. | Email bugs/suggestions to alban.lopez+easytar@gmail.com
  5. +--------------------------------------------------
  6. | This file is part of EasyArchive.class V0.9.
  7. | EasyArchive is free software: you can redistribute it and/or modify
  8. | it under the terms of the GNU General Public License as published by
  9. | the Free Software Foundation, either version 3 of the License, or
  10. | (at your option) any later version.
  11. | EasyArchive is distributed in the hope that it will be useful,
  12. | but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. | See the GNU General Public License for more details on http://www.gnu.org/licenses/
  15. +--------------------------------------------------
  16. http://www.phpclasses.org/browse/package/4239.html **/
  17. class tar
  18. { /* http://www.mkssoftware.com/docs/man4/tar.4.asp */
  19. /**
  20. // You can use this class like that.
  21. $test = new tar;
  22. $test->makeTar('./','./toto.Tar');
  23. var_export($test->infosTar('./toto.Tar'));
  24. $test->extractTar('./toto.Tar', './new/');
  25. **/
  26. function infosTar ($src, $data=true)
  27. {
  28. if ($this->is_tar($src))
  29. {
  30. die('pwd is '.getcwd());
  31. file_put_contents ($tmp=TMP_CACHE_LOCATION.'/~tmp('.microtime().').tar', $src);
  32. $src = $tmp;
  33. }
  34. $ptr = fopen($src, 'r');
  35. while (!feof($ptr))
  36. {
  37. $infos = $this->readTarHeader ($ptr);
  38. if ($infos['name'])
  39. {
  40. if (!$data) unset($infos['data']);
  41. $result[$infos['name']]=$infos;
  42. }
  43. }
  44. if (is_file($tmp)) unlink($tmp);
  45. return $result;
  46. }
  47. function makeTar($src, $dest=false)
  48. {
  49. $src = is_array($src) ? $src : array($src);
  50. $src = array_map('realpath', $src);
  51. foreach ($src as $item) {
  52. // @weevely3
  53. // Skip empty file to avoid creating empty archvies
  54. if($item)
  55. $Tar .= $this->addTarItem($item.((is_dir($item) && substr($item, -1)!='/')?'/':''), dirname($item).'/');
  56. }
  57. // @weevely3
  58. // When empty, returns
  59. if (empty($Tar)) return $Tar;
  60. $Tar = str_pad($Tar, floor((strlen($Tar) + 10240 - 1) / 10240) * 10240, "\0");
  61. if (empty($dest)) return $Tar;
  62. elseif (file_put_contents($dest, $Tar)) return $dest;
  63. else false;
  64. }
  65. function extractTar ($src, $dest)
  66. {
  67. $ptr = fopen($src, 'r');
  68. while (!feof($ptr))
  69. {
  70. $infos = $this->readTarHeader ($ptr);
  71. // @weevely3
  72. // Sanitize name field from unprintable char, and join name and dest folder properly
  73. $infos['name'] = ltrim(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $infos['name']), DIRECTORY_SEPARATOR);
  74. $dest = trim($dest, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  75. if ($infos['type']=='5' && @mkdir($dest.$infos['name'], 0775, true))
  76. $result[]=$dest.$infos['name'];
  77. elseif (($infos['type']=='0' || $infos['type']==chr(0)) && file_put_contents($dest.$infos['name'], $infos['data'])) {
  78. $result[]=$dest.$infos['name'];
  79. // @weevely3
  80. // Better to not set 0775 on already existant folders
  81. //if ($infos)
  82. chmod($dest.$infos['name'], 0775);
  83. }
  84. }
  85. return $result;
  86. }
  87. function is_tar($str)
  88. {
  89. $block = substr($str,0, 512);
  90. if (strlen($block)!=512) return false;
  91. $realchecksum = octdec(substr($str,148,8));
  92. $checksum = 0;
  93. $block = substr_replace($block, ' ', 148, 8);
  94. for ($i = 0; $i < 512; $i++)
  95. $checksum += ord(substr($block, $i, 1));
  96. if ($realchecksum==$checksum) return true;
  97. return false;
  98. }
  99. function tarHeader512($infos)
  100. { /* http://www.mkssoftware.com/docs/man4/tar.4.asp */
  101. $bigheader = $header = '';
  102. if (strlen($infos['name100'])>100)
  103. {
  104. $bigheader = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12",
  105. '././@LongLink','0000000','0000000','0000000',
  106. sprintf("%011o", strlen($infos['name100'])),'00000000000',
  107. ' ', 'L', '', 'ustar ', '0',
  108. $infos['userName32'],
  109. $infos['groupName32'],'','','','');
  110. $bigheader .= str_pad($infos['name100'], floor((strlen($infos['name100']) + 512 - 1) / 512) * 512, "\0");
  111. $checksum = 0;
  112. for ($i = 0; $i < 512; $i++)
  113. $checksum += ord(substr($bigheader, $i, 1));
  114. $bigheader = substr_replace($bigheader, sprintf("%06o", $checksum)."\0 ", 148, 8);
  115. }
  116. $header = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12", // book the memorie area
  117. substr($infos['name100'],0,100), // 0 100 File name
  118. str_pad(substr(sprintf("%07o",$infos['mode8']),-4), 7, '0', STR_PAD_LEFT), // 100 8 File mode
  119. sprintf("%07o", $infos['uid8']), // 108 8 Owner user ID
  120. sprintf("%07o", $infos['gid8']), // 116 8 Group user ID
  121. sprintf("%011o", $infos['size12']), // 124 12 File size in bytes
  122. sprintf("%011o", $infos['mtime12']), // 136 12 Last modification time
  123. ' ', // 148 8 Check sum for header block
  124. $infos['link1'], // 156 1 Link indicator / ustar Type flag
  125. $infos['link100'], // 157 100 Name of linked file
  126. 'ustar ', // 257 6 USTAR indicator "ustar"
  127. ' ', // 263 2 USTAR version "00"
  128. $infos['userName32'], // 265 32 Owner user name
  129. $infos['groupName32'], // 297 32 Owner group name
  130. '', // 329 8 Device major number
  131. '', // 337 8 Device minor number
  132. $infos['prefix155'], // 345 155 Filename prefix
  133. ''); // 500 12 ??
  134. $checksum = 0;
  135. for ($i = 0; $i < 512; $i++)
  136. $checksum += ord(substr($header, $i, 1));
  137. $header = substr_replace($header, sprintf("%06o", $checksum)."\0 ", 148, 8);
  138. return $bigheader.$header;
  139. }
  140. function addTarItem ($item, $racine)
  141. {
  142. $infos['name100'] = str_replace($racine, '', $item);
  143. list (, , $infos['mode8'], , $infos['uid8'], $infos['gid8'], , , , $infos['mtime12'] ) = stat($item);
  144. $infos['size12'] = is_dir($item) ? 0 : filesize($item);
  145. $infos['link1'] = is_link($item) ? 2 : is_dir ($item) ? 5 : 0;
  146. $infos['link100'] == 2 ? readlink($item) : "";
  147. $a=function_exists('posix_getpwuid')?posix_getpwuid (fileowner($item)):array('name'=>'Unknown');
  148. $infos['userName32'] = $a['name'];
  149. $a=function_exists('posix_getgrgid')?posix_getgrgid (filegroup($item)):array('name'=>'Unknown');
  150. $infos['groupName32'] = $a['name'];
  151. $infos['prefix155'] = '';
  152. $header = $this->tarHeader512($infos);
  153. $data = str_pad(file_get_contents($item), floor(($infos['size12'] + 512 - 1) / 512) * 512, "\0");
  154. if (is_dir($item))
  155. {
  156. $lst = scandir($item);
  157. array_shift($lst); // remove ./ of $lst
  158. array_shift($lst); // remove ../ of $lst
  159. foreach ($lst as $subitem)
  160. $sub .= $this->addTarItem($item.$subitem.(is_dir($item.$subitem)?'/':''), $racine);
  161. }
  162. return $header.$data.$sub;
  163. }
  164. function readTarHeader ($ptr)
  165. {
  166. $block = fread($ptr, 512);
  167. if (strlen($block)!=512) return false;
  168. $hdr = unpack ("a100name/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100symlink/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix/a12temp", $block);
  169. $hdr['mode']=$hdr['mode']+0;
  170. $hdr['uid']=octdec($hdr['uid']);
  171. $hdr['gid']=octdec($hdr['gid']);
  172. $hdr['size']=octdec($hdr['size']);
  173. $hdr['mtime']=octdec($hdr['mtime']);
  174. $hdr['checksum']=octdec($hdr['checksum']);
  175. $checksum = 0;
  176. $block = substr_replace($block, ' ', 148, 8);
  177. for ($i = 0; $i < 512; $i++)
  178. $checksum += ord(substr($block, $i, 1));
  179. if (isset($hdr['name']) && $hdr['checksum']==$checksum)
  180. {
  181. if ($hdr['name']=='././@LongLink' && $hdr['type']=='L')
  182. {
  183. $realName = substr(fread($ptr, floor(($hdr['size'] + 512 - 1) / 512) * 512), 0, $hdr['size']-1);
  184. $hdr2 = $this->readTarHeader ($ptr);
  185. $hdr2['name'] = $realName;
  186. return $hdr2;
  187. }
  188. elseif (strtolower(substr($hdr['magic'], 0, 5) == 'ustar'))
  189. {
  190. if ($hdr['size']>0)
  191. $hdr['data'] = substr(fread($ptr, floor(($hdr['size'] + 512 - 1) / 512) * 512), 0, $hdr['size']);
  192. else $hdr['data'] = '';
  193. return $hdr;
  194. }
  195. else return false;
  196. }
  197. else return false;
  198. }
  199. }