Bash & Vim

From wiki.kungfootek.net
Jump to: navigation, search

Bash Envronment

Default Editor

Make VIM your default editor

export VISUAL=vim


My .bashrc

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

export VISUAL=vim

function MyPrompt {

        EXITSTATUS="$?"
        BOLD="\[\033[1m\]"
        RED="\[\033[1;31m\]"
        GREEN="\[\e[32;1m\]"
        BLUE="\[\e[34;1m\]"
        OFF="\[\033[m\]"

        if [ ${UID} -eq 0 ];
        then
                UID_COLOR=${RED}
        else
                UID_COLOR=${OFF}
        fi

        PROMPT="\n |${UID_COLOR} \u@\h ${OFF}\n | Time: \@ \n | \w \n"

        if [ "${EXITSTATUS}" -eq 0 ]
        then
                PS1="${PROMPT} ${BOLD}${GREEN}] ${OFF}"
        else
                PS1="${PROMPT} ${BOLD}${RED}] ${OFF}"
        fi

    PS2="${BOLD}>${OFF} "
}

PROMPT_COMMAND=MyPrompt

Reload Bashrc

Reload your bash settings.

source ~/.bashrc

Bash Scriptlets

Find Folder and File sizes recursively.

This is an overly complex way to perform 'du -h'

find -maxdepth 1 -exec du -sk {} \;|sort -rn|head|while read size loc; do echo "scale=2; $size/1024" |bc|gawk '{ print $1"MB '"$loc"' " }';done|column -t

Find PHP Shells

PHP shells are usually placed by malcontents that have hacked your system and will use a shell to infect other parts of your system. Place the following contents into an executable bash script and let it go where you want to

find PHP Shells.

 !/bin/bash
 find $1 -name "*.php" -o -name "*.sh" -o -name "*.txt" | xargs -i egrep -lr "back_connect|backdoor|r57|PHPJackal|PhpSpy|GiX|Fx29SheLL|w4ck1ng|milw0rm|PhpShell|k1r4|FeeLCoMz|FaTaLisTiCz|Ve_cENxShell|UnixOn|C99madShell|

Spamfordz|Locus7s|c100|c99|x2300|cgitelnet|webadmin|STUNSHELL|Pr!v8|PHPShell|KaMeLeOn|S4T|oRb|tryag|sniper|noexecshell|revengans" {} ;

Bash Expansion

Sometimes you need to create a list of IP address really fast.

echo 10.245.10.{1..252} | tr ' ' '\012'

Kill off Lots of processes

If you find yourself with hundreds of similar or regex-able processes to kill:

 killit=$(ps aux | grep wget | gawk '{print $2}') ; echo $killit ; kill -9 $killit


If you fillter by user and omit root references. Thanks to Wendre Vaughan for this one.

 killit=$(ps aux | grep <user> |grep -v root | gawk '{print $2}') ; echo $killit

VIM Environment

Change default TAB keystroke to 4 spaces

If you're doing any Programming in Python you definitely want to add this to your .vimrc.

set paste
set modeline
set tabstop=2 shiftwidth=2 expandtab
set background=dark
set softtabstop=2

syntax on
filetype indent on

Dark background

Brighten the commented text in VIM!

If you have permission ( Not just access ) to make changes to the root environment:

 echo "set background=dark" >> /etc/vimrc

If you don't control root and only have access to your home folder :

 echo "set background=dark" >> ~/.vimrc


Vim Tricks

Sudo from VIM

Getting root permissions while inside VIM

Sometimes you want to write to a read only file without losing your work or saving to a temporary file.

:w !sudo tee <FileName>

Vim Cheat Sheet

Vim cheat sheet for programmers screen.png