Scaricare la presentazione
La presentazione è in caricamento. Aspetta per favore
1
Prof. Stefano Bistarelli
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
2
S. Bistarelli - Metodologie di Secure Programming
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) S. Bistarelli - Metodologie di Secure Programming
3
A1. Unvalidated Input – esempio (1)
Manipolazione dei parametri inviati: Hidden Field Manipulation Le informazioni ricevute a seguito di una richiesta, non vengono validate dall’applicazione web. Questa tecnica può essere utilizzata per accedere alla parte di backend attraverso l’applicazione web in questione. S. Bistarelli - Metodologie di Secure Programming
4
A1. Unvalidated Input – esempio (2)
Altero il valore in 4.999 S. Bistarelli - Metodologie di Secure Programming
5
A1. Unvalidated Input – esempio (3)
S. Bistarelli - Metodologie di Secure Programming
6
A1. Unvalidated Input – esempio (4)
S. Bistarelli - Metodologie di Secure Programming
7
S. Bistarelli - Metodologie di Secure Programming
1 regola! Per evitare XSS (SQL) injection Cookie poisoning Ma anche Arithmetic overflow e buffer overrun che vedremo!! INPUT VALIDATION!! S. Bistarelli - Metodologie di Secure Programming
8
Input validation in php
<?php function validate ($ ) { $hasAtSymbol = strpos($ , $hasDot = strpos($ , "."); if($hasAtSymbol && $hasDot) return true; else return false; } echo ?> S. Bistarelli - Metodologie di Secure Programming
9
S. Bistarelli - Metodologie di Secure Programming
Oppure cosi’? <?php function validate ($ ) { return $ ); } echo ?> S. Bistarelli - Metodologie di Secure Programming
10
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] S. Bistarelli - Metodologie di Secure Programming
11
S. Bistarelli - Metodologie di Secure Programming
- 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 S. Bistarelli - Metodologie di Secure Programming
12
S. Bistarelli - Metodologie di Secure Programming
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 S. Bistarelli - Metodologie di Secure Programming
13
S. Bistarelli - Metodologie di Secure Programming
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. S. Bistarelli - Metodologie di Secure Programming
14
S. Bistarelli - Metodologie di Secure Programming
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"); ?> S. Bistarelli - Metodologie di Secure Programming
15
S. Bistarelli - Metodologie di Secure Programming
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. S. Bistarelli - Metodologie di Secure Programming
16
S. Bistarelli - Metodologie di Secure Programming
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"); ?> S. Bistarelli - Metodologie di Secure Programming
17
S. Bistarelli - Metodologie di Secure Programming
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"); ?> S. Bistarelli - Metodologie di Secure Programming
18
S. Bistarelli - Metodologie di Secure Programming
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. S. Bistarelli - Metodologie di Secure Programming
19
Esempio: valid PG phone number
<?php function isValidPhone($phoneNum) { echo ereg("^\+39 [[:space:]075]-[1-9] [0-9]{7}$", $phoneNum); } ?> S. Bistarelli - Metodologie di Secure Programming
20
S. Bistarelli - Metodologie di Secure Programming
Ma guardiamo un tool (piu’ facile) tool-regex\RegexBuddy.exe Oppure questo generato con visualstudio: lab-regular-expression\RegEx\before\RegexBench\bin\Debug\RegexBench.exe S. Bistarelli - Metodologie di Secure Programming
21
S. Bistarelli - Metodologie di Secure Programming
Lab: regex in asp Matchare Una Un numero telefonico Un cap Per casa Il codice fiscale S. Bistarelli - Metodologie di Secure Programming
22
S. Bistarelli - Metodologie di Secure Programming
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"); S. Bistarelli - Metodologie di Secure Programming
Presentazioni simili
© 2024 SlidePlayer.it Inc.
All rights reserved.