OldCacicHelper.php
9.59 KB
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
<?php
namespace Cacic\WSBundle\Helper;
use Symfony\Component\HttpFoundation\Request;
//use Symfony\Component\HttpKernel\Kernel;
class OldCacicHelper
{
/*
* Diretório do Symfony
* @var string
*/
public $rootDir;
/*
* No construtor recebo os parâmetros do Kernel do Symfony
*/
public function __construct($kernel) {
// Não sei porquê ele retorna no diretório app
$this->rootDir = $kernel->getRootDir() . "/..";
}
public function getRootDir() {
// Tenta consertar caminho do diretório
$rootDir = realpath($this->rootDir);
return $rootDir;
}
/*
* Configuração do arquivo de hashes e executáveis que será lido pelo Agente
*/
public function iniFile() {
return OldCacicHelper::getRootDir() . OldCacicHelper::CACIC_PATH_RELATIVO_DOWNLOADS . 'versions_and_hashes.ini';
}
// define o nome do agente principal do CACIC
const CACIC_MAIN_PROGRAM_NAME = 'cacic280';
// define o nome da pasta local para o CACIC
const CACIC_LOCAL_FOLDER_NAME = 'Cacic';
// define o nome da pasta de scripts de interface com os agentes
const CACIC_WEB_SERVICES_FOLDER_NAME = 'ws/';
// define chave para agentes CACIC
const CACIC_KEY = 'CacicBrasil';
// define chave para agentes CACIC
//const CACIC_PATH = '/srv/cacic3';
// define IV para agentes CACIC
const CACIC_IV = 'abcdefghijklmnop';
// define path para componentes de instalação, coleta de dados de patrimônio e cliente de Suporte Remoto do CACIC
const CACIC_PATH_RELATIVO_DOWNLOADS = '/web/downloads/';
// Arquivo com hashes dos agentes
//const iniFile = '/srv/cacic3/downloads/versions_and_hashes.ini';
/**
*
* Converte string no padrão Camel Case para o padrão com Underscore
* Ex.: idGrupoUsuario -> id_grupo_usuario
* @param string $string
*/
public static function camelCaseToUnderscore( $string )
{
$string = preg_replace('/(?<=\\w)(?=[A-Z])/',"_$1", $string);
$string = strtolower($string);
return $string;
}
//---------------------------------------------------------------------------------
// Substituir alguns valores inválidos ao tráfego HTTP
//
// @return String contendo o string com valores inv�lidos substituidos por v�lidos
//---------------------------------------------------------------------------------
public static function replaceInvalidHTTPChars($pStrString)
{
$strNewString = str_replace('+' , '[[MAIS]]' , $pStrString);
$strNewString = str_replace(' ' , '[[ESPACE]]' , $strNewString);
$strNewString = str_replace('"' , '[[AD]]' , $strNewString);
$strNewString = str_replace("'" , '[[AS]]' , $strNewString);
$strNewString = str_replace('\\' , '[[BarrInv]]' , $strNewString);
return $strNewString;
}
//------------------------------------------------------------------------------
// Repor valores substituidos durante tr�fego HTTP
//
// @return String contendo o string com valores substituidos
//------------------------------------------------------------------------------
public static function replacePseudoTagsWithCorrectChars($pStrString)
{
// Conven��es Adotadas para as Substitui��es
// -----------------------------------------
// [[MAIS]] => Sinal de Mais => "+" (comumente interpretado como espa�o, prejudicando a decriptografia) (Deve ser substitu�do ANTES da decriptografia!!!!)
// [[BarrInv]] => Barra Invertida => "\" (comumente interpretado como ESCAPE na recep��o)
// [[AS]] => Aspa Simples => "'"
// [[AD]] => Aspa Dupla => '"'
// [[ESPACE]] => Espa�o => ' '
// =============================================================================================
$strNewString = str_replace('[[MAIS]]' , '+' , $pStrString);
$strNewString = str_replace('[[ESPACE]]' , ' ' , $strNewString);
$strNewString = str_replace('[[AD]]' , '"' , $strNewString);
$strNewString = str_replace('[[AS]]' , "'" , $strNewString);
$strNewString = str_replace('[[BarrInv]]' , '.' , $strNewString); // Ao substituir [[BarrInv]] por "\\" tive problemas, prefer� deixar "."
return $strNewString;
}
public static function enCrypt( Request $request ,$pStrPlainData, $pIntCompressLevel=0, $pStrPaddingKey = '', $pBoolForceEncrypt = false)
{
$pStrCipherKey = $pStrPaddingKey;
if ((($request->get('cs_cipher')=='1') && !$request->request->get('cs_debug') || $pBoolForceEncrypt) )
{
$strResult = base64_encode(@mcrypt_cbc(MCRYPT_RIJNDAEL_128,OldCacicHelper::CACIC_KEY,$pStrPlainData,MCRYPT_ENCRYPT,OldCacicHelper::CACIC_IV));
$strResult .= '__CRYPTED__';
}
else
$strResult = $pStrPlainData;
if (($request->get('cs_compress') == '1' || $request->get('cs_compress') == '2') && $pIntCompressLevel > 0)
$strResult = gzdeflate($strResult,$pIntCompressLevel);
$strResult = self::replaceInvalidHTTPChars($strResult);
return trim($strResult);
}
// ---------------------------------
// Metodo para descriptografia
// To decrypt values
// p_cs_cipher => Y/N
// ---------------------------------
public static function deCrypt(Request $request, $pStrCriptedData, $pStrPaddingKey = '', $pBoolForceDecrypt = false)
{
// Bloco de Substituições para antes da Decriptação
// ------------------------------------------------
// Razão: Dependendo da configuração do servidor, os valores
// enviados, pertinentes é criptografia, tendem a ser interpretados incorretamente.
// Obs.: Vide Lista de Convenções Abaixo
// =======================================================================================
$ppStrCriptedData = str_ireplace('[[MAIS]]','+',$pStrCriptedData,$countMAIS);
// =======================================================================================
if ( (substr($ppStrCriptedData,-11) == '__CRYPTED__') && ((($request->get('cs_cipher')=='1') && !$request->get('cs_debug')) || $pBoolForceDecrypt))
{
$ppStrCriptedData = str_replace('__CRYPTED__' , '' , $ppStrCriptedData);
$strResult = (trim($ppStrCriptedData)<>''?@mcrypt_decrypt(MCRYPT_RIJNDAEL_128,OldCacicHelper::CACIC_KEY,base64_decode($ppStrCriptedData),MCRYPT_MODE_CBC,OldCacicHelper::CACIC_IV):'');
}
else
$strResult = $pStrCriptedData;
// Bloco de Substituições para depois da Decriptação
// -------------------------------------------------
// Razão: idem acima, porém, com dados pertinentes aos valores a serem recebidos
// =============================================================================
$strResult = self::replacePseudoTagsWithCorrectChars($strResult);
// =============================================================================
if ($request->get('cs_compress') == '1')
$strResult = gzinflate($strResult);
// Aqui retiro do resultado a ocorrência do preenchimento, caso exista. (o agente Python faz esse preenchimento)
if ($pStrPaddingKey <> '')
{
$char = substr($pStrPaddingKey,0,1);
$re = "/".$char."*$/";
$strResult = preg_replace($re, "", $strResult);
}
return trim($strResult);
}
public function getTest( Request $request){
$iniFile = $this->iniFile();
if ( $iniFile )
{
$arrVersionsAndHashes = parse_ini_file( $iniFile );
return array(
'INSTALLCACIC.EXE_HASH' => OldCacicHelper::EnCrypt($request, $arrVersionsAndHashes['installcacic.exe_HASH'],true),
'MainProgramName' => OldCacicHelper::CACIC_MAIN_PROGRAM_NAME.'.exe',
'LocalFolderName' => OldCacicHelper::CACIC_LOCAL_FOLDER_NAME
);
} else {
error_log("ERRO: Arquivo .ini de configuração dos binários não encontrado");
}
}
/*
* Responsável por autenticação do agente CACIC
*/
public static function autenticaAgente(Request $request)
{
if( ( $request->get('HTTP_USER_AGENT') != "YwpgjzZ86/eCsjvOki1KkQ==__CRYPTED__") ||
( $request->get('PHP_AUTH_USER') != "Dcr8b5IfZOJjt6qyH5dGyw==__CRYPTED__") ||
( $request->get('PHP_AUTH_PW') != "Yot8BeM9lOh431SB7dYQXw==__CRYPTED__")
)
{
echo 'CACIC URL Access Denied.';
die;
}
}
public static function getOnlyFileName($pStrFullFileName)
{
$strResult = str_replace('/' ,'#SLASH#',$pStrFullFileName);
$strResult = str_replace('\\','#SLASH#',$strResult);
$arrResult = explode('#SLASH#',$strResult);
return $arrResult[count($arrResult)-1];
}
public static function stripos2($strString, $strSubString, $boolRetornaPosicao = true)
{
$intPos = strpos($strString, stristr( $strString, $strSubString ));
if (!$boolRetornaPosicao)
$intPos = (($intPos < 0 || trim($intPos) == '') ? 0 : 1);
return $intPos;
}
public static function udate($format = 'u', $utimestamp = null) {
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
}