Recrear COOKIE de SMF. Recreate the SMF COOKIE.

Para Español leer mas abajo.
For the ENGLISH version, click here.

Recrear COOKIE de SMF

Para los que no lo sepan, SMF (Simple Machines Forum) es un CMS para crear tu propio foro. La cosa es que si deseas integrarlo con tu sitio web aparte, debes utilizar una API no oficial o el SSI.php que incluye el CMS.


Ninguna de estas 2 cosas me convence. La API principalmente porque no es oficial y no tiene soporte y el SSI porque trae demasiadas cosas inútiles si lo que se quiere es simplemente hacer un login. Ademas, si quieres integrar el login_ssi debes indicar una session para que no te redireccione directamente al index del foro.. son demasiados lios para un simple login.

Resulta, que estas cosas se deberian encontrar en la documentacion de SMF o buscando en Google. PERO, parece que a los desarrolladores de SMF no les convence que nosotros mismos creemos nuestras sessiones. La razón según ellos: "No ayudamos a hacer SMF (el foro) mas inseguro". Me parece una estupidez pero es lo que hay.

En este enlace, podéis encontrar a un usuario que hasta ofreció dinero para que le digan como hacerlo... la respuesta que recibió es la que cito mas arriba.

La verdad es que el staff de SMF deja mucho que desear pero bueno. He estado hurgando en los archivos (Sources) de SMF y he descubierto lo que hace falta.

Recreando la cookie

Para empezar vamos a analizar lo que contiene la cookie de SMF una vez nos logueamos dentro. Con este simple codigo, podemos ver lo que contiene la cookie.

$data = $_COOKIE['SMFCookie956'];
print_r($data);

A tener en cuenta que el nombre de la $_COOKIE es aleatorio para cada instalación SMF. Podeis ver el vuestro en el archivo Settings.php bajo la variable $cookiename.

El resultado del código de arriba en mi caso es este:
a:4:{i:0;s:1:"4";i:1;s:40:"f1deff63eaefee8da7eba057991de483fa8fe0ff";i:2;i:1358471211;i:3;i:2;}
Obviamente varia muchísimo dependiendo de muchas cosas. Pero la estructura es la misma.

El caso es que esto esta serializado. Al des-serializarlo, obtenemos esto:
Array
(
    [0] => 4
    [1] => f1deff63eaefee8da7eba057991de483fa8fe0ff
    [2] => 1358471211
    [3] => 2
)

De aquí podemos obtener 4 datos. Segun el archivo Subs-Auth.php, estos datos son:
[0] => ID del usuario
[1] => passwd.salt => contraseña del usuario + salt.
[2] => El time() en el que se creo la cookie + su duración.
[3] => El tipo de cookie. Segun SMF puede ser 0,1 ó 2. 0 = no definida. 1 = local cookie, 2 = global cookie.

Teniendo estos datos es bastante sencillo recrear la cookie. Aquí os dejo un script comentado de como hacerlo.
<?php
// Se obtiene de Settings.php
$cookiename = 'SMFCookie956';
// Base de datos... member_id
$id = '4';
// SHA1 - Contraseña del usuario.
// Si usan la base de datos para el usuario, este es member_name
// de la tabla smf_members
$passwd = sha1(strtolower('usuario').'contraseña');
// El salt. ES ALEATORIO POR CADA USUARIO
// Segun se SMF lo genera asi: $salt = substr(md5(rand(0,9999)),0,4);
$salt = 'd33c';
// Unimos el passwd y el salt.
$pwd_salt = sha1($passwd.$salt);
// Tiempo actual + tiempo que duraria la cookie (el que quieran)
$time = time()+9999;
// Tipo de cookie. Yo lo pongo en Global pero puede ser 1= local.
$type = 2;

// Hacemos serialize en array
$data['final'] = serialize(array($id,$pwd_salt,$time,$type));

// Ponemos la cookie
setcookie($cookiename,$data['final']);
?>

Con esto, tendríamos la cookie definida y si entramos a SMF estaríamos logueados.
Sin mas, espero que os sirva. Y de paso, a ver si los del staff de SMF se ponen las pilas con usuarios que quieren integrar sus webs de manera mas eficiente.

Saludos

Recreate the SMF COOKIE



SMF (Simple Machines Forum) is a pretty known CMS. With SMF you can have your own forum. Its nice, secure and extendable. The problem is that if you want to recreate the login cookie at your own, you will have a difficult time. There are 2 ways (without this one :P). The unofficial API and the SSI login function. I don't like neither the API or the SSI. The API is unofficial so, if something happens, SMF won't give you their support and the SSI is just junkware.. too many functions if you want to do a simple login + you have to bypass the forum index...

This type of things should be found in the documentation or at Google. BUT, the SMF staff is not so happy with us (normal users) recreating SMF session at our own. In fact, they won't help you for this reason:
I'm not telling you how to make your forum (and potentially your entire server) insecure

LOL ? In this link, you can find a user asking how to recreate the session.. he/she even offers money to the guy who gives him the "secret" formula.

Recreating the SMF COOKIE

Actually, is pretty easy to recreate the cookie. You can find that in the Subs-Auth.php file in the SMF Sources folder. But let me explain it in more detail. Let's see what does a SMF cookie contain once we are logged in.
Note that the $_COOKIE is random and changes in every SMF installation. You can find yours in the Settings.php file under the variable $cookiename.

This will output something like this:
a:4:{i:0;s:1:"4";i:1;s:40:"f1deff63eaefee8da7eba057991de483fa8fe0ff";i:2;i:1358471211;i:3;i:2;}
.. with different values :D.

Ok, now, this is an array under the serialize function. If we unserialize it, this is what we get:
Array
(
    [0] => 4
    [1] => f1deff63eaefee8da7eba057991de483fa8fe0ff
    [2] => 1358471211
    [3] => 2
)
From the Subs-auth.php you can find that those values are:
[0] => User id.
[1] => User passwd + salt.
[2] => Time() when the cookie was generated + duration time.
[3] => Type of cookie. From the same file we know that: 0 = undefined (will cause logout), 1 = Local cookie, 2 = Global Cookie.

Based on these things, we can know now how to recreate the cookie. Here is a simple script + comments.
<?php
// You can get it from the Settings.php file.
$cookiename = 'SMFCookie956';
// You can find it in the smf_members under member_id.
$id = '4';
// SHA1 - The user password.
// member_name to lower case + passwd
// from the smf_members table.
$passwd = sha1(strtolower('usuario').'contraseña');
// The salt.
// As far as I know SMF generates it like that:
//  $salt = substr(md5(rand(0,9999)),0,4);
$salt = 'd33c';
// Lets merge the passwd and the salt.
$pwd_salt = sha1($passwd.$salt);
// The actual time + the duration of the session.
$time = time()+9999;
// Type of cookie. 2 = global, 1 = local.
$type = 2;

// We serialize the array with the data..
$data = serialize(array($id,$pwd_salt,$time,$type));

// bam, cookie.
setcookie($cookiename,$data);
?>

Hope I just help'd someone. Thanks for reading.
Greetings.

Comentarios

Entradas populares de este blog