Foros del Web » Programando para Internet » PHP »

Ayuda Seguridad al juntar variable

Estas en el tema de Ayuda Seguridad al juntar variable en el foro de PHP en Foros del Web. Bueno provando mi seguridad web me he topado de que por medio de "Shell" ( asi me dijeron que se llamaba ), me tumvaban el ...
  #1 (permalink)  
Antiguo 27/03/2010, 17:32
Avatar de ONahuelO  
Fecha de Ingreso: junio-2009
Ubicación: Gualeguaychú, Argentina
Mensajes: 144
Antigüedad: 14 años, 10 meses
Puntos: 4
Ayuda Seguridad al juntar variable

Bueno provando mi seguridad web me he topado de que por medio de "Shell" ( asi me dijeron que se llamaba ), me tumvaban el servidor.
Y esoty buscando una forma de juntar los datos previniendo cualquier tipo de ataque que me pueda llegar, ya sea inyeccion sql, o algun otro codigo que me produsca un desface o algo por el estilo.

Mi Web Trabaja asi

funciones.php

Código PHP:
/*
En este archivo estan las funciones para codificar y decodificar una variable
Tambien para incluir un archivo (para que usar ajax)
*/
function variable($str){
    
$a base64_decode($str);
    
$b urldecode($a);
    
$c stripslashes($b);
    
$d htmlentities($cENT_QUOTES'iso-8859-1');
    return 
$d;
}

function 
cod($str){
    
$a htmlentities($strENT_QUOTES'iso-8859-1');
    
$b stripslashes($a);
    
$c urlencode($b);
    
$d base64_encode($c);
    return 
$d;
}

function 
Pagina($pag){

if (
eregi("[^a-zA-Z0-9]"$pag)){
echo 
'<br><br><div align="center"><div class="Div_Externa_Error" ><div class="Error_descrip">Error de Recepcion de variable.</div></div></div><br><br>';
exit();
session_destroy();
return 
false;
}
if (!
file_exists("Paginas/".$pag.".inc.php")){echo '<br><br><div align="center"><div class="Div_Externa_Error" ><div class="Error_descrip">Modulo Ineccistente.</div></div></div><br><br>';}
else{    include 
"Paginas/".$pag.".inc.php"; }
    


Y este es el archivo que carga un php segun la variable juntada:

Código PHP:
define('FUNCIONES','Modulos/fw/funciones.inc.php',true);
require 
'Modulos/fw/servseg.inc.php';
session_start();                                                
ob_start("ob_gzhandler");                                        
header ("Cache-control: private");                                
require (
FUNCIONES);    
$pagina variable($_GET['pag']);
Pagina($pagina);
ob_flush(); 
Y este es mi js:
Código:
function $(id){return document.getElementById(id);}
function http(){
	if(window.XMLHttpRequest){
		return new XMLHttpRequest();	
	}else{
		try{
			return new ActiveXObject('Microsoft.XMLHTTP');
		}catch(e){
			alert('nop');
        	return false;
		}	
	}
}
String.prototype.tratarResponseText=function(){
	var pat=/<script[^>]*>([\S\s]*?)<\/script[^>]*>/ig;
	var pat2=/\b\s+src=[^>\s]+\b/g;
	var elementos = this.match(pat) || [];
	for(i=0;i<elementos.length;i++) {
		var nuevoScript = document.createElement('script');
		nuevoScript.type = 'text/javascript';
		var tienesrc=elementos[i].match(pat2) || [];
		if(tienesrc.length){
			nuevoScript.src=tienesrc[0].split("'").join('').split('"').join('').split('src=').join('').split(' ').join('');
		}else{
			var elemento = elementos[i].replace(pat,'$1','');
			nuevoScript.text = elemento;
		}
		document.getElementsByTagName('body')[0].appendChild(nuevoScript);
	}
	return this.replace(pat,'');
}

function SetContainerHTML(id_contenedor,responseText){
	var mydiv = $(id_contenedor);
	mydiv.innerHTML = responseText.tratarResponseText();
}



function cargar(variables,div){
var ajax=new http();
var url = 'main.php?pag=' + variables;
ajax.open('get',url,true);
ajax.onreadystatechange=function(){
	if(ajax.readyState==4){
		SetContainerHTML(div,ajax.responseText);
		ajax.onreadystatechange=null;
	}
}
ajax.send(null);
}








var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}




/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}



function get(div){
return Base64.encode(escape(document.getElementById(div).value));
}


Nose cual es mi error, o si habria alguna forma mejor de prevenirme contra el codigo malisioso, pero el tema es que de esta forma me lograron tumbar el apache por medio de un shell.
  #2 (permalink)  
Antiguo 27/03/2010, 19:19
Avatar de eddwinpaz  
Fecha de Ingreso: noviembre-2007
Ubicación: Merida , Venezuela
Mensajes: 1.066
Antigüedad: 16 años, 5 meses
Puntos: 25
Respuesta: Ayuda Seguridad al juntar variable

tu te respondistes a ti mismo.. Busca formas de protegerte mediante Apache usando configuraciones standar de seguridad de APACHE. tu codigo se ve bien estructurado aunque te recomendaria poner en practica un consejo que escuche en algun lado y me resulto muy importante..

Muchos programadores se viven matandose la vida protegiendo lo que no quieren que les entre en su sitio web via Forms o consultas SQL, y siempre los hackean... sabes por que ?

"Sencillo..." Acepta solo lo que necesitas y mas nada..

ejemplo: si tienes campo de numeros solo acepta numeros no hagas funciones que filtren ';./,()(*&^%$#@ etc... solo ve que sean numeros y si no lo son niega el campo..


saludos.
__________________
O nos hacemos miserables o nos hacemos fuertes , la cantidad de trabajo es la misma.

Etiquetas: juntar, seguridad, variables
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:52.