CGI-IDS
view release on metacpan or search on metacpan
lib/CGI/IDS.pm view on Meta::CPAN
# normalize pipe separated request parameters
$value = preg_replace(qr/\|(\w+=\w+)/m, '&$1', $value);
# normalize ampersand listings
$value = preg_replace(qr/(\w\s)&\s(\w)/, '$1$2', $value);
return $value;
}
#****if* IDS/_run_centrifuge
# NAME
# _run_centrifuge
# DESCRIPTION
# The centrifuge prototype
# INPUT
# value the string to convert
# OUTPUT
# value converted string
# SYNOPSIS
# IDS::_run_centrifuge($value);
#****
sub _run_centrifuge {
my ($value) = @_;
my $threshold = 3.49;
if (strlen($value) > 25) {
# strip padding
my $tmp_value = preg_replace(qr/\s{4}|==$/m, '', $value);
$tmp_value = preg_replace(
qr/\s{4}|[\p{L}\d\+\-=,.%()]{8,}/m,
'aaa',
$tmp_value
);
# Check for the attack char ratio
$tmp_value = preg_replace(qr/([*.!?+-])\1{1,}/m, '$1', $tmp_value);
$tmp_value = preg_replace(qr/"[\p{L}\d\s]+"/m, '', $tmp_value);
my $stripped_length = strlen(
preg_replace(qr/[\d\s\p{L}\.:,%&\/><\-)!]+/m,
'',
$tmp_value)
);
my $overall_length = strlen(
preg_replace(
qr/([\d\s\p{L}:,\.]{3,})+/m,
'aaa',
preg_replace(
qr/\s{2,}/ms,
'',
$tmp_value
)
)
);
if ($stripped_length != 0 &&
$overall_length/$stripped_length <= $threshold
) {
$value .= "\n".'$[!!!]';
}
}
if (strlen($value) > 40) {
# Replace all non-special chars
my $converted = preg_replace(qr/[\w\s\p{L},.:!]/, '', $value);
# Split string into an array, unify and sort
my @array = str_split($converted);
my %seen = ();
my @unique = grep { ! $seen{$_} ++ } @array;
@unique = sort @unique;
# Normalize certain tokens
my %schemes = (
'~' => '+',
'^' => '+',
'|' => '+',
'*' => '+',
'%' => '+',
'&' => '+',
'/' => '+',
);
$converted = implode('', @unique);
$converted = str_replace([keys %schemes], [values %schemes], $converted);
$converted = preg_replace(qr/[+-]\s*\d+/, '+', $converted);
$converted = preg_replace(qr/[()[\]{}]/, '(', $converted);
$converted = preg_replace(qr/[!?:=]/, ':', $converted);
$converted = preg_replace(qr/[^:(+]/, '', stripslashes($converted)); #/
# Sort again and implode
@array = str_split($converted);
@array = sort @array;
$converted = implode('', @array);
if (preg_match(qr/(?:\({2,}\+{2,}:{2,})|(?:\({2,}\+{2,}:+)|(?:\({3,}\++:{2,})/, $converted)) {
return $value . "\n" . $converted;
}
}
return $value;
}
#------------------------- PHP functions ---------------------------------------
#****if* IDS/array_sum
# NAME
# array_sum
# DESCRIPTION
# Equivalent to PHP's array_sum, sums all array values
# INPUT
# array the string to convert
# OUTPUT
# sum sum of all array values
# SYNOPSIS
# IDS::array_sum(@array);
#****
sub array_sum {
( run in 2.643 seconds using v1.01-cache-2.11-cpan-524268b4103 )