A website had an external website linked by a iframe
, for example:
example.com/hls/archive.php?token=f4290354ed8529245633fd8266a8238c44e4ef5aa87d50ff16
I liked that content and linked it on my website, but it didn't show me the data after analyzing your website one after another I realized that my URL where the iframe
content was had to add this:
misite.com/iframe.php?noimportaesto=example.com
The domain taken by $_get
= =example.com
was the key token f4290354ed8529245633fd8266a8238c44e4ef5aa87d50ff16
to access the content.
From there, my idea is born to block the access of each topic that my content subdomain has (ex. view.site.com/contenido1.php
) block access if it enters with an token
expired or invalid one, and if that is the case show a warning message communicating that you will be redirected in x
seconds to another website.
But access to the content must be unique, only for that user, that is, they cannot share the generated link with the token
, and if they do, the content is not displayed.
And that this token
generated is only valid for the content that was redirected from the domain example.com
to the content: that only that URLview.site.com/contenido1.php
allows access , if you try to access other content , that is not valid, you must always generate a from the domain to access to said or other content.contenido2.php
token
example.com
If the security of is broken
token cookie
or is not found, it simply becomesfalse
, and if it exists it becomestrue
.
The token will be generated using new PHP technology , including the use of: bin2hex(random_bytes)
orbin2hex(openssl_random_pseudo_bytes)
.
No use of database or use of .htaccess
, the token
only one must be saved in one cookie
and must expire within 4 hours.
So far I can generate a token
, but I don't know how to validate it with the comments:
<?php
//http://php.net/manual/es/function.phpversion.php
//echo 'Versión actual de PHP: ' . phpversion();
session_start();
$expiry_timestamp = time() + $expiry;
//https://davidwalsh.name/random_bytes //https://secure.php.net/random_bytes
//$token = bin2hex(random_bytes(64)); //Disponible apartir de PHP V 7.
$token = bin2hex(openssl_random_pseudo_bytes(64));
$time_token = 12000;
$time_token = srand(floor(time() / $time_token));
//echo $token;
$_SESSION['token']=$token;
?>
<html>
<head>
</head>
<body>
<a href= "view.site.com/contenido1.php?token=<?php echo $_SESSION['token']; ?>">Contenido 1</a>
</body>
</html>
I do not understand very well how this process would work, can you explain the subject better to me. token
I must work something similar Deactivate session after a while only that instead of sesión
using one cookie
that matches the token
only one generated for that user, taking the domain and subdomain as reference keys.
Font:
After the chat, the conclusion is as follows:
www.mipagina.com/seccion1, www.mipagina.com/seccion2, www.mipagina.com/seccion3,etc
www.miotrapagina.com/generarToken
and select which section you want the token toOn the page that generates the token, the form is used to send the parameters
seccion
andtoken
in POST format and thus keep the data safe and out of sightgenerarToken.php
:On the other page where you only want to access via the token in the cookie, you will first have to set that token for the section so:
validarToken.php
:At this point the user has a cookie in which he has one
seccion
with a unique token.The only thing left is to add a few lines of code in all the sections that you want to protect by token :
Explanation of operation
www.mipagina.com
must access towww.mipagina.com/gerarTokne.php?seccion=seccionInicial
which has to have a parametersección
.php
will generate a token for said section and through the form it will redirect the user to the other page through aPOST
, thus sending the section and the token securely.validarToken.php
will receive the requestsPOST
and will add another section to the variable$_SESSION["secciones"]
that contains an array, in which it will establish an expiration date for said token. When finished, it will redirect the user to the section having the token already validated.Edit to use safer techniques.
To generate tokens (hmacs) based on shared keys with expiration time, this algorithm can be used:
envia_hmac.php
recibe_hmac.php
Differences with the simple version:
the token/hash is generated using hash_hmac in this way a hash derived directly from the key is not used but two passes are made generating internal and external keys, in the first pass a hash is generated with the message and the internal key, the second pass takes this hash and generates the final hmac with the foreign key, providing better immunity against length attacks.
the hash algorithm used in this example is sha-256, the full list of supported algorithms is given by the hash_hmac_algos function
the message is built from an array of two public fields: timestamp (to calculate the expire) and the text to send (if either of the two changes, the signature is invalid)
The comparison of the received hash against the hash that we generated from the data and our version of the key is done with the hash_equals function that protects against timing attacks.
** It is possible to generate two hashes and that one only has a verifiable public part, the second hash depends on the first and includes other information, in this way there is another shared secret that can indicate a resource, an extra token that is exchanged behind ( server to server, sockets, shared filesystem, etc...), or an allowed action, current section, etc... (if the hmac with public component is valid, so will the second one)
plain version with md5 hash
In both domains you have defined the secret key
create token code:
code verify token :
You can pass this token through get, post, put in a cookie, etc. The hash method in this case I put md5 but you can use the one you want. I use a separator
x
but if you generate the timestamps with padding it would not be necessary, you can also change the order, insert characters, etc.The key is hashed, it is never (de)encrypted.
For example:
In
dominio.com
generating the links that include the token, wave:sub.dominio.com/pagina-que-no-se-muestra-si-no-hay-token-valido?token=1533076524x2923B7A49999D4AA216CEBC1D7CB9A14
Here the token is being sent by
get
and you would receive it byget
:On the destination page, if it
$_GET['token']
is empty or the token is invalid, you do not show the page.On other pages you can use the same password, or a different password per page.
you do it with a post, and the value can be a $_SESSION that comes from the DB, so when it arrives at your site it verifies that the post exists and that it is also the same as the one in the db, for greater security you can encrypt information before send so users will not know what parameter they are sending