70006¥

IT & DIARY 自己満ブログ

勉強)汎用CPU Register

EAX register (accumulator register) : 数学的な演算を行うとき使うか関数のretrun値を伝達するために使われる。+, -, x, / , > などの演算は大体このregisterを使う。特に X, / などの演算はの場合EAX registerでしか使えない。関数のreturn値はEAX registerに保存されるからEAX registerを確認すると呼び出した関数が成功したか失敗したかがわかるし返却した値が何かをわかる。

 

EDX register (data register) : EAX registerの拡張概念として使われる。掛け算、割算など複雑な演算のために追加でデータを保存するとき使われてる。大体EAX registerと一緒に使う。

 

ECX register (count register) : 繰り返される演算に使われる。文字列を保存したり、カウントをする作業をする。大事なことは値を増やしながら数えるのではなく減らしながら数えるという点である。

*1

 

ESI register (source index) : データを読むために使われる。

 

EDI register (destination index) : データを書くために使われる。

 

ESP register (stack pointer) : 関数を呼び出すとき関数に伝われるparameterがスタックにpushされてその次にはreturnのアドレスがpushされる。

 

EBP register : 呼び出されたスタックの一番下の位置を示すときに使われる。

 

EIP register : 現在実行中の命令の住所を示す。

*1:アセンブリ言語ではカウントするときいつも減らしながらカウントされる

OverTheWire - Level0~Level1

問題:

OverTheWire: Level Goal

Bandit Level 0 → Level 1

Level Goal

The password for the next level is stored in a file called readme located in the home directory. Use this password to log into bandit1 using SSH. Whenever you find a password for a level, use SSH (on port 2220) to log into that level and continue the game.

Commands you may need to solve this level

ls, cd, cat, file, du, find

 

 

今回の問題は次のlevelに行くためのpasswordがreadmeって言うファイルに書いているからreadmeを読めば解決できる。

とりあえず、homeにどんなファイルがあるか確認。

f:id:psharp:20180708035439p:plain

ls -alはla -aとls -lを合わせた感じで、ドットで始めるファイルとファイルの詳細を出力するって言う意味。

 

一番最後に出る

-rw-r----- 1 bandit1 bandit0 33 Dec 28 2017 readme

に注目。

問題で要求したreadmeと言うファイルがこのdirectoryにあることが分かった。

すぐ cat readme でファイルを読んで見よう

f:id:psharp:20180708041809p:plain

(passwordは途中までしか写ってない。)

次のlevelへのpasswordが出てくる。

 

以上がlevel0~level1の過程です。

 

 

 

 

参考:

【 ls 】 ファイル情報を出力する 【 Linuxコマンドまとめ 】 | Linux Fan

 

windows) python debugger #1 (失敗)

今回はパイソンでdebuggerを作ってみ ることにしてみた。

まずパイソンでwinapiの一種であるCreateProcessAを使うためstructureを定義するmy_debugger_defines.pyを作ってみた。

以下my_debugger_defines.pyのコード:

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
from ctypes import *
 
WORD    = c_ushort
DWORD    = c_ulong
LPBYTE    = POINTER(c_ubyte)
LPTSTR    = POINTER(c_char)
HANDLE    = c_void_p
 
# like DEFINE
DEBUG_PROCESS        = 0x00000001
CREATE_NEW_CONSOLE    = 0x00000010
 
#structure for CreateProcessA 
class STARTUPINFO(Structure):
    _fields_ = [
    ("cb",                DWORD),
    ("lpReserved",        LPTSTR),
    ("lpDesktop",        LPTSTR),
    ("lpTitle",            LPTSTR),
    ("dwX",                DWORD),
    ("dwY",                DWORD),
    ("dwXSize",            DWORD),
    ("dwYSize",            DWORD),
    ("dwXCountChars",    DWORD),
    ("dwYCountChars",    DWORD),
    ("dwFillAttribute",    DWORD),
    ("dwFlags",            DWORD),
    ("wShowWindow",        WORD),
    ("cbReserved2",        WORD),
    ("lpReserved2",        LPBYTE),
    ("hStdInput",        HANDLE),
    ("hStdOutput",        HANDLE),
    ("hStdError",        HANDLE),
    ]
 
class PROCESS_INFORMATION(Structure):
    _fields_ = [
    ("hProcess",    HANDLE),
    ("hThread",        HANDLE),
    ("dwProcessId",    DWORD),
    ("dwThreadId",    DWORD),
    ]
 
 
    
cs

 

次はこのAPIを使って実行したprocessをdebuggerに添付するclass debuggerが書いているmy_debugger.pyのコード:

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from ctypes import *
from my_debugger_defines import *
 
kernel32  = windll.kernel32
 
class debugger():
    def __init__(self):
        self.h_process            = None
        self.pid                 = None
        self.debugger_active    = False
        
    def load(self,path_to_exe):
        #if you want to see GUI of Calc, creation_flags = CREATE_NEW_CONSOLE
        creation_flags = DEBUG_PROCESS
 
        startupinfo         = STARTUPINFO()
        process_information    = PROCESS_INFORMATION()
 
        startupinfo.dwFlags        = 0x1
        startupinfo.wShowWindow    = 0x0
 
        startupinfo.cb             = sizeof(startupinfo)
 
        if kernel32.CreateProcessA(    path_to_exe,
                                    None,
                                    None,
                                    None,
                                    None,
                                    creation_flags,
                                    None,
                                    None,
                                    byref(startupinfo),
                                    byref(process_information)):
            print "[*] We have successfully launched the process!"
            print "[*] PID: %d" % process_information.dwProcessId
            self.h_process = self.open_process(process_information.dwProcessId)
        else:
            print "[*] Error: 0x%08x." % kernel32.GetLastError()
 
    def open_process(self,pid):
        h_process = kernel32.OpenProcess(PROCESS_ALL_ACCESS,False,pid)
        return h_process
        
    def attach(self,pid):
        self.h_process = self.open_process(pid)
 
        if kernel32.DebugActiveProcess(pid):
            self.debugger_active = True
            self.pid              = int(pid)
        else:
            print "[*] Unable to attach to the process."
    
    def run(self):
        while self.debugger_active == True:
            self.get_debug_event()
 
    def get_debug_event(self):
        debug_event     = DEBUG_EVENT()
        continue_status = DBG_CONTINUE
 
        if kernel32.WaitForDebugEvent(byref(debug_event),INFINITE):
            raw_input("Press a Key to continue...")
            self.debugger_active = False
            kernel32.ContinueDebugEvent( \
                debug_event.dwProcessId,\
                debug_event.dwThreadId,\
                continue_status )
 
    def detatch(self):
        if kernel32.DebugActiveProcessStop(self.pid):
            print "[*] Finished debugging. Exiting..."
            return True
        else:
            print "There was an error"
            return False

cs

 

 

 

最後にpidを入力してdebuggerにprocessを添付させるmy_test.pyのコード:

1
2
3
4
5
6
7
import my_debugger
 
debugger = my_debugger.debugger()
pid = raw_input("Enter the PID of the process to attach to: ")
debugger.attach(int(pid))
debugger.detach()
 
cs

 

結果:失敗。

問題点が何かがわからない。次はこの問題を解決してみせる。