본문 바로가기

Learning/└◆리눅스 서버 보안

01_HISTTIMEFORMAT 변수(관리자 정책_사용자 환경파일 관리)

HISTTIMEFORMAT 변수

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

(linux200)

<!--[if !supportEmptyParas]--> <!--[endif]-->

HISTTIMEFORMAT 변수

명령어 히스토리(Command history) 기능에 시간을 설정할 수 있는 기능이다.

사용자가 수행한 명령어를 추적할 때 사용할 수 있으므로 사용을 권장한다.

<!--[if !supportEmptyParas]--> <!--[endif]-->

# export LANG=C

# man bash

/HISTTIMEFORMAT

HISTTIMEFORMAT

If this variable is set and not null, its value

is used as a format string for strftime(3) to

print the time stamp associated with each history

entry displayed by the history builtin. If this

variable is set, time stamps are written to the

history file so they may be preserved across

shell sessions.

man 3 strftime 에서 format string의 자세한 정보가 있다. 


<!--[endif]-->

# man 3 strftime

/%F

%F Equivalent to %Y-%m-%d (the ISO 8601 date for-

mat). (C99)

/%T

%T The time in 24-hour notation (%H:%M:%S). (SU)

<!--[if !supportEmptyParas]--> <!--[endif]-->

# vi /etc/profile

..... (중략) .....

for i in /etc/profile.d/*.sh ; do

if [ -r "$i" ]; then

if [ "${-#*i}" != "$-" ]; then

. $i

else

. $i >/dev/null 2>&1

fi

fi

done

<!--[if !supportEmptyParas]--> <!--[endif]-->

#

# (1) Sfecific Configuration

#

export HISTTIMEFORMAT="%F %T "

<!--[if !supportEmptyParas]--> <!--[endif]-->

unset i

unset pathmunge

<!--[if !supportEmptyParas]--> <!--[endif]-->

# telnet localhost

user01 사용자로 로그인

<!--[if !supportEmptyParas]--> <!--[endif]-->

$ export LANG=C

$ history

..... (중략) ....

1 2016-05-24 12:39:02 clear

2 2016-05-24 12:39:04 id

3 2016-05-24 12:39:11 export LANG=C

4 2016-05-24 12:39:15 history

<!--[if !supportEmptyParas]--> <!--[endif]-->

$ exit

# export LANG=C

# history

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

Command History (History)

이전에 사용한 명령어를 저장하는 기능

<!--[if !supportEmptyParas]--> <!--[endif]-->

# ps

PID TTY TIME CMD

5028 pts/3 00:00:00 bash

<!--[if !supportEmptyParas]--> <!--[endif]-->

# pmap 5028 메모 어드레스 번지와 올라가있는 내용

5028: bash

00235000 4K r-x-- [ anon ]

00574000 108K r-x-- /lib/ld-2.5.so

0058f000 4K r-x-- /lib/ld-2.5.so

00590000 4K rwx-- /lib/ld-2.5.so

00593000 1368K r-x-- /lib/libc-2.5.so

006e9000 8K r-x-- /lib/libc-2.5.so

006eb000 4K rwx-- /lib/libc-2.5.so

006ec000 12K rwx-- [ anon ]

0071c000 12K r-x-- /lib/libdl-2.5.so

0071f000 4K r-x-- /lib/libdl-2.5.so

00720000 4K rwx-- /lib/libdl-2.5.so

00ded000 40K r-x-- /lib/libnss_files-2.5.so

00df7000 4K r-x-- /lib/libnss_files-2.5.so

00df8000 4K rwx-- /lib/libnss_files-2.5.so

06764000 12K r-x-- /lib/libtermcap.so.2.0.8

06767000 4K rwx-- /lib/libtermcap.so.2.0.8

08047000 700K r-x-- /bin/bash

080f6000 24K rw--- /bin/bash

080fc000 16K rw--- [ anon ]

09e0f000 264K rw--- [ anon ]

b7d92000 2048K r---- /usr/lib/locale/locale-archive

b7f92000 8K rw--- [ anon ]

b7f97000 8K rw--- [ anon ]

b7f99000 28K r--s- /usr/lib/gconv/gconv-modules.cache

bfd16000 84K rw--- [ stack ]

total 4776K

<!--[if !supportEmptyParas]--> <!--[endif]-->

[ stack ]

<!--[if !supportEmptyParas]--> <!--[endif]-->

| | ---------

                    |          A

|          |

|                  |   1000

|                  |

|                  |    V

+-----------------+ ---------

                 ~/.bash_history

<!--[if !supportEmptyParas]--> <!--[endif]-->

HISTSIZE=1000 /* stack 크기 */

HISTFILE=~/.bash_history /* history 파일 지정 */

HISTFILESIZE=1000 /* history 파일 크기 지정 */

<!--[if !supportEmptyParas]--> <!--[endif]-->

# grep -i HISTSIZE /etc/profile

HISTSIZE=1000

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC

<!--[if !supportEmptyParas]--> <!--[endif]-->



[참고] history -c (~/.bash_logout)         -c clear 스택의 내용을 삭제 (중요 명령어passwd등 볼 수없게)

(history -c 명령어의 필요성)

# echo user01 | passwd --stdin user01

# mysql -u root -p soldesk1.

# smbclient -L 192.168.20.200 -u root -p soldesk1.

<!--[if !supportEmptyParas]--> 

관리자 정책<!--[endif]-->


사용자 history 관리

# vi /etc/skel/.bash_logout       사용자 홈 디렉토리 하위에 설정이 들어간다 /etc/skel

# ~/.bash_logout

<!--[if !supportEmptyParas]--> <!--[endif]-->

/usr/bin/clear

history -c

<!--[if !supportEmptyParas]--> <!--[endif]-->

# useradd test01

# passwd test01

# su - test01

# ls -al

.bash_logout

# cat ~/.bash_logout

# ~/.bash_logout

<!--[if !supportEmptyParas]--> <!--[endif]-->

/usr/bin/clear

history -c

<!--[if !supportEmptyParas]--> <!--[endif]-->

(복원) /etc/skel/.bash_logout 파일 추가된 삭제(history -c)

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

사용자가 환경파일을 변경할수 없도록 설정

<!--[if !supportEmptyParas]--> <!--[endif]-->

() history -c ---> /etc/skel/.bash_logout(history -c)

                     --- useradd CMD ---> /home/$USER/.bash_logout(사용자 삭제 가능)

<!--[if !supportEmptyParas]--> <!--[endif]-->

/home/user01 (rwxr-xr-x root root)    관리자권한으로 모드변경

+

|

+----- .bash_profile (-rw-r--r-- root root)     <---- cd work

|        ......

+----- .bash_logout (-rw-r--r-- root root)    logout 디렉토리 점프  

|

+----- work (drwx------ user01 user01)

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

~/.bash_history 파일

<!--[if !supportEmptyParas]--> <!--[endif]-->

# cat ~/.bash_history (# export LANG=C)

......

#1441086536 /* 2015.03.20 13:00:00 history 초단위 환산*/

history                            빠져나간 사람들을 추적하기 위한 출력표시

#1441086605

vi /etc/skel/.bash_logout

#1441088279

clear

#1441088283

unalias ls

#1441088291

useradd hacker

<!--[if !supportEmptyParas]--> <!--[endif]-->

# pinfo date

-> Examples of date 선택

-> 예제들 중 아래 명령어 예제를 찾는다.


# date +%Y:%m:%d                 <-- 오늘

# date -d '1 day ago' +%Y:%m:%d  <-- 하루 전

 <!--[endif]-->

# date -d '1970-01-01 UTC 1441155935 seconds' +"%Y-%m-%d %T %z"   (# date +%Y:%m:%d)

2015-12-30 14:29:09 +0900

-> 시간 출력 포맷을 변경하여 출력
-> 위 ~/.bash_history 의 초단위 환산 출력 시간을 date -d 명령어를 사용해서 보기좋게 변환


<!--[if !supportEmptyParas]-->
위 명령어를 짧게 사용하기


<!--[endif]-->

(명령어 형식)

# time.sh 1441155935

2015-12-30 14:29:09 +0900

<!--[if !supportEmptyParas]--> <!--[endif]-->

# cd /root/bin

# vi time.sh


#!/bin/bash

 

eval date -d \'1970-01-01 UTC $1 seconds\' +\"%Y-%m-%d %T %z\"

 

# chmod 755 time.sh

 

# cat ~/.bash_history

-> 적당한 시간 선택

# ./time.sh 1441155935

2015-12-30 14:29:09 +0900

 



<!--[if !supportEmptyParas]--> <!--[endif]-->

cat ~/.bash_history 파일의 출력 형식 변경 작업

<!--[if !supportEmptyParas]--> <!--[endif]-->

~/.bash_history ---- 보기좋은 형식 -----> ~/.bash_history.conv

#1450183998                                  2015-12-30 14:29:09 +0900      passwd

passwd

#1450184477                                  2015-12-30 14:29:09 +0900      clear

clear

#1450184555                                  2015-12-30 14:29:09 +0900      poweroff

poweroff

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

# man paste  파일을 여러가지 형태로 합칠 수 있다.

NAME

paste - merge lines of files

# cat file1

1111

2222

3333

# cat file2

aaaa

bbbb

cccc

# paste file1 file2

1111 aaaa

2222 bbbb

3333 cccc

<!--[if !supportEmptyParas]-->
컨버팅 프로그램 작성 <!--[endif]-->

          # ~/.bash_history 내용(시간, 명령어)
          # paste time.sh 1441155935


<!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> ■ 사용자가 환경파일을 변경할수 없도록 설정<!--[endif]-->

root[~]# useradd user10<!--[endif]-->

root[~]# passwd user10

Changing password for user user10.

New UNIX password: 

BAD PASSWORD: it is based on a dictionary word

Retype new UNIX password: 

passwd: all authentication tokens updated successfully.

root[~]# ls -l /home

total 8.0K

drwx------ 3 user01 user01 4.0K Feb 20 16:32 user01

drwx------ 3 user10 user10 4.0K Feb 20 18:06 user10

root[~]# 



root[~]# cd /home

root[/home]# ls -l

total 8.0K

drwx------ 3 user01 user01 4.0K Feb 20 16:32 user01

drwx------ 3 user10 user10 4.0K Feb 20 18:06 user10

root[/home]# chmod 755 user10

root[/home]# chown root:root user10

root[/home]# ls -l

total 8.0K

drwx------ 3 user01 user01 4.0K Feb 20 16:32 user01

drwxr-xr-x 3 root   root   4.0K Feb 20 18:06 user10

root[/home]# 



root[~]# cd /home/user10

root[/home/user10]# vi .bash_profile

root[/home/user10]# ls -l .bash_profile

-rw-r--r-- 1 user10 user10 186 Feb 20 18:08 .bash_profile




# .bash_profile


# Get the aliases and functions

if [ -f ~/.bashrc ]; then

    . ~/.bashrc

fi


# User specific environment and startup programs


PATH=$PATH:$HOME/bin



export PATH


cd work  <-- 라인 추가


root[/home/user10]# chown root:root .bash_profile
root[/home/user10]# ls -l .bash_profile 
-rw-r--r-- 1 root root 186 Feb 20 18:08 .bash_profile


root[/home/user10]# mkdir work
root[/home/user10]# chmod 700 work
root[/home/user10]# chown user10:user10 work
root[/home/user10]# ls -ld work
drwx------ 2 user10 user10 4.0K Feb 20 18:09 work


root[/home/user10]# telnet localhost
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.

    linux200.example.com (Linux release 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:35 EDT 2010) (2)

login: user10


[user10@linux200 work]$ pwd
/home/user10/work
[user10@linux200 work]$ 


[user10@linux200 work]$ cd ..
[user10@linux200 ~]$ ls -la
total 36
drwxr-xr-x 4 root   root   4096 Feb 20 18:09 .
drwxr-xr-x 4 root   root   4096 Feb 20 18:06 ..
-rw-r--r-- 1 user10 user10   33 Feb 20 18:06 .bash_logout
-rw-r--r-- 1 root   root    186 Feb 20 18:08 .bash_profile
-rw-r--r-- 1 user10 user10  124 Feb 20 18:06 .bashrc
drwxr-xr-x 4 user10 user10 4096 Feb 20 18:06 .mozilla
-rw-r--r-- 1 user10 user10   81 Feb 20 18:06 .vimrc
drwx------ 2 user10 user10 4096 Feb 20 18:09 work
[user10@linux200 ~]$ 

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->

<!--[if !supportEmptyParas]--> <!--[endif]-->


스크립트

#!/bin/bash



#cat ~/.bash_history | grep '^#' > history1.txt

cat ~/.bash_history | grep '^#' | awk -F# '{print $2}' > history1.txt

cat ~/.bash_history | grep -v '^#' > history2.txt


#eval date -d \'1970-01-01 UTC $1 seconds\' +\"%Y-%m-%d %T %z\"

#date -d '1970-01-01 UTC 1441155935 seconds' +"%Y-%m-%d %T %z"


LINE='cat /root/bin/history1.txt'

while read LINE

do

    ./time.sh $LINE

done < time.txt > history2.txt



paste history1.txt history2.txt > history.txt

cat history.txt


#expr='$COUNT + 1'

#sed -n "${COUNT},s{COUNT2}p"

#MIN='expr $MIN + 2'


<!--[if !supportEmptyParas]--> <!--[endif]-->

history_윤진식.txt

histroty(이도경).txt

history김영두.txt

hisy.sh(이헌종).txt

history_박철용.txt

his_time_history_이대수.txt

test(황덕현).txt