PageRenderTime 76ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/config.php

http://webhoneypot.googlecode.com/
PHP | 380 lines | 222 code | 92 blank | 66 comment | 37 complexity | 049aadea5b16056c202c11712bdfb5a6 MD5 | raw file
  1. <?php
  2. /**
  3. * Project: DShield "Webhoneypot"
  4. * File name: install.php
  5. * Description: web honeypot install script.
  6. *
  7. * $Date$
  8. * $Id$
  9. * $Author$
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  18. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  19. * for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /**
  26. * Initialization function
  27. */
  28. function init() {
  29. /*
  30. * this script will only run on the command line. abort if it is not
  31. */
  32. if (php_sapi_name () !== "cli") {
  33. echo "This installer must be run from the command-line.";
  34. exit ( 1 );
  35. }
  36. /*
  37. * banner.
  38. */
  39. echo "Starting the Web honeypot command-line installer...\n";
  40. echo "\n";
  41. echo "The following tasks will be performed:\n";
  42. echo "* Detection of the PHP version\n";
  43. echo "* Detection of the curl version\n";
  44. echo "* Detection of a HTTP server\n";
  45. echo "* Detection of the OS\n";
  46. echo "* Identify the log directory for the HTTP server\n";
  47. echo "* Configure DShield user/pass for log submission\n";
  48. echo "* Configure a cron job for automatic template updates\n";
  49. echo "\n";
  50. echo "Please refer to the INSTALL file for more information.\n";
  51. echo "\n";
  52. echo " (please wait)...\n";
  53. sleep ( 3 );
  54. } # end init()
  55. /*
  56. * Check OS and pass control to the appropriate install function
  57. */
  58. function checkOS() {
  59. $website = "http://sites.google.com/site/webhoneypotsite/home";
  60. $os = strtolower ( PHP_OS );
  61. switch ($os) {
  62. case "linux" :
  63. echo "Preparing for a Linux installation...\n\n";
  64. install ( $os );
  65. break;
  66. case "winnt" :
  67. echo "Preparing for a Windows NT/2000/XP installation...\n\n";
  68. install ( $os );
  69. break;
  70. case "darwin" :
  71. echo "Preparing for an OS X/Darwin installation...\n\n";
  72. install ( $os );
  73. break;
  74. default :
  75. echo "Unknown OS ($os). Please check the requirements at $website or perform a manual installation. \n";
  76. exit ( 1 );
  77. }
  78. } # end checkOS()
  79. /*
  80. * Check the PHP version
  81. */
  82. function checkPHP() {
  83. #
  84. # Define the minimum version that can be used to run the installer.
  85. #
  86. # Reference: http://us.php.net/manual/en/features.commandline.php
  87. #
  88. #$phpMinVersion = "4.2.0";
  89. echo "Checking PHP version... ";
  90. $phpMinVersion = "5.0.0";
  91. $aVersionParts = explode ( '.', PHP_VERSION );
  92. $aMinVersionParts = explode ( '.', $phpMinVersion );
  93. $nMax = max ( array_merge ( $aVersionParts + $aMinVersionParts ) );
  94. $nLog = pow ( 10, ceil ( log10 ( $nMax ) ) );
  95. $nMinVersion = 0;
  96. $nVersion = 0;
  97. $nCount = count ( $aMinVersionParts );
  98. if (count ( $aVersionParts ) < $nCount) {
  99. for($i = count ( $aVersionParts ); $i <= $nCount; $i ++) {
  100. $aVersionParts [$i] = 0;
  101. }
  102. }
  103. for($i = 0; $i < $nCount; $i ++) {
  104. $nMinVersion += $aMinVersionParts [$i] * pow ( $nLog, $nCount - $i - 1 );
  105. $nVersion += $aVersionParts [$i] * pow ( $nLog, $nCount - $i - 1 );
  106. }
  107. if ($nMinVersion <= $nVersion) {
  108. echo "( " . PHP_VERSION . " > " . $phpMinVersion . ") ";
  109. echo "[ OK ]" . "\n";
  110. } else {
  111. echo "[ FAILED ]" . "\n";
  112. echo "PhP >= $phpMinVersion is required!\n";
  113. exit ( 1 );
  114. }
  115. } # end checkPHP()
  116. /*
  117. * check what web server we are running.
  118. */
  119. function checkWWW() {
  120. echo "Checking for a webserver... ";
  121. $host = '127.0.0.1';
  122. $port = '80';
  123. $request = "HEAD / HTTP/1.0\n\n";
  124. $socket = @fsockopen ( $host, $port );
  125. if (! $socket) {
  126. echo "[ FAILED ]" . "\n";
  127. echo "Could not create a connection to $host : $port!";
  128. exit ( 1 );
  129. } else {
  130. @fwrite ( $socket, $request );
  131. stream_set_timeout ( $socket, 10 );
  132. $reply = fread ( $socket, 400 );
  133. $wwwVersion = stream_get_meta_data ( $socket );
  134. @fclose ( $socket );
  135. if ($wwwVersion ['timed_out']) {
  136. echo "[ FAILED ]\n";
  137. echo "Error: connection lost!\n";
  138. } else {
  139. list ( $string1, $string2 ) = split ( "Server: ", $reply, 2 );
  140. list ( $wwwPlatform, $wwwPlatformVersion, $remainder ) = split ( "[/ \n\r]", $string2, 3 );
  141. }
  142. switch (strtolower ( $wwwPlatform )) {
  143. case NULL :
  144. echo "[ FAILED ]\n";
  145. echo "Webserver not found!\n";
  146. exit ( 1 );
  147. break;
  148. case "apache" :
  149. echo "( " . $wwwPlatform . " " . $wwwPlatformVersion . " )" . " [ OK ]\n";
  150. echo "Please note the following for $wwwPlatform:\n\n";
  151. echo "Add the following line to httpd.conf for a dedicated server:\n\n";
  152. echo "AliasMatch .* DOCUMENT_ROOT/index.php\n\n";
  153. echo "Please change DOCUMENT_ROOT to the appropriate directory.\n";
  154. echo "Note: this must be the first (or only) AliasMatch directive!\n\n";
  155. break;
  156. case "iis" :
  157. echo "( " . $wwwPlatform . " " . $wwwPlatformVersion . " )" . " [ OK ]\n";
  158. break;
  159. default :
  160. echo "Detected Possibly Unsupported: ( " . $wwwPlatform . " " . $wwwPlatformVersion . " )" . " [ OK ]\n";
  161. } // end switch
  162. }
  163. } # end checkWWW()
  164. /*
  165. * Check for the existance of curl
  166. */
  167. function checkCurl() {
  168. $curlVer = curl_version ();
  169. echo "Checking for curl... ";
  170. if (is_null ( $curlVer ) === TRUE) {
  171. echo "[ FAILED ]\n";
  172. echo "Curl not found!\n";
  173. exit ( 1 );
  174. } else {
  175. echo "( " . $curlVer ["version"] . " ) ";
  176. echo "[ OK ]\n";
  177. }
  178. } # end checkCurl()
  179. /*
  180. * Perform the installation tasks
  181. */
  182. function install($os) {
  183. $dirSep = DIRECTORY_SEPARATOR;
  184. $requiredDirs = array("docs", "etc", "logs", "lib", "templates", "update");
  185. while ( true ) {
  186. echo "Please enter the full path to DOCUMENT_ROOT\n";
  187. echo "Press enter to use '" . realpath ( dirname ( __FILE__ ) . $dirSep . '..' . $dirSep . 'html' ) . "'\n";
  188. $docRoot = fgets ( STDIN );
  189. $docRoot = trim ( $docRoot );
  190. if ($docRoot == '') {
  191. $docRoot = dirname ( __FILE__ ) . $dirSep . '..' . $dirSep . 'html' ;
  192. }
  193. $docRoot = realpath ( $docRoot );
  194. clearstatcache ();
  195. # We need to check:
  196. # - does the directory exist
  197. # - is it actually a directory and not a file / symlink
  198. # - is the directory writable
  199. if (! file_exists ( $docRoot )) {
  200. echo "Directory $docRoot does not exist\n\n";
  201. continue;
  202. }
  203. if (! is_dir ( $docRoot )) {
  204. echo "$docRoot is not a valid directory\n\n";
  205. continue;
  206. }
  207. foreach($requiredDirs as $idx => $dir) {
  208. $path = $docRoot . $dirSep . ".." . $dirSep . $dir;
  209. if (! is_dir ($path)) {
  210. echo "$path does not appear to be a valid directory\n\n";
  211. continue;
  212. }
  213. if (! is_readable ($path)) {
  214. echo "I am not able to read the $path directory\n\n";
  215. continue;
  216. }
  217. if (! is_writable ($path)) {
  218. echo "I am not able to write to the $path directory\n\n";
  219. continue;
  220. }
  221. } # end foreach
  222. break;
  223. }
  224. echo "DOCUMENT_ROOT set to $docRoot\n";
  225. echo "setting permissions of logs directory to 1777\n";
  226. chmod($docRoot.$dirSep."..".$dirSep."logs", 01777 );
  227. confDShield ( $os, $docRoot );
  228. confAutoUpdates ( $os, $docRoot );
  229. } # end install()
  230. /*
  231. * Setup DShield log submission
  232. */
  233. function confDShield($os, $docRoot) {
  234. $website = "https://secure.dshield.org/register.html";
  235. $dshieldConf = "config.local";
  236. $dirSep = DIRECTORY_SEPARATOR;
  237. echo "Configuring DShield (OS: $os)...\n";
  238. echo "\n";
  239. echo "If you do not have a DShield login, please register now at $website\n";
  240. echo "\n";
  241. echo "Please enter your DShield user id: ";
  242. $userid = rtrim ( fgets ( STDIN ) );
  243. echo "Please enter your password: ";
  244. $password = sha1 ( rtrim ( fgets ( STDIN ) ) . $userid );
  245. $dshieldConfPath = $docRoot . $dirSep . ".." . $dirSep . "etc" . $dirSep . $dshieldConf;
  246. echo "Writing configuration file to $dshieldConfPath ...\n";
  247. if (! $fileHandle = @fopen ( $dshieldConfPath, "wt" )) {
  248. echo "Failed to open or create $dshieldConfPath!\n";
  249. exit ( 1 );
  250. } else {
  251. fwrite ( $fileHandle, "[config]\n" . "userid=$userid\n" . "hashpassword=$password\n" );
  252. }
  253. @fclose ( $fileHandle );
  254. } # end confDShield()
  255. /*
  256. * Setup automatic template updates
  257. */
  258. function confAutoUpdates($os, $docRoot) {
  259. $dirSep = DIRECTORY_SEPARATOR;
  260. switch ($os) {
  261. case "linux" :
  262. echo "Please add the following two lines to your crontab to enable template updates:\n";
  263. echo "\n";
  264. $r1=rand(0,23);
  265. $r2=rand(0,59);
  266. echo "$r2 $r1 * * * ".realpath($docRoot . $dirSep . "..".$dirSep."update" . $dirSep . "update-templates.php")." > /dev/null\n";
  267. echo "\n";
  268. $r1=rand(0,23);
  269. $r2=rand(0,59);
  270. echo "$r2 $r1 * * * ".realpath($docRoot . $dirSep . "..".$dirSep."update" . $dirSep . "update-client.php")." > /dev/null\n";
  271. echo "If you need help, please see 'man cron' for more information.\n";
  272. echo "\n";
  273. echo "\n";
  274. echo "It is highly recommended that you first run these two commands right now to make sure they work. \n";
  275. break;
  276. case "winnt" :
  277. #
  278. # winnt update code goes here
  279. #
  280. break;
  281. }
  282. } # end confAutoUpdates()
  283. init ();
  284. checkPHP ();
  285. checkCurl ();
  286. checkWWW ();
  287. checkOS ();
  288. echo "Web honeypot installation completed.\n";