Java Security & Error prevention Flashcards
Druhy utoku (6)
DOS - denial of service
Code injection
Code corruption
Deserialization
Sensitive data leaks
Directory traversal
Java Security Descriptor - jmeno, co muze obsahovat(3), sample location + obsah
java.security
security policy files, cert keystore files, general settings
policy.url.1=file:${java.home}/conf/security/java.policy
policy.url.2=file:/someDir/java.policy
$JAVA_HOME/conf/security
Java Security Policy Descriptor - jmeno, syntax:
povol pripojeni k localhost:7777
povol cteni a zapis do souboru abc.txt
pro kod z /lib.jar, pokud ma podpis “John and Joe”
grant codeBase “file:/lib.jar” signed “John and Joe” {
permission java.net.SocketPermission “localhost:7777”, “connect”;
permission java.io.FilePermission “abc.txt”, “read,write”;
}
Vytvor povoleni pro poslech localhost:7777, programatically
Zjisti, jestli mas povoleni
Co se stane, pokud ne?
SocketPermission sp = new SocketPermission(“localhost:7777”, “listen”);
AccessController.checkPermission(sp);
vyhodi se AccessControlException
Directory traversal - metody obrany (2)
path.normalize()
path.toRealPath(LinkOption)
Privilegovany kod - co to je
Syntax
pristup se nekontroluje, je proste povolen
AccessController.doPrivileged(new PrivilegedAction<T>() {
public T run() { ... }
})</T>
Utok pri deserializaci - obrana
deserializuji jen trusted data
validace objektu po deserializaci
DOS - obrana (5)
validace vstupu
omezeni pristupu ke kritickym zdrojum
vzdy po sobe uzavirej zdroje
timeouty dlouheho zpracovani
sledovani velikosti filu/streamu/narustu pozadavku
Povoleni spusteni souboru file.exe, programaticcally - vytvor a checkni
FilePermission fp = new FilePermission(“file.exe”, “execute”);
try { AccessController.checkPermission(fp); } catch (AccessControlPermission e) { … }
Metoda pro numericke plus, ktere je citlive na preteceni
Jak se pozna preteceni?
Math.addExact(Integer.MAX, 1)
vyhodi se ArtithmeticException
Jak poznam, ze cislo je po operaci konecne?
Math.isInfinite(1/Integer.MAX)
Check, jestli je Double fakt ok po parsovani
!Math.isNaN(myDouble)
Osetreni, jestli Wrapper obsahuje cislo, tedy neni null
Optional<Double> o = ...
if (o.isPresent) Double d = o.get();</Double>
SQL injection - osetreni, pokud nemuzu pouzit prepared statement nebo prepared callable
enquoteLiteral(param)
Scramble dat - hexadecimalni vystup - syntax pro SHA-256
MessageDigest md = MessageDigest.getInstance(“SHA-256”);
byte[] digest = md.digest(message.getBytes);
BigInteger hash = BigInteger.valueOf(digest).toString(16);