Hacking bash History

Source: Internet
Author: User

[------------------------- [Hacking bash History] ---------------------------]
[===================================================== ===============================================]

By: ithilgore-ithilgore.ryu.L@gmail.com
Sock-raw.org/sock-raw.homeunix.org
July 2008

------------- [Table of Contents] -------------

I. Preface
Ii. Hardening bash_history
Iii. Attacking the logging mechanism
Iv. Hacking bash-interfacing with syslog
V. Conclusion
Vi. References


[I. Preface]
====================

Bash is probably the most widely used shell in the * nix world and one of its
Features is the history mechanic. The history mechanic is mainly used for
Users convenience-less typing-> work done faster. However, it has been
Discussed that bash_history can also be used as a logging mechanic to monitor
Users activity. This article covers the arguments against the above and why
Mechanic is useless against someone who thinks out of the box. We are going
To see that every defensive measure taken for protecting the history file can
Be subverted with little or no difficulty. The discussion will be increasive
In the strictness of the methods applied but that doesnt meant they will be
Increasingly difficult to implement. Most of them are no-brainers. In the end,
We are going to meddle with the bash source code to make the logging mechanism
(At first sight) "invincible" and we are going to see why even that can fail.

 

[Ii. Hardening bash_history]
====================================

Suppose you are an administrator of a shell-providing box and there is a really
Pesky user whose activities you wowould like to monitor, since you are really
Suspicious about what he does late at night with the precious CPU power and
System resources that you have pledged to protect against malicious (or other)
Usage. Lets call the user Bob-enough of using Trinity as the "bad" one all
The time. Since all users use bash as their default shell in the server, you
Start making a few changes to the bash configuration files.


// Step 1 //

-- Make the bash history and relevant files undeletable/unchangeable.

The first thing Bob wowould probably do wowould be to symlink his history
/Dev/null.

Bob $ rm ~ /. Bash_history
Bob $ ln-s/dev/null ~ /. Bash_history

That can be prevented by making that file append-only. This can be accomplished
By issuing the following command:

# Chattr + a/home/bob/. bash_history

This will use file system extended attributes to mark the file as append only.
Most filesystems (ext2/3, XFS, JFS) support this. On FreeBSD the same
Wocould be done by issuing:

# Sappnd/home/bob/. bash_history

You might also want to apply this to all the bash configuration files that
Are read during bash startup:

# Chattr + a/home/bob/. bash_profile
# Chattr + a/home/bob/. bash_login
# Chattr + a/home/bob/. profile
# Chattr + a/home/bob/. bash_logout
# Chattr + a/home/bob/. bashrc

The first three are read by bash in that order (after reading/etc/profile
Which applies to all users) when an interactive login bash shell (or
Non-interactive shell with the -- login option) is invoked.
. Bashrc is only read when a non-login interactive shell is invoked. That means
The case when the user has already logged in and invokes a new bash shell
Himself like:

Bob $ bash

Note that. bashrc is the * only * configuration file that is read in this case.
The other 3 conf files are * not * read again.

After doing the above changes, its time to move on to some more "hardening ".
One more step towards (futile) protection.


// Step 2 //

-- Configure. bash * configuration files

All changes will be made to. bashrc. It is assumed the other three
Configuration files mention reading. bashrc in their body. This means that
. Bashrc is read in * every * case (whether the user just logins or invokes a new
Bash shell after he has logged in ).

By making all changes to. bashrc protects against the case where Bob wowould
Invoke a new bash shell after he had logged in so that all configuration
Options wocould be nullified. If the options were only at the three main
Configuration files (. bash_profile,. bash_login,. profile) then the above wowould
Happen. On the other hand, these files must read. bashrc in their body so that
The options mentioned to. bashrc are actually applied in the first login shell
As well.

# Cat>/home/bob/. bashrc <EOF
> Shopt-s histappend
> Readonly PROMPT_COMMAND = "history-"
> EOF

The option histappend orders bash to append the last $ HISTSIZE lines to
$ HISTFILE file (normally ~ /. Bash_history) whenever an interactive shell exits.
By default, bash overwrites $ HISTFILE each time so that only one session is
Kept to save space.

The enviromental variable PROMPT_COMMAND holds a command that is to be executed
Prior to issuing each prompt. This means that "history-a" is executed prior
To every command the user issues. This ensures that whatever command was typed
Just before the current one, is immediately appended to $ HISTFILE. This ensures
More robustness in the logging mechanism, as bash doesnt wait until the whole
Session is finished to transfer to the disk the history lines from the memory.
The readonly attribute is used so that this variable is marked as non-writeable
In case Bob wants to ovewrite it and most probably nullify it.

One last substep to the above changes wocould be to mark as readonly all
Environment variables associated with bash_history:

Readonly HISTFILE
Readonly HISTFILESIZE
Readonly HISTSIZE
Readonly HISTCMD
Readonly HISTCONTROL
Readonly HISTIGNORE


// Step 3 //

-Disable all access to all other out of the box shells of the system. Usually,
These will be csh, tcsh and maybe ksh.

# Chmod 750 csh
# Chmod 750 tcsh
# Chmod 750 ksh

This will prevent Bob from changing his shell from bash to another one.
Now, the astute administrator will complain that the above are ** not *
The only shells out of the box! This is both true and false. But before you jump
To quantum theory conclusions based on the above statement, lets clear some
Things up.
A long time ago... (you know the rest), there was only the Bourne shell or sh.
Nowadays,/bin/sh is actually a symbolic link to/bin/bash. Bash checks
Name by which it was invoked and if this is sh, it tries to mimic the behaviour
Of the historic versions of sh and also conform to POSIX. If started as
Interactive login shell or non-interactive shell with the -- login option it
Attemts to read/etc/profile and ~ /. Profile for startup configuration. If it is
Invoked as an interactive shell, then it tries to expand the variable $ ENV and
If it is not empty, uses its value as the configuration file to read and
Execute. We shall see in the next section of this text, how this can be used
Override most or all bash settings.

 

[Iii. Attacking the logging mechanism]
==========================================================

It is time to see the whole thing from Bobs perspective. We are going
Examine how each of the above steps can be subverted. In practice,
Possibilities are endless. The techniques that will be discussed here are only
A small subset of the available methods to override the bash_history logging
Mechanisms.


// Method 1 //

-- Use the Bourne shell/bin/sh as an escape mechanic

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.