Save your Drupal logs!
At OWASP AppSec DC 09 a couple weeks ago my friend and colleague Greg Knaddison was telling me about a session he attended where the idea of reporting by follow-up important transaction data to the author was expressed. For example, if you transfer funds on your bank's website the bank follows up with an email stating that funds were transfered. If someone other than you had somehow gotten access to your bank account online and transfered themselves money you would be informed soon after to report the malicious activity.
This got me thinking about Drupal's logging system, watchdog. By default, and for ease of use, logs are stored in the database. The watchdog
table is kept trim and old log entries are removed during Drupal's cron system run. These logs are an important source of transaction data and on some sites it can be beneficial to keep them around longer than Drupal's default. Rather than expanding the table I thought up a module that could write old entries to disk for other backup means.
What follows is some proof-of-concept code (that works) for saving old watchdog entries to disk. It writes to the files directory in the file dblog/watchdog.log
. While the directory dblog
is protected with an htaccess file it is not recommended that log data be left under the web root.
<span style="color: #000000"><span style="color: #0000BB"><?php<p></p></span><span style="color: #FF8000">/**<br> * Implementation of hook_cron().<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB">dblog_save_cron</span><span style="color: #007700">() {<br> </span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br> </span><span style="color: #0000BB">$levels </span><span style="color: #007700">= </span><span style="color: #0000BB">watchdog_severity_levels</span><span style="color: #007700">();<br> </span><span style="color: #0000BB">$max </span><span style="color: #007700">= </span><span style="color: #0000BB">db_result</span><span style="color: #007700">(</span><span style="color: #0000BB">db_query</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT MAX(wid) FROM {watchdog}'</span><span style="color: #007700">));<br> </span><span style="color: #0000BB">$results </span><span style="color: #007700">= </span><span style="color: #0000BB">db_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT uid, type, message, variables, severity, link, location, referer, hostname, timestamp FROM {watchdog} WHERE wid <= %d"</span><span style="color: #007700">, </span><span style="color: #0000BB">$max </span><span style="color: #007700">- </span><span style="color: #0000BB">variable_get</span><span style="color: #007700">(</span><span style="color: #DD0000">'dblog_row_limit'</span><span style="color: #007700">, </span><span style="color: #0000BB">1000</span><span style="color: #007700">));<br> while (</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">db_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$results</span><span style="color: #007700">)) {<br> </span><span style="color: #0000BB">$data </span><span style="color: #007700">.= </span><span style="color: #0000BB">dblog_save_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">, </span><span style="color: #0000BB">$levels</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br> }<br> if (</span><span style="color: #0000BB">$data </span><span style="color: #007700">!= </span><span style="color: #DD0000">''</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">dblog_save_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br> }<br>}<p>function </p></span><span style="color: #0000BB">dblog_save_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">'watchdog.log'</span><span style="color: #007700">;<br> </span><span style="color: #0000BB">$filepath </span><span style="color: #007700">= </span><span style="color: #DD0000">'dblog'</span><span style="color: #007700">;<br> </span><span style="color: #0000BB">$filepath </span><span style="color: #007700">= </span><span style="color: #0000BB">file_create_path</span><span style="color: #007700">(</span><span style="color: #0000BB">$filepath</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">file_check_directory</span><span style="color: #007700">(</span><span style="color: #0000BB">$filepath</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_CREATE_DIRECTORY</span><span style="color: #007700">);<br> if (!</span><span style="color: #0000BB">is_file</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$filepath</span><span style="color: #DD0000">/.htaccess"</span><span style="color: #007700">)) {<br> </span><span style="color: #0000BB">$htaccess_lines </span><span style="color: #007700">= </span><span style="color: #DD0000">"Order allow,deny\ndeny from all"</span><span style="color: #007700">;<br> if ((</span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$filepath</span><span style="color: #DD0000">/.htaccess"</span><span style="color: #007700">, </span><span style="color: #DD0000">'w'</span><span style="color: #007700">)) && </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$htaccess_lines</span><span style="color: #007700">)) {<br> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">chmod</span><span style="color: #007700">(</span><span style="color: #0000BB">$filepath </span><span style="color: #007700">.</span><span style="color: #DD0000">'/.htaccess'</span><span style="color: #007700">, </span><span style="color: #0000BB">0664</span><span style="color: #007700">);<br> }<br> }<br> </span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filepath </span><span style="color: #007700">.</span><span style="color: #DD0000">'/' </span><span style="color: #007700">. </span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">'a'</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br>}<p>function </p></span><span style="color: #0000BB">dblog_save_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$entry</span><span style="color: #007700">, </span><span style="color: #0000BB">$levels</span><span style="color: #007700">) {<br> global </span><span style="color: #0000BB">$base_url</span><span style="color: #007700">;<br> </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'variables'</span><span style="color: #007700">] = </span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'variables'</span><span style="color: #007700">]);<br> <br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">= </span><span style="color: #0000BB">$base_url</span><span style="color: #007700">;<br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'|'</span><span style="color: #007700">. </span><span style="color: #0000BB">$levels</span><span style="color: #007700">[</span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'severity'</span><span style="color: #007700">]];<br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'|'</span><span style="color: #007700">. </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'timestamp'</span><span style="color: #007700">];<br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'|'</span><span style="color: #007700">. </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'type'</span><span style="color: #007700">];<br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'|'</span><span style="color: #007700">. </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'hostname'</span><span style="color: #007700">];<br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'|'</span><span style="color: #007700">. </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'location'</span><span style="color: #007700">];<br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'|'</span><span style="color: #007700">. </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'referer'</span><span style="color: #007700">];<br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'|'</span><span style="color: #007700">. </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'uid'</span><span style="color: #007700">];<br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'|'</span><span style="color: #007700">. </span><span style="color: #0000BB">strip_tags</span><span style="color: #007700">(</span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'link'</span><span style="color: #007700">]);<br> </span><span style="color: #0000BB">$message </span><span style="color: #007700">.= </span><span style="color: #DD0000">'|'</span><span style="color: #007700">. </span><span style="color: #0000BB">strip_tags</span><span style="color: #007700">(</span><span style="color: #0000BB">is_null</span><span style="color: #007700">(</span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'variables'</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">] : </span><span style="color: #0000BB">strtr</span><span style="color: #007700">(</span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'message'</span><span style="color: #007700">], </span><span style="color: #0000BB">$entry</span><span style="color: #007700">[</span><span style="color: #DD0000">'variables'</span><span style="color: #007700">]));<p> return </p></span><span style="color: #0000BB">$message</span><span style="color: #007700">;<br>}<br></span><span style="color: #0000BB">?></span></span>
Tags: DrupalPlanetDrupal