programing

메모리에서 지뢰 찾기의 광산 레이아웃을 나타내는 데이터 구조를 어떻게 찾을 수 있습니까?

nasanasas 2020. 9. 1. 07:36
반응형

메모리에서 지뢰 찾기의 광산 레이아웃을 나타내는 데이터 구조를 어떻게 찾을 수 있습니까?


저는 Minesweeper를 샘플 애플리케이션으로 사용하여 리버스 엔지니어링에 대해 배우려고합니다. 나는 모든 광산을 드러내는 간단한 WinDbg 명령에 대한 MSDN 기사찾았 지만 오래되었고, 자세히 설명되지 않았으며 실제로 내가 찾고있는 것이 아닙니다.

나는 IDA Pro 디스어셈블러WinDbg 디버거를 가지고 있으며 두 가지 모두에 winmine.exe를로드했습니다. 누군가 광산 필드를 나타내는 데이터 구조의 위치를 ​​찾는 측면에서 이러한 프로그램 중 하나에 대한 실용적인 팁을 제공 할 수 있습니까?

WinDbg에서는 중단 점을 설정할 수 있지만 중단 점을 설정할 지점과 메모리 위치를 상상하기가 어렵습니다. 마찬가지로 IDA Pro에서 정적 코드를 볼 때 광산 필드를 나타내는 함수 또는 데이터 구조를 어디서부터 찾을 지 확신 할 수 없습니다.

저를 올바른 방향으로 안내 할 수있는 Stackoverflow에 리버스 엔지니어가 있습니까?


3 부 중 1 부


리버스 엔지니어링을 진지하게 생각한다면 트레이너와 치트 엔진은 잊으십시오.

좋은 리버스 엔지니어는 먼저 OS, 핵심 API 기능, 프로그램 일반 구조 (실행 루프, Windows 구조, 이벤트 처리 루틴), 파일 형식 (PE)을 알아야합니다. Petzold의 고전 "Programming Windows"(www.amazon.com/exec/obidos/ISBN=157231995X)와 온라인 MSDN이 도움이 될 수 있습니다.

먼저 minefield 초기화 루틴이 호출 될 수있는 위치를 생각해야합니다. 나는 다음을 생각했다 :

  • 게임을 시작할 때
  • 행복한 얼굴을 클릭하면
  • Game-> New를 클릭하거나 F2를 누르면
  • 레벨 난이도를 변경할 때

F2 가속기 명령을 확인하기로 결정했습니다.

가속기 처리 코드를 찾으려면 창 메시지 처리 절차 (WndProc)를 찾으십시오. CreateWindowEx 및 RegisterClass 호출로 추적 할 수 있습니다.

읽다:

IDA, Imports 창을 열고 "CreateWindow *"를 찾아서 이동 한 다음 "Jump xref to operand (X)"명령을 사용하여 호출 된 위치를 확인합니다. 한 번만 전화하면됩니다.

이제 위의 RegisterClass 함수와 WndClass.lpfnWndProc 매개 변수를 살펴보십시오. 제 경우에는 이미 mainWndProc 함수를 명명했습니다.

.text:0100225D                 mov     [ebp+WndClass.lpfnWndProc], offset mainWndProc
.text:01002264                 mov     [ebp+WndClass.cbClsExtra], edi
.text:01002267                 mov     [ebp+WndClass.cbWndExtra], edi
.text:0100226A                 mov     [ebp+WndClass.hInstance], ecx
.text:0100226D                 mov     [ebp+WndClass.hIcon], eax

.text:01002292                 call    ds:RegisterClassW

함수 이름에서 Enter 키를 누릅니다 ( 'N'을 사용하여 더 나은 이름으로 변경).

이제보세요

.text:01001BCF                 mov     edx, [ebp+Msg]

F2 버튼 누름의 경우 WM_COMMAND 값을 포함해야하는 메시지 ID입니다. 111h와 비교되는 위치를 찾아야합니다. IDA에서 edx를 추적하거나 WinDbg에서 조건부 중단 점설정 하고 게임에서 F2를 눌러 수행 할 수 있습니다 .

어느 쪽이든 다음과 같은 결과로 이어집니다.

.text:01001D5B                 sub     eax, 111h
.text:01001D60                 jz      short loc_1001DBC

111h를 마우스 오른쪽 버튼으로 클릭하고 "기호 상수"-> "표준 기호 상수 사용"을 사용하고 WM_을 입력하고 Enter를 누릅니다. 이제 있어야합니다

.text:01001D5B                 sub     eax, WM_COMMAND
.text:01001D60                 jz      short loc_1001DBC

메시지 ID 값을 찾는 쉬운 방법입니다.

가속기 처리를 이해하려면 다음을 확인하세요.

단일 답변에 대한 텍스트가 상당히 많습니다. 관심이 있으시면 다른 두 개의 게시물을 작성할 수 있습니다. 긴 이야기 짧은 지뢰밭 [24x36], 0x0F는 바이트가 사용되지 않음 (작은 필드 재생), 0x10-빈 필드, 0x80-광산으로 저장됩니다.

3 부 중 2 부


좋습니다. F2 버튼으로 계속 진행하겠습니다.

F2 버튼을 눌렀을 때 키보드 단축키 사용 에 따라 wndProc 기능

... WM_COMMAND 또는 WM_SYSCOMMAND 메시지를받습니다. wParam 매개 변수의 하위 단어에는 가속기의 식별자가 포함됩니다.

좋아, 우리는 이미 WM_COMMAND가 처리되는 위치를 찾았지만 해당 wParam 매개 변수 값을 결정하는 방법은 무엇입니까? 이것이 리소스 해커 가 작동하는 곳 입니다. 바이너리로 피드하면 모든 것을 보여줍니다. 나를위한 액셀러레이터 테이블처럼.

대체 텍스트 http://files.getdropbox.com/u/1478671/2009-07-29_161532.jpg

여기에서 F2 버튼이 wParam의 510에 해당하는 것을 볼 수 있습니다.

이제 WM_COMMAND를 처리하는 코드로 돌아 갑시다. wParam을 다른 상수와 비교합니다.

.text:01001DBC HandleWM_COMMAND:                       ; CODE XREF: mainWndProc+197j
.text:01001DBC                 movzx   eax, word ptr [ebp+wParam]
.text:01001DC0                 mov     ecx, 210h
.text:01001DC5                 cmp     eax, ecx
.text:01001DC7                 jg      loc_1001EDC
.text:01001DC7
.text:01001DCD                 jz      loc_1001ED2
.text:01001DCD
.text:01001DD3                 cmp     eax, 1FEh
.text:01001DD8                 jz      loc_1001EC8

컨텍스트 메뉴 또는 'H'키보드 단축키를 사용하여 소수점 값을 표시하면 점프를 볼 수 있습니다.

.text:01001DBC HandleWM_COMMAND:                       ; CODE XREF: mainWndProc+197j
.text:01001DBC                 movzx   eax, word ptr [ebp+wParam]
.text:01001DC0                 mov     ecx, 528
.text:01001DC5                 cmp     eax, ecx
.text:01001DC7                 jg      loc_1001EDC
.text:01001DC7
.text:01001DCD                 jz      loc_1001ED2
.text:01001DCD
.text:01001DD3                 cmp     eax, 510
.text:01001DD8                 jz      loc_1001EC8 ; here is our jump

일부 proc을 호출하고 wndProc를 종료하는 코드 청크로 이어집니다.

.text:01001EC8 loc_1001EC8:                            ; CODE XREF: mainWndProc+20Fj
.text:01001EC8                 call    sub_100367A     ; startNewGame ?
.text:01001EC8
.text:01001ECD                 jmp     callDefAndExit  ; default

그것이 새로운 게임을 시작하는 기능입니까? 마지막 부분에서 찾아보세요! 계속 지켜봐주세요.

3 부 중 3 부

해당 기능의 첫 번째 부분을 살펴 보겠습니다.

.text:0100367A sub_100367A     proc near               ; CODE XREF: sub_100140C+CAp
.text:0100367A                                         ; sub_1001B49+33j ...
.text:0100367A                 mov     eax, dword_10056AC
.text:0100367F                 mov     ecx, uValue
.text:01003685                 push    ebx
.text:01003686                 push    esi
.text:01003687                 push    edi
.text:01003688                 xor     edi, edi
.text:0100368A                 cmp     eax, dword_1005334
.text:01003690                 mov     dword_1005164, edi
.text:01003696                 jnz     short loc_10036A4
.text:01003696
.text:01003698                 cmp     ecx, dword_1005338
.text:0100369E                 jnz     short loc_10036A4

두 개의 값 (dword_10056AC, uValue)이 eax 및 ecx 레지스터로 읽고 다른 두 값 (dword_1005164, dword_1005338)과 비교됩니다.

WinDBG ( 'bp 01003696'; on break 'p eax; p ecx')를 사용하여 실제 값을 살펴보십시오. 저에게는 지뢰밭 치수처럼 보였습니다. 사용자 지정 지뢰밭 크기를 가지고 노는 것은 첫 번째 쌍이 새로운 차원이고 두 번째-현재 차원이라는 것을 보여주었습니다. 새 이름을 설정합시다.

.text:0100367A startNewGame    proc near               ; CODE XREF: handleButtonPress+CAp
.text:0100367A                                         ; sub_1001B49+33j ...
.text:0100367A                 mov     eax, newMineFieldWidth
.text:0100367F                 mov     ecx, newMineFieldHeight
.text:01003685                 push    ebx
.text:01003686                 push    esi
.text:01003687                 push    edi
.text:01003688                 xor     edi, edi
.text:0100368A                 cmp     eax, currentMineFieldWidth
.text:01003690                 mov     dword_1005164, edi
.text:01003696                 jnz     short loc_10036A4
.text:01003696
.text:01003698                 cmp     ecx, currentMineFieldHeight
.text:0100369E                 jnz     short loc_10036A4

조금 후에 새로운 값이 현재를 덮어 쓰고 서브 루틴이 호출됩니다.

.text:010036A7                 mov     currentMineFieldWidth, eax
.text:010036AC                 mov     currentMineFieldHeight, ecx
.text:010036B2                 call    sub_1002ED5

그리고 내가 그것을 보았을 때

.text:01002ED5 sub_1002ED5     proc near               ; CODE XREF: sub_1002B14:loc_1002B1Ep
.text:01002ED5                                         ; sub_100367A+38p
.text:01002ED5                 mov     eax, 360h
.text:01002ED5
.text:01002EDA
.text:01002EDA loc_1002EDA:                            ; CODE XREF: sub_1002ED5+Dj
.text:01002EDA                 dec     eax
.text:01002EDB                 mov     byte ptr dword_1005340[eax], 0Fh
.text:01002EE2                 jnz     short loc_1002EDA

나는 지뢰밭 배열을 발견했다고 확신했습니다. 0xF로 360h 바이트 길이 배열 (dword_1005340)을 초기화하는주기의 원인입니다.

왜 360h = 864입니까? 그 아래에는 행이 32 바이트를 차지하고 864를 32로 나눌 수있는 몇 가지 단서가 있습니다. 따라서 배열은 27 * 32 셀을 보유 할 수 있습니다 (UI는 최대 24 * 30 필드를 허용하지만 테두리에 대해 배열 주위에 1 바이트 패딩이 있음).

다음 코드는 지뢰밭 상단 및 하단 테두리 (0x10 바이트)를 생성합니다. 나는 당신이 그 혼란 속에서 루프 반복을 볼 수 있기를 바랍니다.) 나는 종이와 펜을 사용해야했습니다.

.text:01002EE4                 mov     ecx, currentMineFieldWidth
.text:01002EEA                 mov     edx, currentMineFieldHeight
.text:01002EF0                 lea     eax, [ecx+2]
.text:01002EF3                 test    eax, eax
.text:01002EF5                 push    esi
.text:01002EF6                 jz      short loc_1002F11    ; 
.text:01002EF6
.text:01002EF8                 mov     esi, edx
.text:01002EFA                 shl     esi, 5
.text:01002EFD                 lea     esi, dword_1005360[esi]
.text:01002EFD
.text:01002F03 draws top and bottom borders
.text:01002F03 
.text:01002F03 loc_1002F03:                            ; CODE XREF: sub_1002ED5+3Aj
.text:01002F03                 dec     eax
.text:01002F04                 mov     byte ptr MineField?[eax], 10h ; top border
.text:01002F0B                 mov     byte ptr [esi+eax], 10h       ; bottom border
.text:01002F0F                 jnz     short loc_1002F03
.text:01002F0F
.text:01002F11
.text:01002F11 loc_1002F11:                            ; CODE XREF: sub_1002ED5+21j
.text:01002F11                 lea     esi, [edx+2]
.text:01002F14                 test    esi, esi
.text:01002F16                 jz      short loc_1002F39

나머지 서브 루틴은 왼쪽과 오른쪽 테두리를 그립니다.

.text:01002F18                 mov     eax, esi
.text:01002F1A                 shl     eax, 5
.text:01002F1D                 lea     edx, MineField?[eax]
.text:01002F23                 lea     eax, (MineField?+1)[eax+ecx]
.text:01002F23
.text:01002F2A
.text:01002F2A loc_1002F2A:                            ; CODE XREF: sub_1002ED5+62j
.text:01002F2A                 sub     edx, 20h
.text:01002F2D                 sub     eax, 20h
.text:01002F30                 dec     esi
.text:01002F31                 mov     byte ptr [edx], 10h
.text:01002F34                 mov     byte ptr [eax], 10h
.text:01002F37                 jnz     short loc_1002F2A
.text:01002F37
.text:01002F39
.text:01002F39 loc_1002F39:                            ; CODE XREF: sub_1002ED5+41j
.text:01002F39                 pop     esi
.text:01002F3A                 retn

WinDBG 명령을 현명하게 사용하면 멋진 지뢰밭 덤프 (사용자 지정 크기 9x9)를 제공 할 수 있습니다. 국경을 확인하십시오!

0:000> db /c 20 01005340 L360
01005340  10 10 10 10 10 10 10 10-10 10 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
01005360  10 0f 0f 0f 0f 0f 0f 0f-0f 0f 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
01005380  10 0f 0f 0f 0f 0f 0f 0f-0f 0f 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
010053a0  10 0f 0f 0f 0f 0f 0f 0f-0f 0f 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
010053c0  10 0f 0f 0f 0f 0f 0f 0f-0f 0f 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
010053e0  10 0f 0f 0f 0f 0f 0f 0f-0f 0f 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
01005400  10 0f 0f 0f 0f 0f 0f 0f-0f 0f 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
01005420  10 0f 0f 0f 0f 0f 0f 0f-0f 0f 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
01005440  10 0f 0f 0f 0f 0f 0f 0f-0f 0f 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
01005460  10 0f 0f 0f 0f 0f 0f 0f-0f 0f 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
01005480  10 10 10 10 10 10 10 10-10 10 10 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
010054a0  0f 0f 0f 0f 0f 0f 0f 0f-0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
010054c0  0f 0f 0f 0f 0f 0f 0f 0f-0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................
010054e0  0f 0f 0f 0f 0f 0f 0f 0f-0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f 0f  ................................

흠, 주제를 닫으려면 다른 게시물이 필요한 것 같습니다.


소스를 분해하려는 것처럼 보이지만 실행중인 프로그램의 메모리 공간을 살펴 보는 것입니다. 16 진 편집기 HxD 에는이를 가능하게하는 기능이 있습니다.

http://www.freeimagehosting.net/uploads/fcc1991162.png

Once you're in the memory space, it is a matter of taking snapshots of the memory while you mess around with the board. Isolate what changes versus what doesn't. When you think you have a handle on where the data structure lies in hex memory, try editing it while it is in memory and see if the board changes as a result.

The process you want is not unlike building a 'trainer' for a video game. Those are usually based on finding where values like health and ammo live in memory and changing them on the fly. You may be able to find some good tutorials on how to build game trainers.


Check out this code project article, it's a little more in depth than the blog post you mentioned.

http://www.codeproject.com/KB/trace/minememoryreader.aspx

Edit

And this article, although not about minesweeper directly, gives you a good step by step guide on hunting through memory using WinDbg:

http://www.codingthewheel.com/archives/extracting-hidden-text-with-windbg

Edit 2

Again, this isn't about minesweeper, but it has definitely given me some food for thought for my memory debugging, there's a wealth of tutorials here:

http://memoryhacking.com/forums/index.php

Also, download CheatEngine (mentioned by Nick D.) and work through the tutorial it comes with.


"In WinDbg I can set breakpoints, but it is difficult for me to imagine at what point to set a breakpoint and at what memory location. Similarly, when I view the static code in IDA Pro, I'm not sure where to even begin to find the function or datastructure that represents the mine field."

Exactly!

Well, you can look for routines like random() that will be called during the construction of the mines table. This book helped me a lot when I was experimenting with reverse engineering. :)

In general, good places for setting break points are calls to message boxes, calls to play a sound, timers and other win32 API routines.

BTW, I am scanning minesweeper right now with OllyDbg.

Update: nemo reminded me a great tool, Cheat Engine by Eric "Dark Byte" Heijnen.

Cheat Engine (CE) is a great tool for watching and modifying other processes memory space. Beyond that basic facility, CE has more special features like viewing the disassembled memory of a process and injecting code into other processes.

(the real value of that project is that you can download the source code -Delphi- and see how those mechanisms were implemented - I did that many years ago :o)


A pretty good article on this very topic can be found at Uninformed. It covers reversing Minesweeper (as an introduction to reverse engineering Win32 apps) in pretty great detail and is all around a pretty great resource.


This website might be more helpful:

http://www.subversity.net/reversing/hacking-minesweeper

The general way to go about doing this is:

  1. Somehow get source code.
  2. Disassemble and hope leftover symbols can help you.
  3. Guess the data type and try to manipulate it and use a memory scanner to limit the possibilities.

In response to Bounty

Well, on a second reading, it appears as though you wanted a guide on how to use a debugger like WinDBG rather than the usual question of how to reverse engineer. I've already shown you the website that tells you the values you need to search for, so the question is, how do you search for it?

I am using Notepad in this example because I do not have Minesweeper installed. But the idea is the same.

대체 텍스트

You type

s <options> <memory start> <memory end> <pattern>

Press "? " and then "s " to see the help.

Once you've found the memory pattern you want, you can then press alt+5 to bring up the memory viewer for a nice display.

대체 텍스트

WinDBG takes some getting used to, but it is as good as any other debugger out there.


A good point to start tracing in debugger would be on mouse up. So find main window procedure (I think tools like spyxx can inspect windows properties and event handler address is one of them). Break in to it and find where it handles mouse events -- there will be a switch, if you can recognize it in assembler (look at value of WM_XXX for mouse up in windows.h).

Put a breakpoint there and start stepping in. Somewhere between the time you released mouse button and screen being updated the victum will access the datastructure you are looking for.

Be patient, try to identify what is being done at any given time, but don't bother looking too deep into code you suspect of being uninteresting for your current objective. It might take several runs in debugger to nail it down.

Knowledge of normal win32 applications workflow helps too.


The mines will probably be stored in some kind of two-dimensional array. This means that it is either an array of pointers or a single C style array of booleans.

Whenever the form receives a mouse-up event this data structure is referenced. The index will be calculated using the mouse coordinate, probably using integer division. That means that you should probably look for a cmp or a similar instruction, where one of the operands is computed using an offset and x, where x is the result of a calculation involving integer division. The offset will then be the pointer to the beginning of the data structure.


It is fairly reasonable to assume that information about mines is layed out contiguously in memory at least for rows (i.e. it's a 2D-array, or an array-of-arrays). Thus, I would try opening several adjacent cells in the same row, making memory dumps of the process as I go, and then diff them and look for any repeating changes in the same memory region (i.e. 1 byte changed on first step, the next byte changed to exact same value on the next step, etc).

There's also possibility that it's a packed bit array (3 bits per mine should be sufficient to record all possible states - closed/open, mine/no-mine, flagged/non-flagged), so I'd look out for that too (the patterns would also be repeatable, though harder to spot). But it's not a convenient structure to deal with, and I don't think memory usage was a bottleneck for Minesweeper, so it is unlikely that this sort of thing would be used.


엄격히 "역 엔지니어의 도구"는 아니고 나 같은 바보라도 사용할 수있는 장난감이 더 많지만 Cheat Engine을 확인하십시오 . 메모리의 어떤 부분이 언제, 언제 변경되었는지 추적하기가 다소 쉬우 며 포인터를 통해 변경된 메모리 부분을 추적 할 수있는 조항도 있습니다 (아마 필요하지 않을 수도 있음). 멋진 대화 형 튜토리얼이 포함되어 있습니다.

참고 URL : https://stackoverflow.com/questions/931257/how-can-i-find-the-data-structure-that-represents-mine-layout-of-minesweeper-in

반응형