3048f62fff991cd7ccc258cffbc691434ee6000e
bernd input-filtering

bernd authored 16 years ago

1) <?php
2) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

3) require_once('inc/error.php');
4) 
bernd input-filtering

bernd authored 16 years ago

5) 
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

6) function strong_password($password)
7) {
bernd Einige Dummheiten repariert...

bernd authored 14 years ago

8)   if (config('use_cracklib') === NULL or config('use_cracklib') === false) {
bernd cracklib ausschaltbar machen

bernd authored 16 years ago

9)     DEBUG('Cracklib deaktiviert');
10)     return true;
11)   }
bernd Einige Dummheiten repariert...

bernd authored 14 years ago

12)   DEBUG("Öffne Wörterbuch: ".config('cracklib_dict'));
bernd Mehr config-optionen und co...

bernd authored 14 years ago

13)   if (! ($dict = crack_opendict(config('cracklib_dict'))))
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

14)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

15)     logger(LOG_ERR, "inc/security", "cracklib", "could not open cracklib-dictionary »".config('cracklib_dict')."«");
bernd Einige Dummheiten repariert...

bernd authored 14 years ago

16)     system_failure("Kann Crack-Lib-Wörterbuch nicht öffnen: ".config('cracklib_dict'));
bernd * Passwörter mit cracklib p...

bernd authored 16 years ago

17)   }
18)   // Führe eine Überprüfung des Passworts durch
19)   $check = crack_check($dict, $password);
20) 
21)   $message = crack_getlastmessage();
22)   crack_closedict($dict);
23) 
24)   if ($check === True)
25)   {
26)     DEBUG("Passwort ok");
27)     return true;
28)   }
29)   else
30)   {
31)     DEBUG("Passwort nicht ok: {$message}");
32)     return $message;
33)   }
34) }
35) 
36) 
bernd input-filtering

bernd authored 16 years ago

37) function filter_input_general( $input )
38) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

39)   return htmlspecialchars(iconv('UTF-8', 'UTF-8', $input), ENT_QUOTES, 'UTF-8');
40) }
41) 
42) 
43) function verify_input_general( $input )
44) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

45)   if (filter_input_general($input) != $input) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

46)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Logger mit Logleveln

bernd authored 14 years ago

47)     logger(LOG_WARNING, 'inc/security', 'verify_input_general', 'Ungültige Daten: '.$input);
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

48)   }
bernd input-filtering

bernd authored 16 years ago

49) }
50) 
51) 
52) function filter_input_username( $input )
53) {
hanno Hatte die Kompatibilität ge...

hanno authored 16 years ago

54)   return ereg_replace("[^[:alnum:]\_\.\+\-]", "", $input );
55) }
56) 
57) function verify_input_username( $input )
58) {
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

59)   if (filter_input_username( $input ) != $input) {
bernd Logger mit Logleveln

bernd authored 14 years ago

60)     logger(LOG_WARNING, 'inc/security', 'verify_input_username', 'Ungültige Daten: '.$input);
bernd Add IP-address patterns

bernd authored 15 years ago

61)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Loggen, wenn ungültige Zeic...

bernd authored 16 years ago

62)   }
bernd input-filtering

bernd authored 16 years ago

63) }
64) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

65) 
66) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

67) function filter_input_hostname( $input, $wildcard=false )
bernd check auf hostname

bernd authored 16 years ago

68) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

69)   // FIXME: Eine "filter"-Funktion sollte keinen system_failure verursachen sondern einfach einen bereinigten String liefern.
70)   
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

71)   $input = str_replace(array('Ä', 'Ö', 'Ü'), array('ä', 'ö', 'ü'), strtolower($input));
bernd Hostnames sollten nicht mit...

bernd authored 16 years ago

72)   $input = rtrim($input, "\t\n\r\x00 .");
73)   $input = ltrim($input, "\t\n\r\x00 .");
bernd Erlaube * im Hostname

bernd authored 14 years ago

74)   if (ereg_replace("[^[:alnum:]äöü*\.\-]", "", $input ) != $input)
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

75)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Erlaube * im Hostname

bernd authored 14 years ago

76)   if (! $wildcard && ereg_replace("\*", "", $input ) != $input)
77)     system_failure("Ihre Daten enthielten ungültige Zeichen (Keine Wildcards erlaubt)!");
bernd Hostname darf kein »..« ent...

bernd authored 16 years ago

78)   if (strstr($input, '..'))
79)     system_failure("Ungültiger Hostname");
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

80)   return $input;
bernd check auf hostname

bernd authored 16 years ago

81) }
82) 
bernd Erlaube * im Hostname

bernd authored 14 years ago

83) function verify_input_hostname( $input, $wildcard=false )
bernd Add IP-address patterns

bernd authored 15 years ago

84) {
bernd Erlaube * im Hostname

bernd authored 14 years ago

85)   if (filter_input_hostname( $input, $wildcard ) != $input) {
bernd Logger mit Logleveln

bernd authored 14 years ago

86)     logger(LOG_WARNING, 'inc/security', 'verify_input_hostname', 'Ungültige Daten: '.$input);
bernd Add IP-address patterns

bernd authored 15 years ago

87)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
88)   }
89) }
90) 
91) 
92) function verify_input_ipv4( $input )
93) {
94)   if (! preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $input))
95)     system_failure('Keine IP-Adresse');
96) }
97) 
98) 
99) function verify_input_ipv6( $input )
100) {
101)   // ripped from Perl module Net-IPv6Addr v0.2
102)   if (! preg_match("/^(([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}|[0-9a-f]{0,4}::|:(?::[a-f0-9]{1,4}){1,6}|(?:[a-f0-9]{1,4}:){1,6}:|(?:[a-f0-9]{1,4}:)(?::[a-f0-9]{1,4}){1,6}|(?:[a-f0-9]{1,4}:){2}(?::[a-f0-9]{1,4}){1,5}|(?:[a-f0-9]{1,4}:){3}(?::[a-f0-9]{1,4}){1,4}|(?:[a-f0-9]{1,4}:){4}(?::[a-f0-9]{1,4}){1,3}|(?:[a-f0-9]{1,4}:){5}(?::[a-f0-9]{1,4}){1,2}|(?:[a-f0-9]{1,4}:){6}(?::[a-f0-9]{1,4}))$/i", $input))
103)     system_failure("Ungültige IPv6-Adresse");
104) }
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

105) 
106) 
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

107) function filter_quotes( $input )
108) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

109)   return ereg_replace('["\'`]', '', $input );
bernd Im Passwort dürfen auch kei...

bernd authored 16 years ago

110) }
111) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

112) 
113) 
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

114) function filter_shell( $input )
115) {
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

116)   return ereg_replace('[^-[:alnum:]\_\.\+ßäöüÄÖÜ/%§=]', '', $input );
117) }
118) 
119) function verify_shell( $input )
120) {
121)   if (filter_shell($input) != $input)
122)     system_failure("Ihre Daten enthielten ungültige Zeichen!");
bernd Diverse shell-kritische zei...

bernd authored 16 years ago

123) }
bernd input-filtering

bernd authored 16 years ago

124) 
bernd Umfangreiche Code-Aufräumar...

bernd authored 16 years ago

125) 
126) 
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

127) function check_path( $input )
128) {
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

129)   DEBUG("checking {$input} for valid path name");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

130)   if ($input != filter_input_general($input))
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

131)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

132)     logger(LOG_WARNING, 'inc/security', 'check_path', 'HTML-Krams im Pfad: '.$input);
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

133)     DEBUG("HTML-Krams im Pfad");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

134)     return False;
bernd Auch @ darf im Mailbox-Pfad...

bernd authored 16 years ago

135)   }
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

136)   $components = explode("/", $input);
137)   foreach ($components AS $item)
138)   {
139)     if ($item == '..')
140)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

141)       logger(LOG_WARNING, 'inc/security', 'check_path', '»..« im Pfad: '.$input);
bernd check auf hostname

bernd authored 16 years ago

142)       DEBUG("»..« im Pfad");
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

143)       return False;
144)     }
145)   }
bernd Auch Großbuchstaben sind im...

bernd authored 16 years ago

146)   return (preg_match('/^[A-Za-z0-9.@\/_-]*$/',$input) == 1);
bernd XSS/CSRF-Bugs behoben

bernd authored 16 years ago

147) }
148) 
149) 
bernd * alle internen Links sinnv...

bernd authored 15 years ago

150) function in_homedir($path)
151) {
152)   DEBUG("Prüfe »{$path}«");
153)   if (! check_path($path))
154)   {
155)     DEBUG('Kein Pfad');
156)     return False;
157)   }
158)   if (! isset($_SESSION['userinfo']['homedir']))
159)   {
160)     DEBUG("Kann homedir nicht ermitteln");
161)     return False;
162)   }
163)   return strncmp($_SESSION['userinfo']['homedir'], $path, count($_SESSION['userinfo']['homedir'])) == 0;
164) }
165) 
166) 
bernd Neues Modul für "Kunde werden"

bernd authored 16 years ago

167) function check_emailaddr( $input )
168) {
bernd Syntax-fehler

bernd authored 16 years ago

169)   return (bool) filter_var($input, FILTER_VALIDATE_EMAIL) == $input;
bernd don't be too complicated. /...

bernd authored 16 years ago

170) }
171) 
172) function check_domain( $input )
173) {
174)   return (bool) preg_match("/[a-z0-9\.\-]+\.[a-z]{2,4}$/i", $input );