Author Topic: Clearing /var/log  (Read 6106 times)

0 Members and 1 Guest are viewing this topic.

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Clearing /var/log
« on: March 05, 2013, 10:07:44 pm »
For security's sake, I was looking into removing some logs at shutdown. Namely I was looking at the logs in /var/log.

Does anyone have any input on this? I want to make sure this won't damage anything if I erase all logs every shutdown.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Clearing /var/log
« Reply #1 on: March 05, 2013, 10:10:35 pm »
Pretty sure you will be fine, just make sure files that are supposed to be there, empty or not, are there and that their permissions/owners are correct.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: Clearing /var/log
« Reply #2 on: March 05, 2013, 10:15:02 pm »
I kinda thought so. Thanks for confirming  :D
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline wookie

  • Peasant
  • *
  • Posts: 68
  • Cookies: -4
    • View Profile
Re: Clearing /var/log
« Reply #3 on: March 05, 2013, 10:49:44 pm »
An alternative would be truncating them all?


Try something like...


Code: [Select]
cd /var/log
echo > $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2)


That should truncate the files in /var/log that aren't gzip compressed and have the term "log" in the filename.  You may want to do some extra validation to make sure it's a file or a folder.


You could also look at doing some form of log rotation on a daily basis and either compress your log files or move them off disk?

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: Clearing /var/log
« Reply #4 on: March 05, 2013, 10:59:00 pm »
An alternative would be truncating them all?


Try something like...


Code: [Select]
cd /var/log
echo > $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2)


That should truncate the files in /var/log that aren't gzip compressed and have the term "log" in the filename.  You may want to do some extra validation to make sure it's a file or a folder.


You could also look at doing some form of log rotation on a daily basis and either compress your log files or move them off disk?

That didn't work.

Code: [Select]
bash: $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2): ambiguous redirect
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline wookie

  • Peasant
  • *
  • Posts: 68
  • Cookies: -4
    • View Profile
Re: Clearing /var/log
« Reply #5 on: March 05, 2013, 11:08:39 pm »
That didn't work.

Code: [Select]
bash: $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2): ambiguous redirect


Sorry, should've tested it.


Try this in a bash script:


Code: [Select]

for file in $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2)
do
        echo > $file
done

Offline wookie

  • Peasant
  • *
  • Posts: 68
  • Cookies: -4
    • View Profile
Re: Clearing /var/log
« Reply #6 on: March 05, 2013, 11:13:32 pm »

Code: [Select]

for file in $(ls -l /var/log | grep -v ".gz" | grep "log" | cut -d ':' -f 2 | cut -d ' ' -f 2)
do
        echo > $file
done


You could wrap the echo > $file in something like this;


Code: [Select]

if [ -f $file ]
then
echo > $file
fi
[size=78%]


Which should validate that it isn't a system file or directory judging by http://tldp.org/LDP/abs/html/fto.html[/size]

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: Clearing /var/log
« Reply #7 on: March 05, 2013, 11:22:39 pm »
Well I found a pretty simple solution given that no log files in /var/log are not to be truncated. Simply:

cat /dev/null > whatever.log

Now to do it to the whole directory recursively...

EDIT: Wow, I have the perfect solution that I've tested and have no problems with.

Code: [Select]
find /var/log -type f exec sh -c '> "{}"' \;

Works like a charm.
« Last Edit: March 05, 2013, 11:33:15 pm by lucid »
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline wookie

  • Peasant
  • *
  • Posts: 68
  • Cookies: -4
    • View Profile
Re: Clearing /var/log
« Reply #8 on: March 05, 2013, 11:37:07 pm »
Well I found a pretty simple solution given that no log files in /var/log are not to be truncated. Simply:

cat /dev/null > whatever.log

Now to do it to the whole directory recursively...


Don't really understand what that does that what I've posted doesn't do?  I assumed you wanted to remove all logs, not just a specific log.


Regardless, both methods will truncate files.


Bash isn't really my strong point, but you could use PHP?


You could use opendir and readdir methods but you could also do something like this, which would allow you to recurse quite easily and parse it into a nice array for you to do some truncating with:


Code: [Select]

<?php


$_dir 
"/var/log";
$cmd "ls -lR $_dir";
$files=$out1=$out2=null;
exec($cmd$out1$out2);
foreach(
$out1 as $k=>$line){
        if(
substr($line08) == '/var/log'){
                
$total $out1[$k 1];
                
$total explode(" "trim($total));
                if(
$total[1] < 1) continue;
                
$dir substr($line8);
                
$dir substr($dir0, -1);
        }
        if(
substr($line08) != '/var/log' && substr($line05) != 'total'){
                
$file explode(":"$line);
                
$file explode(" "$file[1]);
                
$file $file[1];
                
$files[$dir][]="$_dir$dir$file";
        }
}


print_r($files);


?>


Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: Clearing /var/log
« Reply #9 on: March 06, 2013, 12:32:25 am »
I guess the two bash scripts do the same thing, one is just simpler. I like the PHP idea.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python

Offline proxx

  • Avatarception
  • Global Moderator
  • Titan
  • *
  • Posts: 2803
  • Cookies: 256
  • ФФФ
    • View Profile
Re: Clearing /var/log
« Reply #10 on: March 06, 2013, 09:47:07 am »
Just wanted to state that actually deleting certain log files can cause problems.
And for security sake you could better overwrite them with garbage than deleting them.
Thats is if your paranoid.
Wtf where you thinking with that signature? - Phage.
This was another little experiment *evillaughter - Proxx.
Evilception... - Phage

Offline lucid

  • #Underground
  • Titan
  • **
  • Posts: 2683
  • Cookies: 243
  • psychonaut
    • View Profile
Re: Clearing /var/log
« Reply #11 on: March 06, 2013, 05:43:51 pm »
I'm not deleting any of them. I suppose I could look into overwriting them. Perhaps something like that could be done with the shred command, without actually deleting the files that is.
"Hacking is at least as much about ideas as about computers and technology. We use our skills to open doors that should never have been shut. We open these doors not only for our own benefit but for the benefit of others, too." - Brian the Hacker

Quote
15:04  @Phage : I'm bored of Python