UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e...

22
UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione Input validation Prof. Stefano Bistarelli C Consiglio Nazionale delle Ricerche Iit Istituto di Informatica e Telematica - Pisa Università “G. d’Annunzio” Dipartimento di Scienze, Pescara

Transcript of UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e...

Page 1: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

UNIVERSITÀ DI PERUGIADIPARTIMENTO DI MATEMATICA E INFORMATICAMaster di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Input validation

Prof. Stefano Bistarelli

C Consiglio Nazionale delle RicercheIit Istituto di Informatica e Telematica - Pisa

Università “G. d’Annunzio”Dipartimento di Scienze, Pescara

Page 2: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

2

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

A1. Unvalidated Input (2)A1. Unvalidated Input (2) E’ necessario avere una classificazione ben precisa di ciò che

sia permesso o meno per ogni singolo parametro dell’applicativo

Ciò include una protezione adeguata per tutti i tipi di dati ricevuti

da una HTTP request, inclusi le URL, i form, i cookie, le query

string, gli hidden field e i parametri.

OWASP WebScarab permette di manipolare tutte le informazioni

da e verso il web browser

OWASP Stinger HTTP request è stato sviluppato da OWASP per

gli ambienti J2EE (motore di validazione)

Page 3: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

3

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Manipolazione dei parametri inviati: Hidden Field Manipulation

A1. Unvalidated Input – A1. Unvalidated Input – esempio (1)esempio (1)

Page 4: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

4

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Altero il valore in

4.999

A1. Unvalidated Input – A1. Unvalidated Input – esempio (2)esempio (2)

Page 5: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

5

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

A1. Unvalidated Input – A1. Unvalidated Input – esempio (3)esempio (3)

Page 6: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

6

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

A1. Unvalidated Input – A1. Unvalidated Input – esempio (4)esempio (4)

Page 7: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

7

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

1 regola!

Per evitare XSS (SQL) injection Cookie poisoning Ma anche Arithmetic overflow e buffer overrun

che vedremo!! INPUT VALIDATION!!

Page 8: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

8

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Input validation in php <?php

function validateEmail($email)

{

$hasAtSymbol = strpos($email, "@");

$hasDot = strpos($email, ".");

if($hasAtSymbol && $hasDot)

return true;

else

return false;

}

echo validateEmail("[email protected]");

?>

Page 9: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

9

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Oppure cosi’? <?php

function validateEmail($email)

{

return ereg("^[a-zA-Z]+@[a-zA-Z]+\.[a-zA-Z]+$", $email);

}

echo validateEmail("[email protected]");

?>

Page 10: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

10

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Espressioni Regolari: - Put a sequence of characters in brackets, and it defines a set of characters, any one of which matches

[abcd]

- Dashes can be used to specify spans of characters in a class

[a-z]

- A caret at the left end of a class definition means the opposite

[^0-9]

Page 11: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

11

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

- Quantifiers

- Quantifiers in braces

Quantifier Meaning

{n} exactly n repetitions {m,} at least m repetitions {m, n} at least m but not more than n repetitions - Other quantifiers (just abbreviations for the most commonly used quantifiers)

- * means zero or more repetitions e.g., \d* means zero or more digits

- + means one or more repetitions e.g., \d+ means one or more digits

- ? Means zero or one e.g., \d? means zero or one digit

Page 12: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

12

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

anchor - Anchors

- The pattern can be forced to match only at the left end with ^; at the end with $

e.g., /^Lee/

matches "Lee Ann" but not "Mary Lee Ann"

/Lee Ann$/

matches "Mary Lee Ann", but not "Mary Lee Ann is nice"

- The anchor operators (^ and $) do not match characters in the string--they match positions, at the beginning or end

Page 13: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

13

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Syntax continued Beginning of string:

To search from the beginning of a string, use ^. For example,

<?php echo ereg("^hello", "hello world!"); ?>

Would return true, however

<?php echo ereg("^hello", "i say hello world"); ?>

would return false, because hello wasn't at the beginning of the string.

End of string:

To search at the end of a string, use $. For example,

<?php echo ereg("bye$", "goodbye"); ?>

Would return true, however

<?php echo ereg("bye$", "goodbye my friend"); ?>

would return false, because bye wasn't at the very end of the string.

Page 14: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

14

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Any single character:

To search for any character, use the dot. For example,

<?php echo ereg(".", "cat"); ?>

would return true, however

<?php echo ereg(".", ""); ?>

would return false, because our search string contains no characters. You can optionally tell the regular expression engine how many single characters it should match using curly braces. If I wanted a match on five characters only, then I would use ereg like this:

<?php echo ereg(".{5}$", "12345"); ?>

The code above tells the regular expression engine to return true if and only if at least five successive characters appear at the end of the string. We can also limit the number of characters that can appear in successive order:

<?php echo ereg("a{1,3}$", "aaa"); ?>

In the example above, we have told the regular expression engine that in order for our search string to match the expression, it should have between one and three 'a' characters at the end.

<?php echo ereg("a{1,3}$", "aaab"); ?>

The example above wouldn't return true, because there are three 'a' characters in the search string, however they are not at the end of the string. If we took the end-of-string match $ out of the regular expression, then the string would match.

We can also tell the regular expression engine to match at least a certain amount of characters in a row, and more if they exist. We can do so like this:

<?php echo ereg("a{3,}$", "aaaa"); ?>

Page 15: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

15

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Repeat character zero or more times

To tell the regular expression engine that a character may exist, and can be repeated, we use the * character. Here are two examples that would return true:

<?php echo ereg("t*", "tom"); ?>

<?php echo ereg("t*", "fom"); ?>

Even though the second example doesn't contain the 't' character, it still returns true because the * indicates that the character may appear, and that it doesn't have to. In fact, any normal string pattern would cause the second call to ereg above to return true, because the 't' character is optional.

Repeat character one or more times

To tell the regular expression engine that a character must exist and that it can be repeated more than once, we use the + character, like this:

<?php echo ereg("z+", "i like the zoo"); ?>

The following example would also return true:

<?php echo ereg("z+", "i like the zzzzzzoo!"); ?>

Repeat character zero or one times

We can also tell the regular expression engine that a character must either exist just once, or not at all. We use the ? character to do so, like this:

<?php echo ereg("c?", "cats are fuzzy"); ?>

If we wanted to, we could even entirely remove the 'c' from the search string shown above, and this expression would still return true. The '?' means that a 'c' may appear anywhere in the search string, but doesn't have to.

Page 16: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

16

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

The space character

To match the space character in a search string, we use the predefined Posix class, [[:space:]]. The square brackets indicate a related set of sequential characters, and ":space:" is the actual class to match (which, in this case, is any white space character). White spaces include the tab character, the new line character, and the space character. Alternatively, you could use one space character (" ") if the search string must contain just one space and not a tab or new line character. In most circumstances I prefer to use ":space:" because it signifies my intentions a bit better than a single space character, which can easy be overlooked. There are several Posix-standard predefined classes that we can match as part of a regular expression, including [:alnum:], [:digit:], [:lower:], etc. A complete list is available here.

We can match a single space character like this:

<?php echo ereg("Mitchell[[:space:]]Harper", "Mitchell Harper"); ?>

We could also tell the regular expression engine to match either no spaces or one space by using the ? character after the expression, like this:

<?php echo ereg("Mitchell[[:space:]]?Harper", "MitchellHarper"); ?>

Page 17: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

17

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Grouping patterns

Related patterns can be grouped together between square brackets. It's really easy to specify that a lower case only or upper case only sequence of characters should exist as part of the search string using [a-z] and [A-Z], like this:

<?php

// Require all lower case characters from first to last

echo ereg("^[a-z]+$", "johndoe"); // Returns true

?>

or like this:

<?php

// Require all upper case characters from first to last

ereg("^[A-Z]+$", "JOHNDOE"); // Returns true?

>

We can also tell the regular expression engine that we expect either lower case or upper case characters. We do this by joining the [a-z] and [A-Z] patterns:

<?php echo ereg("^[a-zA-Z]+$", "JohnDoe"); ?>

In the example above, it would make sense if we could match "John Doe," and not "JohnDoe." We can use the following regular expression to do so:

^[a-zA-Z]+[[:space:]]{1}[a-zA-Z]+$

It's just as easy to search for a numerical string of characters:

<?php echo ereg("^[0-9]+$", "12345"); ?>

Page 18: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

18

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Grouping terms

It's not only search patterns that can be grouped together. We can also group related search terms together using parentheses:

<?php echo ereg("^(John|Jane).+$", "John Doe"); ?>

In the example above, we have a beginning of string character, followed by "John" or "Jane", at least one other character, and then the end of string character. So ...

<?php echo ereg("^(John|Jane).+$", "Jane Doe"); ?>

... would also match our search pattern.

Special character circumstances

Because several characters are used to actually specify the grouping or syntax of a search pattern, such as the parentheses in (John|Jane), we need a way to tell the regular expression engine to ignore these characters and to process them as if they were part of the string being searched and not part of the search expression. The method we use to do this is called "character escaping" and involves propending any "special symbols" with a backslash. So, for example, if I wanted to include the or symbol '|' in my search, then I could do so like this:

<?php echo ereg("^[a-zA-z]+\|[a-zA-z]+$", "John|Jane"); ?>

There are only a handful of symbols that you have to escape. You must escape ^, $, (, ), ., [, |, *, ?, +, \ and {.

Hopefully you've now gotten a bit of a feel for just how powerful regular expressions actually are. Let's now take a look at two examples of using regular expressions to validate a string of data.

Page 19: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

19

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Esempio: valid PG phone number <?php

function isValidPhone($phoneNum)

{

echo ereg("^\+39 [[:space:]075]-[1-9] [0-9]{7}$", $phoneNum);

}

?>

Page 20: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

20

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Ma guardiamo un tool (piu’ facile) tool-regex\RegexBuddy.exe

Oppure questo generato con visualstudio: lab-regular-expression\RegEx\before\RegexB

ench\bin\Debug\RegexBench.exe

Page 21: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

21

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Lab: regex in asp

Matchare Una email Un numero telefonico Un cap Per casa

Il codice fiscale

Page 22: UNIVERSITÀ DI PERUGIA DIPARTIMENTO DI MATEMATICA E INFORMATICA Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della.

S. Bistarelli - Metodologie di Secure Programming

22

Master di I° livello in Sistemi e Tecnologie per la sicurezza dell'Informazione e della Comunicazione

Codice asp

Dare una occhiata alle primitive per match di regex: lab-regular-expression\RegEx\after\RegexLab.sln

if (!Regex.IsMatch(userInput, pattern, RegexOptions.IgnorePatternWhitespace)) {

throw new ValidationException("Malformed zip code");