Add a new datasource that: - Receives HTTP requests from remediation components - Apply rules on them to determine whether they are malicious or not - Rules can be evaluated in-band (the remediation component will block the request directly) or out-band (the RC will let the request through, but crowdsec can still process the rule matches with scenarios) The PR also adds support for 2 new hub items: - appsec-configs: Configure the Application Security Engine (which rules to load, in which phase) - appsec-rules: a rule that is added in the Application Security Engine (can use either our own format, or seclang) --------- Co-authored-by: alteredCoder <kevin@crowdsec.net> Co-authored-by: Sebastien Blot <sebastien@crowdsec.net> Co-authored-by: mmetc <92726601+mmetc@users.noreply.github.com> Co-authored-by: Marco Mariani <marco@crowdsec.net>
129 lines
2.1 KiB
Go
129 lines
2.1 KiB
Go
package hubtest
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func sortedMapKeys[V any](m map[string]V) []string {
|
|
keys := make([]string, 0, len(m))
|
|
for k := range m {
|
|
keys = append(keys, k)
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
return keys
|
|
}
|
|
|
|
func Copy(src string, dst string) error {
|
|
content, err := os.ReadFile(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = os.WriteFile(dst, content, 0o644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// checkPathNotContained returns an error if 'subpath' is inside 'path'
|
|
func checkPathNotContained(path string, subpath string) error {
|
|
absPath, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
absSubPath, err := filepath.Abs(subpath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
current := absSubPath
|
|
|
|
for {
|
|
if current == absPath {
|
|
return fmt.Errorf("cannot copy a folder onto itself")
|
|
}
|
|
|
|
up := filepath.Dir(current)
|
|
if current == up {
|
|
break
|
|
}
|
|
|
|
current = up
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func CopyDir(src string, dest string) error {
|
|
err := checkPathNotContained(src, dest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
f, err := os.Open(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
file, err := f.Stat()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !file.IsDir() {
|
|
return fmt.Errorf("Source " + file.Name() + " is not a directory!")
|
|
}
|
|
|
|
err = os.MkdirAll(dest, 0755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
files, err := os.ReadDir(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, f := range files {
|
|
if f.IsDir() {
|
|
if err = CopyDir(filepath.Join(src, f.Name()), filepath.Join(dest, f.Name())); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if err = Copy(filepath.Join(src, f.Name()), filepath.Join(dest, f.Name())); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func IsAlive(target string) (bool, error) {
|
|
start := time.Now()
|
|
for {
|
|
conn, err := net.Dial("tcp", target)
|
|
if err == nil {
|
|
log.Debugf("appsec is up after %s", time.Since(start))
|
|
conn.Close()
|
|
return true, nil
|
|
}
|
|
time.Sleep(500 * time.Millisecond)
|
|
if time.Since(start) > 10*time.Second {
|
|
return false, fmt.Errorf("took more than 10s for %s to be available", target)
|
|
}
|
|
}
|
|
}
|