16

I always thought the greatest benefit of the logs is to confirm to you that your machine has been hacked. However I see hackers bragging about "rooting" servers all over the Internet. Whats stopping a hacker with root access from deleting the logs in order to cover their tracks?

Ulkoma
  • 8,793
  • 16
  • 65
  • 95
  • 46
    Why you worry about logs, root can delete everything. Some account must have full control at some time in order to manage the system. That's why syslog to remote system is important if you can't trust root. – Fiasco Labs Nov 10 '15 at 18:36
  • 2
    @FiascoLabs A smart attacker will be interested in espionage more than sabotage – Ulkoma Nov 10 '15 at 18:37
  • 18
    *Why is it possible for the root user to delete the logs?* - how do you propose making it *impossible* for root to delete logs? – TessellatingHeckler Nov 10 '15 at 18:43
  • @TessellatingHeckler I am not sure, it's that hard to implement? maybe something like [this](http://security.stackexchange.com/a/65425/31356)? – Ulkoma Nov 10 '15 at 18:56
  • 12
    root types `setenforce 0` to disable SELinux and then deletes the logs. Now what? It is very hard to implement - if you make another user to delete logs, root can `su` to that user. If you have a part of the system that root cannot get to, how can it be managed or configured? Then you just move the hacker from trying to "gain root access" to "trying to gain 'secure area' access" instead. – TessellatingHeckler Nov 10 '15 at 19:03
  • 11
    if you are doing logrotate, some process needs permission to delete logs – schroeder Nov 10 '15 at 19:16
  • 2
    @TessellatingHeckler write your logs to some kind of write-once media? – Michael Nov 10 '15 at 19:40
  • @schroeder: If logs are stored in a box outside the computer with sufficient storage, one could have "hardware" [or firmware in another box which cannot be changed without physical access to it] designed so that newer log entries will eventually overwrite old ones, but that would take some minimum length of time to occur. Further, at the risk of facilitating some denial-of-service attacks one could have the remote system sound an alarm if certain kinds of logs are getting added too quickly and throttle the actions that would require such logging. – supercat Nov 10 '15 at 19:48
  • 3
    @supercat once you export the logs, you can treat the incoming logs however you'd like - but it's on a **different system**. The local root user needs to be able to delete local logs because some process needs to be able to. – schroeder Nov 10 '15 at 20:04
  • 1
    @Michael Yeah that would do it - but I suggest that that is not meaningfully different from the answers saying to log to a remote syslog server; sending the log data out over a one-way interface saves it. – TessellatingHeckler Nov 10 '15 at 20:09
  • 9
    It's possible because servers are not considered disposable hardware. I've got a server that's been running for 11 years so far, and in that time it's generated log files many times the size of its hard drive. If logs were impossible to delete, I would have needed to retire the server after less than a year. – Mark Nov 10 '15 at 21:31
  • Because Skynet is not a self-sustaining entity, yet... – MonkeyZeus Nov 10 '15 at 21:33
  • @TessellatingHeckler I think that's disingenuous. You certainly can lock down root/superuser access in SELinux. You can't make it bullet proof, because any user with physical access to the machine can disable SELinux at bootup, but you 100% _can_ make it so that root can't issue `setenforce` commands. Most 'real' SELinux setups I've seen have two admins - the SELinux user 'SELinux Admin' (low discretionary access), and root (high discretionary access, but no access to modifying SELinux). It might not be bulletproof against your sysadmins, but it helps against hackers getting root. In fact... – Shaz Nov 11 '15 at 14:23
  • I would argue any SELinux implementation where root has the ability to modify SELinux is a wrong implementation. Because any hacker who _does_ gain root automatically gains everything. – Shaz Nov 11 '15 at 14:25
  • @Ryan: Is `root` able to load kernel modules? Access `/dev/mem` or `/dev/kmem`? Access block devices where the log files are stored? It's very hard to set up rules that successfully constraint `root` – Ben Voigt Nov 11 '15 at 14:52
  • 1
    @BenVoigt `root` would be the same as always on the discretionary access side of things, but the mandatory access assigned by SELinux would cripple root from doing pretty much anything until SELinux is disabled, and in this case it could only be disabled by a different person using a unique 'SELinux Admin' account. *I should add, I've seen implementations where it's impossible to login as root while SELinux is enabled. – Shaz Nov 11 '15 at 14:59
  • @Ryan Then this question becomes *"why is it possible for ___ to delete log files?"* where ___ is any powerful account or combination of accounts, no? It's like a bank putting the "Toilets" sign on the vault, and the "Vault" sign on the toilets, and saying "now criminals gaining access to the 'vault' can no longer take any valuables" - they'll go for the 'toilets' instead. If some part of the software has power, it becomes the part that hackers will target, regardless of whether it's named `root`. SELinux makes it harder to crack - great - but an attacker who does so could still delete logs..? – TessellatingHeckler Nov 11 '15 at 17:12
  • @TessellatingHeckler At the end of the day, yes - if a hacker gains some access X to your system, they can do bad things. There's no way to refute that, because there's no such thing as perfect, infallible security. But that's not my point. My point is - when things are working properly - root doesn't _have_ to have access to everything, or anything. I'm just going back to your question "how do you propose making it impossible for root to delete logs?" The solution: restrict the superuser in SELinux and make it so the superuser can't modify SELinux. That's all I'm saying. – Shaz Nov 11 '15 at 17:22
  • Q's like this make me wish there were some moderator screening before Q's are promoted to 'hot'. Users here are taking the bait and expounding on a variety of different topics without pointing out that two questions are asked here. I think @Mark has the best answer that takes the question literally. There seems to be too little focus on the fact that a log is a file, so which question does OP mean to ask, 'Is it possible to make files that root cannot delete/modify?' or something else? I need to remind myself that 'hot' does not make a question and its related discussion 'informative'. – user1445967 Nov 11 '15 at 23:40
  • @user1445967, promotion to "hot" is entirely automated, based on computer analysis of the activity level of the question. – Mark Nov 11 '15 at 23:42
  • @Mark thanks, full disclosure and honesty I realized my comment could be worded more effectively and edited it. – user1445967 Nov 11 '15 at 23:43
  • Most system log daemon is capable to send the log files to the net to a centralized log storage. It is very useful exactly for similar dangers as yours. – peterh Nov 12 '15 at 00:32
  • @Ryan So you can use SELinux to prevent root doing things... then what if root disables SELinux? Or maybe you stop root from being able to disable SELinux, in which case *nobody* can disable SELinux, and if you want to stop using SELinux you have to reformat the server. Wait a minute though - reformatting deletes the logs! Therefore you make it so nobody can reformat the server, and you have to throw it out and buy a new one instead. – user253751 Nov 12 '15 at 08:08
  • GRsecurity for Linux might be of interest to you... it basically creates a permission level above that of root which enables you to mitigate this particular scenario, amongst many other things. – Olipro Nov 12 '15 at 08:44
  • Also, this is why it's SO important to protect root on public-facing servers. Ours are set up to deny remote root login. Then all SSH users with sudo access are required to have CRAZY long passwords (40+ random characters) and use key exchange for login. – Rick Chatham Nov 12 '15 at 19:48

8 Answers8

40

Why is it possible for the root user to delete the logs?

Because you need to be able to manage the server.

Whats stopping a hacker with root access from deleting the logs in order to cover their tracks?

Regarding local logging, only their own stupidity. Regarding external logging, their skill set and yours.

There are many, many different ways to "log." In fact, you could have the rooted machine log to a separate, hidden device that can't easily be overwritten, such as a raspberry pi / arduino logger, of which I have a few.

In Linux, root is supposed to be able to do pretty much everything. In Windows, the same concept applies; if you have root access to a machine, you will be able to do almost anything you want to that machine, which includes deleting logs.

You can also log all connections to a certain machine, especially if it isn't supposed to be "accessed" in the first place.

How would you find out what's going on, and how would you circumvent it?

Here's a way to find all changes in the last 120 minutes:

find / -mmin -120 -printf '%p\t%a\n'

Nice, huh? Gotcha, hacker! Mwahahaha... but you can change these attributes if you know what you're doing, like so:

touch -d "24 hours ago" <file>

Even worse, automating it:

find / -print | while read file; do
    touch -d "$(date -r "$file") - 24 hours" "$file"
done

But wait, Mark Hulkalo! If all the files were last edited 24 hours ago, then that clearly shows we've got a haxor! True, but you can also apply $RANDOM where 24 exists. Here's an example of a number from 1-24: $((RANDOM % 10) + 1). Now it doesn't look so easy to discover, does it?

But what if all files look like that, especially files which aren't supposed to be changed? Limit the scope to a certain directory, such as /root. There are lots of ways to mask your presence locally.

Unless the attacker forgot to cleanse ~/.bash_history, you'll be flying blind. Really, if the only "logging" you have is ~/.bash_history, then you're in trouble if your attacker is at least moderately intelligent. That can be erased on each user account with astounding ease, so as long as you have the appropriate permissions.

It's much easier to log entries somewhere it won't be easily detected or modified, such as an external device. While it's possible to use forensics to recover your attacker's footprints, it's very, very easy to circumvent if you know what you're doing. This is why external logging is a much better solution.

And that's the story of why external logging is generally the answer... keep in mind there are also ways to circumvent external logging. Nothing is ever 100% safe; you can only make it harder for your attacker, not impossible.

Doorknob
  • 103
  • 5
Mark Buffalo
  • 22,498
  • 8
  • 74
  • 91
  • but how do you erase from the base_history the command you used to erase bash_history? – njzk2 Nov 11 '15 at 04:17
  • 2
    @njzk2 history -c doesn't leave itself. or just delete .bash_history. or copy it then do your thing then replace it with the original. or just don't erase it; your other tracks are still covered. – Jason C Nov 11 '15 at 04:23
  • `cat /dev/null > ~/.bash_history && history -c && exit` – Mark Buffalo Nov 11 '15 at 04:24
  • 4
    Or you can use `export HISTFILE=/dev/null` before you start typing your commands. – Marco Leogrande Nov 11 '15 at 07:37
  • @MarcoLeogrande, Nice suggestion. Or perhaps add `HISTCONTROL=ignorespace` to `~/.bash_rc`, then execute all commands with a space prefix. Though that could be detectable by 1) detecting file changes to those files in particular, and 2) finding that particular property in the bashrc file. – Mark Buffalo Nov 11 '15 at 14:26
  • 2
    (in bash at least) putting a space before a command hides it from the bash history. Bash history is not intended as a security feature. – AMADANON Inc. Nov 11 '15 at 20:37
  • @AMADANONInc., but doesn't that space prefixing only hold true if you set `HISTCONTROL=ignorespace`? – Mark Buffalo Nov 11 '15 at 20:43
  • 1
    The default on Debian is HISTCONTROL=ignoreboth. There are other ways to hide things from bash history - e.g ssh target command-to-run. No matter what you do, it is still not a security feature, and should not be depended on. – AMADANON Inc. Nov 12 '15 at 00:55
  • @njzk2 bash history (and shell history generally) is trivial to beat. Most shells only write the history back to a file on exit. If you do a 'ard kill' of the shell, such as kill -9, it will exit without writing the history file. For a more secure history, you need something like kernel level process accounting, but that can dome at a performance cost – Tim X Nov 12 '15 at 21:16
16

Why is it possible for the root user to delete the logs?

Because somebody must be able to make changes to the system, i.e. rotate log files, remove log files etc. In UNIX root is traditionally this fully privileged user. Actually there is not much of a difference between modifying a log and replacing a binary to omit logging - and root can do both by default.

Whats stopping a hacker with root access from deleting the logs in order to cover their tracks?

The standard configuration usually does not stop the attacker at all. One obvious way to improve this is logging to a remote logserver which is protected enough so that the attacker has no access (in the simplest case: no remote login possible).

Apart from that FreeBSD, OpenBSD and probably others have the idea of immutable or append-only files. With chflags you could mark log files this way and then only appends are possible and the file can not be deleted. Combining this feature with securelevel and you can achieve log files which can only be changed (removed, rotated) in single-user mode.

And you could use alternative or extended security mechanisms like SELinux to make sure that the "normal" root user cannot change files.

Steffen Ullrich
  • 184,332
  • 29
  • 363
  • 424
  • 1
    Been a while since I've used FreeBSD / OpenBSD, but what happens when you do this? `chflags nouchg file`. And I'm almost fairly confident that securelevel can be circumvented. – Mark Buffalo Nov 10 '15 at 18:55
  • 1
    Linux has immutable files too, and you can permanently revoke the right to change the immutable flag on any file until rebooted... this makes certain mechanisms at least hard to tamper with. You need to revoke further rights to make that really bulletproof though - if root can change memory or block device contents directly.... so logging to a remote service, or to true append-only media (eg a printer, cd-r, tape that cannot be made to rewind from the host) is the only really safe thing there... – rackandboneman Nov 10 '15 at 19:26
  • @MarkHulkalo: I have no idea of uchg but schg can only cleared by the superuser and only when running in security level 0 or -1 (see [documentation](https://www.freebsd.org/cgi/man.cgi?query=chflags&sektion=1). And I doubt that you will be able to decrease the securelevel once it is set without a reboot. – Steffen Ullrich Nov 10 '15 at 19:29
  • 1
    @rackandboneman: Linux has immutable flag but linux has not the simple securelevel concept. Instead they use capabilities and by default root has all of them - which means that root can remove the immutable flag again. – Steffen Ullrich Nov 10 '15 at 19:31
  • @Steffen Ullrich root can drop the capability to set or unset the flag - permanently – rackandboneman Nov 10 '15 at 20:18
  • @rackandboneman: that's what I mean - the idea of securelevel from *BSD is more simple and you even root cannot decrease the securelevel without a reboot. – Steffen Ullrich Nov 11 '15 at 05:31
  • Neither can root on a linux system undo dropping CAP_SYS_IMMUTABLE, unless you left capabilities enabled that would allow forcing the kernel (CAP_SYS_RAWIO). – rackandboneman Nov 11 '15 at 17:14
  • Well, if all else fails, just load your own damn driver and do it from kernel-mode. – Deduplicator Nov 11 '15 at 19:27
  • @Deduplicator: I recommend you read about what [securelevel](https://www.freebsd.org/doc/faq/security.html#idp60199632) does. Securelevel also limits the ability to load kernel modules. And the same restriction can be done with Linux capabilities. Properly used these restriction can limit the abilities of root a lot, at least until the next restart. Thus if you make sure that these restrictions are established before you even start the network interface then you can limit the capabilities of a remote user a lot. – Steffen Ullrich Nov 11 '15 at 21:54
4

If the server has had the root user compromised, that user can certainly delete any files (including logs) on the systems. But, in most enterprises, logs aren't stored locally.

If logs are stored locally and deleted, in order to determine the extent of the breach, forensic analysis can be conducted to recover the files.

However, most enterprise web servers and corporate environments use some type of log management -- this can range from standard tools such as Syslog and SNMP, and proprietary log management software like Splunk.

When those systems are used, the attacker would have to compromise the log management system and any backups thereof in order to delete the logs and cover their tracks.

So sure, one can as root delete files; but this isn't a certain way of covering one's tracks.

Herringbone Cat
  • 4,242
  • 15
  • 19
4

Because WORM (write once, read many) devices are still uncommon. In systems where security and forensics are very important these devices are used to record logs in real time, but cannot be deleted from, even by root. To be able to implement this securely it has to be a hardware limitation. Less securely it could expose an API to a software service which only has operations to write new files and read old ones. If the server also has a hardware device or hypervisor which logs basically every change to the system it would be very difficult to hide your tracks.

l0b0
  • 2,981
  • 20
  • 29
  • Do these WORM devices probably just use DVD+-R's, or something more exotic/expensive? – Xen2050 Nov 11 '15 at 09:24
  • Some magneto-optic drives were WORM, but I haven't seen one of them in years. – Simon B Nov 11 '15 at 11:07
  • @Xen2050 a DVD-R burning session takes minutes, which may well be enough for the attacker to remove the traces, so the next DVD will not contain them. – Dmitry Grigoryev Nov 11 '15 at 17:21
  • @DmitryGrigoryev I'd assume they would use a custom writing format, so no waiting for a whole "session to close" or anything like that. I remember old CD-R software that would treat the whole disc like a big floppy and, I think, start writing files in a few seconds, it was only "finishing" the whole disc that took a few minutes. Packet writing it's called. (And replace DVD-R with BD-R XL, or whatever the current big-disc letters are in the future) – Xen2050 Nov 11 '15 at 18:05
  • @Xen2050 I believe packet writing only seems to happen within seconds because of caching, and ejecting the disk before the session is closed would result in data loss. – Dmitry Grigoryev Nov 11 '15 at 18:17
  • @Xen2050 Early in my career, a mainframe admin accidentally included a write-only disk drive in the system's general drive pool. I got very bizarre errors in retrieving from a temp file I'd created and reported to the site's support group. It was a diagnostics drive supposed to be dedicated for on-site IBM support. Some 40+ years ago, so the concept's been around for a while. – user2338816 Nov 11 '15 at 19:50
4

For the truly paranoid, something along the lines of:

tail -f /var/log/auth.log > /dev/lp0

... would probably stop all but those who would stoop to arson.

Whip out the Oki ribbons!

4

Because SOMEBODY has to be able to manage log files, and if not root, then who? Would you really want a system where if, say, a log file grows to take up 90% of your disk space, there is absolutely no way to delete it?

Sure, this is a security hole. In the same sense that a system that allows any user to do anything creates a security hole. Most door locks are designed so that the lock can only be removed from the door by someone who is inside the house. But this means that anyone who can get inside your house, such as a guest or a plumber, could replace your door lock with one to which only he has the key. Is this a security hole? Sure. But what's the alternative? Make it so that no one can replace the lock? What if the lock breaks?

In real life, it's not a simple either/or: "insecure systems" where anybody can get in and do anything, versus "secure systems" where we are 100% certain that only authorized users can do authorized things. There's a range of "secureness". Often, the more secure you make a system, the more inconvenient it becomes for authorized users to do their jobs. At some point security is so tight that the system is virtually unusable.

Jay
  • 859
  • 5
  • 5
  • Yes but why the superuser? why not let this aspect managed by an other user in order to make it difficult for the hacker to erase his tracks? – Ulkoma Nov 11 '15 at 19:47
  • Okay, so suppose instead of root manipulate the logs, you create some other user, "log_manager", who can manipulate the logs. How does this help? Then if a hacker can log in as log_manager, he can cover his tracks. How is this better than requiring him to log in as root to do this? If root has authority to change log_manager's password, then if the hacker can get in as root, he can get in as log_manager. I suppose you could create many users, each able to do only a small set of "admin" things, and with no one user able to manage all their IDs and passwords, but like only Al can update Bob, ... – Jay Nov 11 '15 at 20:13
  • ... only Bob can update Cathy, only Cathy can update David, etc. Still, if the hacker can get in at any point in the chain, he could chase it down. It would be harder, and if security needs were big enough -- if we're talking plans for nuclear weapons or my credit card number -- maybe it's worth it. But it's still not 100% secure, it's just more work to hack. – Jay Nov 11 '15 at 20:15
  • why cant we have something like the SAM files in MSWindoes, even the administrator can't modify them, only the system user can. This means if you want to mess with them you need to boot the server from an external device. How difficult it is to implement such thing?? – Ulkoma Nov 11 '15 at 20:19
2

I've kind of said this in comments but it seems it deserves a full answer.

If you're using SELinux, you can configure it in such a way so that root cannot delete log files. SELinux uses Mandatory Access Control (control based on roles) in order to determine which roles can read/write/execute each file, on top of Linux's Discretionary Access Control which states what each user/group/everyone can do to a file - the typical rwxrwxrwx permissions.

There seems to be confusion about how SELinux might be used to accomplish this, but it's pretty straight forward. One possible point of confusion is the idea of a Linux user and an SELinux role. Nothing changes as far as Linux users go. SELinux only adds on top of what Linux already has. So you'd setup your users as normal. root would be the same as always as well.

Then you need to introduce SELinux roles. Linux users can be assigned many SELinux roles, and a user's roles determine what it can do on the system. Now, the default SELinux policy - 'targeted' I think is what it's called - has an unconfined user, which is analogous to a superuser. By default, root is assigned as unconfined, so root can do anything just like it could without SELinux. I imagine most people don't modify the default policy or the default roles in SELinux, so it gives the impression that root can do whatever it wants.

However, this isn't necessary. If you take away root's SELinux role, then it can't do anything(you wouldn't even be able to log in). Before doing this, however, you would want to setup a user with the role of administrating SELinux. Both 'targeted' and 'mls' policies come with such administration roles built in, but I forget what it's called exactly. You'd typically give this to one of your system administrators so that they can modify SELinux policy, roles, etc if needed.

Your question hits on the entire point to SELinux and its mandatory access system. People realized that getting root gives hackers far too much. They can screw up your ssh, your httpd, and anything else they want to. But with SELinux you can restrict or remove root access, so even if someone logs in to root they can't just destroy your system. Note: this applies to more than just log files. This applies to everything on the system. Getting root becomes meaningless very quickly if you configure SELinux properly.

And to be clear - some user at some point has to be able to delete the log files. The point of this isn't to make deleting log files impossible, the point is to make it impossible for root to delete any/all log files. If someone/something on the system can make a log file, then they can also delete that log file, unless you're using some sort of write-once memory like others have mentioned.

So then the question shifts a little bit: a hacker compromises your web service, and can subsequently delete the logs associated with it, right? And that's really what we care about, because that's the log that will trace their actions. If they can still modify that, then what's the point of this whole circus act? Well, the answer is they might be able to delete/alter the log files, depending on what context they're running in once they've exploited the system, and if that context has permission to modify the relevant log files. But that's a rabbit hole in itself and this answer is long enough as-is. Still, SELinux will be recording system calls anyways, and the hacker who compromised your web service won't be able to modify SELinux logs (they're two separate contexts/roles on the system), so you'll at least have that.

Shaz
  • 374
  • 1
  • 4
1

By default, nothing stops an attacker from deleting the logs to cover their tracks, however, if an attacker deletes the logs it will be obvious that something has happened. Something more intelligent from an attacker point of view is modifying the logs to just delete his/her tracks.

In the other hand, you can implement some measures to protect the logs, for example, send them in real time to an external server.

Eloy Roldán Paredes
  • 1,507
  • 12
  • 25