All Projects → GramThanos → php-csrf

GramThanos / php-csrf

Licence: MIT License
Single PHP library file for protection over Cross-Site Request Forgery

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to php-csrf

macOS-Security-Updates
Notifies the user when macOS Security components like Gatekeeper and XProtect have been updated
Stars: ✭ 53 (+20.45%)
Mutual labels:  protection
branch-protection-bot
A bot tool to disable and re-enable "Include administrators" option in branch protection
Stars: ✭ 57 (+29.55%)
Mutual labels:  protection
spring-security-jwt-csrf
A demonstration of stateless JWT authentication with Spring Security, Spring Boot and Vue js
Stars: ✭ 62 (+40.91%)
Mutual labels:  csrf
js-confuser
JS-Confuser is a JavaScript obfuscation tool to make your programs *impossible* to read.
Stars: ✭ 38 (-13.64%)
Mutual labels:  protection
csrf
PSR-15 middleware implementing CSRF protection
Stars: ✭ 20 (-54.55%)
Mutual labels:  csrf
framework
A stylish PHP application framework crafted using Slim, Twig, Eloquent and Sentinel designed to get you from clone to production in a matter of minutes.
Stars: ✭ 56 (+27.27%)
Mutual labels:  csrf
fastify-csrf
A fastify csrf plugin.
Stars: ✭ 88 (+100%)
Mutual labels:  csrf
AdGuard-WireGuard-Unbound-Cloudflare
The Ultimate Network Security Guide 🔒 Protection | 🔎 Privacy | 🚀 Performance on home network 24/7 🕛 Accessible anywhere 🌏
Stars: ✭ 160 (+263.64%)
Mutual labels:  protection
stb.core
A NEW ECOSYSTEM OF SOFTWARE SERVICES DRIVEN BY BLOCKCHAIN
Stars: ✭ 15 (-65.91%)
Mutual labels:  protection
JAW
JAW: A Graph-based Security Analysis Framework for JavaScript and Client-side CSRF
Stars: ✭ 26 (-40.91%)
Mutual labels:  csrf
protection
Flexible server protection system (development)
Stars: ✭ 23 (-47.73%)
Mutual labels:  protection
ring-anti-forgery
Ring middleware to prevent CSRF attacks
Stars: ✭ 115 (+161.36%)
Mutual labels:  csrf
security-wrapper
对springSecurity进行二次开发,提供OAuth2授权(支持跨域名,多应用授权)、JWT、SSO、文件上传、权限系统无障碍接入、接口防刷、XSS、CSRF、SQL注入、三方登录(绑定,解绑)、加密通信等一系列安全场景的解决方案
Stars: ✭ 21 (-52.27%)
Mutual labels:  csrf
okta-spring-boot-react-crud-example
Simple CRUD with React and Spring Boot 2.0
Stars: ✭ 214 (+386.36%)
Mutual labels:  csrf
Flag-Capture
Solutions and write-ups from security-based competitions also known as Capture The Flag competition
Stars: ✭ 84 (+90.91%)
Mutual labels:  csrf
Anti-DDOS-Script
Anti DDOS Protection that will stop DDOS from taking down your Linux Server
Stars: ✭ 51 (+15.91%)
Mutual labels:  protection
diwa
A Deliberately Insecure Web Application
Stars: ✭ 32 (-27.27%)
Mutual labels:  csrf
warshield
Warshield is a file encryption and decryption CLI using AES 256 algorithm
Stars: ✭ 29 (-34.09%)
Mutual labels:  protection
SecExample
JAVA 漏洞靶场 (Vulnerability Environment For Java)
Stars: ✭ 228 (+418.18%)
Mutual labels:  csrf
killswitch-windows
VPN kill switch for windows.
Stars: ✭ 22 (-50%)
Mutual labels:  protection

latest release latest release latest release

PHP-CSRF

Cross-Site Request Forgery protection PHP library

PHP-CSRF manage, generate and validate hashes, on the user's session, to provide a basic protection from Cross-Site Request Forgery.


Download

  • Direct download php-csrf.php file (right click save as).
  • Using wget wget -O php-csrf.php https://raw.githubusercontent.com/GramThanos/php-csrf/master/php-csrf.php

Example usage

<?php
    // Include the PHP-CSRF library
    include('php-csrf.php');
    // Start or Resume a session
    session_start();
    // Initialize an instance
    $csrf = new CSRF();

    // If form was submitted
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // Validate that a correct token was given
        if ($csrf->validate('my-form')) {
            // Success
        }
        else {
            // Failure
        }
    }
?>

<!-- Your normal HTML form -->
<form method="POST">
    <!-- Print a hidden hash input -->
    <?=$csrf->input('my-form');?>
    ...
    <input type="submit" value="Submit"/>
</form>

API

Create a csrf object

  • $csrf = new CSRF($session_name='csrf-lib', $input_name='key-awesome', $hashTime2Live=0, $hashSize=64);
    • $session_name the name to be used for the session variable.
    • $input_name the name to be used on the HTML input with the hash.
    • $hashTime2Live the default hash live time of each hash in seconds. Should be >=0. If zero, by default the hash will not expire.
    • $hashSize the default hash size in chars. Should be >0.

Clear excess hashes

  • $deleted = $csrf->clearHashes($context='', $max_hashes=0);
    • $context the name of the group to clear
    • $max_hashes the number of hashes to keep. If the hash population is bigger, the oldest hashes will be deleted.
    • returns the number of deleted hashes.

Generate HTML input element code

  • echo $csrf->input($context='', $time2Live=-1, $max_hashes=5)
    • $context the name of the group to save the hash to. Usually, it is different for each form.
    • $time2Live the hash live time in seconds. If zero, the hash will not expire. If negative, the default value will be used.
    • $max_hashes the hash limit of the group. If the group has already reached this limit, the oldest hash will be discarded.
    • returns a string with the HTML code. Example return value '<input type="hidden" name="key-awesome" value="1234567890ABCDEF1234567890ABCDEF"/>'.

Generate javascript script element code (alternative)

  • echo $csrf->script($context='', $name='', $declaration='var', $time2Live=-1, $max_hashes=5)
    • $context the name of the group to save the hash to. Usually, it is different for each form.
    • $name the name of the javascript variable. If it is empty string, the name of the input_name will be used, but the default one is an invalid variable name.
    • $declaration the declaration key word of the variable, usually var, let or const.
    • $time2Live the hash live time in seconds. If zero, the hash will not expire. If negative, the default value will be used.
    • $max_hashes the hash limit of the group. If the group has already reached this limit, the oldest hash will be discarded.
    • returns a string with the HTML script code. Example return value '<script type="text/javascript">var name = "1234567890ABCDEF1234567890ABCDEF";</script>'.

Generate javascript variable code (alternative)

  • echo $csrf->javascript($context='', $name='', $declaration='var', $time2Live=-1, $max_hashes=5)
    • $context the name of the group to save the hash to. Usually, it is different for each form.
    • $name the name of the javascript variable. If it is empty string, the name of the input_name will be used, but the default one is an invalid variable name.
    • $declaration the declaration key word of the variable, usually var, let or const.
    • $time2Live the hash live time in seconds. If zero, the hash will not expire. If negative, the default value will be used.
    • $max_hashes the hash limit of the group. If the group has already reached this limit, the oldest hash will be discarded.
    • returns a string with the javascript code. Example return value 'var name = "1234567890ABCDEF1234567890ABCDEF";'.

Generate hash as a string (alternative)

  • echo $csrf->string($context='', $time2Live=-1, $max_hashes=5)
    • $context the name of the group to save the hash to. Usually, it is different for each form.
    • $time2Live the hash live time in seconds. If zero, the hash will not expire. If negative, the default value will be used.
    • $max_hashes the hash limit of the group. If the group has already reached this limit, the oldest hash will be discarded.
    • returns a string with the hash. Example return value '1234567890ABCDEF1234567890ABCDEF'.

Check if a valid hash was posted

  • $is_valid = $csrf->validate($context='', $hash=null)
    • $context the name of the group to search for the hash into.
    • $hash the hash to validate. If null, the hash will be retrieved by the $_POST or the $_GET objects.
    • returns true if the validation was successful or false otherwise.

Get the hashes of a context

  • $is_valid = $csrf->getHashes($context='', $max_hashes=-1)
    • $context the name of the group to get its hashes.
    • $max_hashes max number of hashes to get. If negative value, all the hashes will be returned.
    • returns an array of string hashes.

The hashes are saved on the $_SESSION under the a single variable using serialize and unserialize. Thus, if the session expires or get destroyed, the hashes would too.


About the Security

This library uses the openssl_random_pseudo_bytes function to generate random hashes. In order to be sure that your system can produce cryptographically strong hashes, you should run the following PHP code and check the result.

<?php
	// Test if random_pseudo is cryptographically strong in your system
	$hash = openssl_random_pseudo_bytes(32, $crypto_strong);
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>PHP-CSRF Test</title>
	</head>
	<body>
		Is cryptographically strong: <?=($crypto_strong ? 'yes' : 'no');?><br>
	</body>
</html>

This library was created to provide a basic protection from Cross-Site Request Forgery attacks. Thus, sophisticated attacks like a timing attack may break the protection.

By using relative big hash sizes, relative short hash expiration times and small group hash limits, you can strengthen the security.


License

This project is under The MIT license. I do although appreciate attribute.

Copyright (c) 2020 Grammatopoulos Athanasios Vasileios


GramThanos DinoDevs

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].