Coder Social home page Coder Social logo

tonnyadhi / coraza-waf Goto Github PK

View Code? Open in Web Editor NEW

This project forked from corazawaf/coraza

0.0 0.0 0.0 26.4 MB

Coraza WAF is a golang modsecurity compatible web application firewall library

Home Page: https://coraza.io

License: Apache License 2.0

Makefile 0.26% Go 99.74%

coraza-waf's Introduction

Coraza Web Application Firewall

Build Status CodeQL Maintainability Rating Coverage GoDoc Project Status: Active โ€“ The project has reached a stable, usable state and is being actively developed.

Welcome to Coraza Web Application Firewall, this project is a Golang port of ModSecurity with the goal to become the first enterprise-grade Open Source Web Application Firewall, flexible and powerful enough to serve as the baseline for many projects.

Prerequisites

  • Linux distribution (Debian and Centos are recommended, Windows is not supported)
  • Golang compiler v1.16+

Optional Prerequisites

In this Coraza version, you can set CGO_ENABLED to 1 or 0, if you set it to 1, you will be required to link libinjection and libpcre to enable PCRE expressions, @detectSQLi and @detectXSS, if you set it to 0 you won't need any dynamic library but your implementation won't support @detectSQLi, @detectXSS nor PCRE expressions, which means OWASP CRS won't work.

Future versions of Coraza will fully remove CGO.

CGO Enabled CGO Disabled
@detectSQLi Yes No
@detectXSS Yes No
PCRE regex Yes No
RE2 regex Yes Yes
OWASP CRS Yes No

If you want to install Coraza with CGO support, you will need:

  • libpcre-dev (apt install libpcre++-dev for Ubuntu)
  • CGO_ENABLED environmental variable must be set to 1
  • libinjection must be installed and linked

Running the test suite

Run the go tests:

go test ./...
go test -race ./...

Run the test suite against OWASP CRS

You can run the testsuite using our OWASP CRS test docker image, it will run a Coraza instance using Caddy and go-ftw

git clone https://github.com/jptosso/coraza-ruleset
cd coraza-ruleset
docker build . -t crs
docker run crs -name crs

Your first Coraza WAF project

Make sure CGO_ENABLED=1 env is set before compiling and all dependencies are met.

package main
import(
	"fmt"
	engine"github.com/jptosso/coraza-waf"
	"github.com/jptosso/coraza-waf/seclang"
)

func main() {
	// First we initialize our waf and our seclang parser
	waf := engine.NewWaf()
	parser := seclang.NewParser(waf)

	// Now we parse our rules
	parser.FromString(`SecRule REMOTE_ADDR "@rx .*" "id:1,phase:1,drop"`)

	// Then we create a transaction and assign some variables
	tx := waf.NewTransaction()
	tx.ProcessConnection("127.0.0.1", 8080, "127.0.0.1", 12345)

	tx.ProcessRequestHeaders()

	// Finally we check the transaction status
	if tx.Interrupted() {
		fmt.Println("Transaction was interrupted")
	}
}

Integrate with any framework

Using the standard net/http library:

package main
import(
	engine"github.com/jptosso/coraza-waf"
	"github.com/jptosso/coraza-waf/seclang"
	"net/http"
)

func SomeErrorPage(w http.ResponseWriter) {
	w.WriteHeader(403)
	w.Write([]byte("WAF ERROR")
}

func someHandler(waf *engine.Waf) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    tx := waf.NewTransaction()
	tx.ProcessRequest(r)
	if tx.Interruption != nil {
		SomeErrorPage(w)
	}
  })
}

Responses are harder to handle, because we must intercept the response writers and integrate them with the Coraza BodyReader.

Handling HTTP responses with Coraza

Responses are usually long buffers, so duplicating the response or buffering it in memory is hard. In order to avoid issues while handling long buffers Coraza provides the engine.BodyReader struct, it will handle long buffers storing them to temporary files if needed.

func someHandler(waf *engine.Waf) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		tx := waf.NewTransaction()
		tx.ProcessRequest(r)
		if tx.Interruption != nil {
			SomeErrorPage(w)
		}
		// We will use the Coraza response reader:
		tx.ProcessResponseHeaders()
		tx.ResponseBuffer.Write([]byte("Some of the response body"))
		tx.ProcessResponseBody()
		// We will dump the buffered response into the response writer:
		io.Copy(w, tx.ResponseBuffer)
	})
}

Compatibility status

We have currently achieved a 91% compatibility with OWASP CRS, some features are under development, like:

  • Persistent Collections
  • Some operators: fuzzyHash
  • Lua is still being tested, it may be replaced with WASM

Why Coraza WAF?

Philosophy

  • Simplicity: Anyone should be able to understand and modify Coraza WAF's source code
  • Extensibility: It should be easy to extend Coraza WAF with new functionalities
  • Innovation: Coraza WAF isn't just a ModSecurity port, it must include awesome new functions (in the meantime it's just a port ๐Ÿ˜…)
  • Community: Coraza WAF is a community project and everyone's idea will be heard

Roadmap (long term)

  • WASM scripts support, Lua was removed
  • Performance improvements
  • More tests and documentation
  • Integrated DDOS protection and directives with iptables(And others) integration
  • Integrated protocol validations (rfc2616)
  • Integrated CSRF protection
  • Integrated bot detection with captcha
  • More loggers and persistence engines
  • More integrations (traefik, gin and buffalo)
  • Open Policy Agent package (OPA)
  • Online sandbox
  • HTTP/2 and HTTP/3 support
  • Enhanced rule profiling
  • Native antivirus integration (maybe)
  • Automatic coreruleset integration (download and setup) (maybe)
  • Enhanced data masking features
  • Enhanced data signing features (cookies, forms, etc)
  • OpenAPI enforcement
  • JWT enforcement
  • JSON and YAML query

Coraza WAF implementations

Some useful tools

Troubleshooting

How to contribute

Contributions are welcome, there are so many TODOs, also functionalities, fixes, bug reports and any help you can provide. Just send your PR.

cd /path/to/coraza
egrep -Rin "TODO|FIXME" -R --exclude-dir=vendor *

Useful links

Special thanks

  • Modsecurity team for creating SecLang
  • OWASP Coreruleset team for the CRS and their feedback

About

The name Coraza is trademarked, Coraza is a registered trademark of Juan Pablo Tosso.

coraza-waf's People

Contributors

jptosso avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.