Contribute  :  Web Resources  :  Past Polls  :  Site Statistics  :  Downloads  :  Forum  
    BiW ReversingThe challenge is yours    
 Welcome to BiW Reversing
 Tuesday, March 31 2020 @ 09:08 PM CEST

A simple keygenerator template

   

CodingLevel : beginner

OS : windows
Language : ASM

This article will show you how to create a simple dialogbox keygen,
featuring an image, skinned buttons and dialog and an aboutbox.



The comments in the code are limited, if you have trouble understanding a part,
use the msdn to look up the API's or post a comment for other questions.

The approach

This sample uses a resource script to describe how our dialogs will look like.
The script also holds all the images, icons, pointers,... we want to use.
It is also possible to create the entire dialogbox dynamically in the code,
but in this example we chose the easiest way ;)

The resource script

There are visual editors to modify/create resource scripts, but this one is so small it
can easily be made by hand.
The script holds the description of 2 dialogboxes (our main dialogbox and a small aboutbox),
an icon, and 3 images.

Lets not waste any time, here is the resource script (rsrc.rc)

#include "resource.h"

#define DIALOG1 	   1
#define DIALOG2		2


DIALOG1 DIALOG 0, 0, 195, 110
STYLE 0x0004 | DS_CENTER | WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE
EXSTYLE  WS_EX_TOOLWINDOW | WS_EX_TOPMOST
CAPTION "BiW Keygen"
FONT 8, "MS Sans Serif"
BEGIN
 CONTROL 22, -1, "Static", SS_BITMAP | WS_CHILD | WS_VISIBLE, 7,2, 0, 0
 LTEXT "Name", 104, 5, 65, 30, 8
 LTEXT "Serial",105, 5, 80, 30, 8
 EDITTEXT 101, 33, 65, 140, 12, ES_AUTOHSCROLL | WS_TABSTOP
 EDITTEXT 103, 33, 80, 140, 12, ES_AUTOHSCROLL | ES_READONLY | WS_TABSTOP
 PUSHBUTTON "Generate", 1001, 33, 95, 43, 14, BS_PUSHBUTTON  | BS_FLAT | WS_CHILD | WS_VISIBLE | WS_TABSTOP
 PUSHBUTTON "About", 1002, 85, 95, 43, 14, BS_PUSHBUTTON | BS_FLAT | WS_CHILD | WS_VISIBLE | WS_TABSTOP
END

DIALOG2 DIALOG 0, 0, 160, 50
STYLE 0x0004 | DS_CENTER | WS_POPUPWINDOW | WS_VISIBLE
EXSTYLE  WS_EX_TOOLWINDOW | WS_EX_TOPMOST
CAPTION "About"
FONT 8, "MS Sans Serif"
BEGIN
 GROUPBOX "", -1, 2, 2, 156, 45, BS_GROUPBOX | BS_FLAT | WS_CHILD | WS_VISIBLE
 LTEXT "", 106, 30, 10, 100, 20, ES_CENTER
 LTEXT "", 107, 30, 35, 100, 8, ES_CENTER
END

21 ICON "ICOkeyg.ico"
22 BITMAP "main.bmp"
23 BITMAP "generate.bmp"
24 BITMAP "about.bmp"


If you want more information about resource scripts, have a look at win32asm.


The source code

Without further delay the template.asm file containing all the source code for the keygen :

.386
.Model flat, stdcall
option casemap:none

include masm32includewindows.inc
include masm32includekernel32.inc
include masm32includeuser32.inc
include masm32includegdi32.inc
include masm32includeshell32.inc

includelib masm32libkernel32.lib
includelib masm32libuser32.lib
includelib masm32libgdi32.lib
includelib masm32libshell32.lib

wndproc PROTO :DWORD, :DWORD, :DWORD, :DWORD
aboutwndproc PROTO :DWORD, :DWORD, :DWORD, :DWORD
GenerateKey PROTO :DWORD, :DWORD, :DWORD

.data
whattodo       db "Insert your name and press generate.",0
captionbar     db "BiW Keygen - Some app",0
abcaptionbar   db "About - Some appr",0
caption1       db "Name :",0
caption2       db "Serial :",0
ab1            db "This keygen is brought to you",13,10
               db "by BiW.",0
ab2            db "Coded in pure win32asm ;)",0
noname         db "Please insert your name first!",0
ahex           db "%X",0
;---------------------------------------------------------------------
; Variables for the keygen algorithm
;---------------------------------------------------------------------
hash1          qword 04058C00000000000h
hash2          qword 03FB999999999999Ah
hash3          qword 043E181184A35A128h
zero1          qword 0h

.data?
hInstance     HINSTANCE ?
MyArrow       DWORD ?
temp          DWORD ?
username      db 100 dup (?)
userserial    db 100 dup (?)
regcode       db 100 dup (?)

.const
; Here we define some easier to remember names for the resource identifiers.
ICON1         equ 21
PICGEN        equ 23
PICABOUT      equ 24
DIALOG1       equ 1
DIALOG2       equ 2
IDC_NAME      equ 101
IDC_SERIAL    equ 103
CAP1          equ 104
CAP2          equ 105
ABOUT1        equ 106
ABOUT2        equ 107
URL1          equ 108
ID_GENERATE   equ 1001
ID_ABOUT      equ 1002


.code
start:
invoke GetModuleHandle, NULL
mov hInstance, eax
invoke DialogBoxParam, eax, DIALOG1, NULL, addr wndproc, NULL
invoke ExitProcess, NULL

;---------------------------------------------------------------------
; MessageLoop MainDialog
;---------------------------------------------------------------------
wndproc PROC hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

.if uMsg==WM_CLOSE
	invoke EndDialog, hWnd, NULL

.elseif uMsg==WM_INITDIALOG
	invoke SetWindowText, hWnd, ADDR captionbar ; Set caption
	invoke SendDlgItemMessage, hWnd, IDC_SERIAL, WM_SETTEXT, NULL, ADDR whattodo ; Set text in serialbox
	invoke LoadBitmap, hInstance, 23 ; Load image with identifier 23 in rc file
	invoke SendDlgItemMessage, hWnd, ID_GENERATE, BM_SETIMAGE, IMAGE_BITMAP, eax ; place image on button
	invoke LoadBitmap, hInstance, 24 ; Load image with identifier 24 in rc file
	invoke SendDlgItemMessage, hWnd, ID_ABOUT, BM_SETIMAGE, IMAGE_BITMAP, eax ; place image on button

	invoke LoadIcon, hInstance, ICON1 ; load the icon
	invoke SendMessage, hWnd, WM_SETICON, DIALOG1, eax ; set icon for dialogbox
	invoke SendDlgItemMessage, hWnd, CAP1, WM_SETTEXT, NULL, addr caption1 ; set label
	invoke SendDlgItemMessage, hWnd, CAP2, WM_SETTEXT, NULL, addr caption2 ; set label
	
.elseif uMsg==WM_LBUTTONDOWN ; make dialog draggable
	mov eax, lParam 
	call ReleaseCapture
	invoke SendMessage, hWnd, WM_NCLBUTTONDOWN, HTCAPTION, NULL

.elseif uMsg==WM_CTLCOLORDLG ; custom color for the dialogbox
	invoke CreateSolidBrush, 0EB9746h
	ret

.elseif uMsg==WM_CTLCOLORSTATIC ; custom color for the labels
	mov eax, lParam
	invoke SetTextColor, wParam, White
	invoke SetBkMode, wParam, TRANSPARENT
	invoke CreateSolidBrush, 0EB9746h
	invoke SetBkColor, wParam, eax
	invoke CreateSolidBrush, 0EB9746h
        ret

.elseif uMsg==WM_CTLCOLOREDIT ; custom color for the editboxes
	mov eax, lParam
	invoke SetTextColor, wParam, White
	;invoke CreateSolidBrush, 0EB9746h
	invoke SetBkColor, wParam, 0EB9746h
	invoke CreateSolidBrush, 0EB9746h
	ret

.elseif uMsg==WM_COMMAND
     	mov eax,wParam
     	mov edx,wParam
     	shr edx,16
     	.IF dx == BN_CLICKED
		.if ax == ID_GENERATE ; Generate button clicked
			invoke SendDlgItemMessage, hWnd, IDC_NAME, WM_GETTEXT, 201, addr username
			test eax, eax
			jnz proceed
			invoke SendDlgItemMessage, hWnd, IDC_SERIAL, WM_SETTEXT, NULL, addr noname
			jmp skip_algo
			proceed:
			invoke GenerateKey, addr username, addr userserial, eax // GenerateKey(name, serial, namelength)
			invoke SendDlgItemMessage, hWnd, IDC_SERIAL, WM_SETTEXT, NULL, addr userserial
                        skip_algo:

                .elseif ax == ID_ABOUT ; about button clicked
                	invoke DialogBoxParam, hInstance, DIALOG2, hWnd, addr aboutwndproc, NULL
                .endif
	.endif
.else
mov eax, FALSE
ret
.endif
mov eax, TRUE
ret
wndproc ENDP


;---------------------------------------------------------------------
; MessageLoop AboutDialog
;---------------------------------------------------------------------

aboutwndproc PROC ahWnd:HWND, auMsg:UINT, awParam:WPARAM, alParam:LPARAM

.if auMsg==WM_INITDIALOG
	invoke SetWindowText, ahWnd, ADDR abcaptionbar
	invoke SendDlgItemMessage, ahWnd, ABOUT1, WM_SETTEXT, NULL, addr ab1
	invoke SendDlgItemMessage, ahWnd, ABOUT2, WM_SETTEXT, NULL, addr ab2

.elseif auMsg==WM_CLOSE
invoke EndDialog, ahWnd, NULL

.elseif auMsg==WM_LBUTTONDOWN
	mov eax, alParam
	call ReleaseCapture
	invoke SendMessage, ahWnd, WM_NCLBUTTONDOWN, HTCAPTION, NULL

.elseif auMsg==WM_CTLCOLORDLG
	invoke CreateSolidBrush, 0EB9746h
	ret

.elseif auMsg==WM_CTLCOLORSTATIC
	mov eax, alParam
	invoke SetTextColor, awParam, White
	invoke SetBkMode, awParam, TRANSPARENT
	invoke CreateSolidBrush, 0EB9746h
	invoke SetBkColor, awParam, eax
	invoke CreateSolidBrush, 0EB9746h
	ret

.elseif auMsg==WM_COMMAND
     	mov eax,awParam
     	mov edx,awParam
     	shr edx,16
     	.IF dx == BN_CLICKED
	.endif

.else
	mov eax, FALSE
	ret
.endif
	mov eax, TRUE
	ret
aboutwndproc ENDP



;-------------------------------------------------------------------+
;                    key-generation procedure, replace with yours   |
;-------------------------------------------------------------------+
GenerateKey PROC aname:DWORD, aserial:DWORD, alength:DWORD
LOCAL temp1:DWORD  		;local vars
LOCAL serial:QWORD
push edx						;algorithm
push ecx
xor ecx, ecx
mov edx, [aname]
fld qword ptr [zero1]
loop_on:
	xor eax, eax
	mov al, byte ptr [edx+ecx]
	and al, 7Fh
	mov [temp1], eax
	fild dword ptr [temp1]
	faddp st(1), st
	fmul qword ptr [hash1]
	inc ecx
	cmp ecx, [alength]
	jb loop_on

fmul qword ptr [hash2]
fmul qword ptr [hash3]
fabs
fstp qword ptr [serial] 	; end algorithm

invoke wsprintf, ADDR userserial, ADDR ahex, qword ptr [serial] ; store serial in userserial var as ascii
pop ecx
pop edx
ret
GenerateKey ENDP

end start

Don't copy any code from within this article, all source is added in the following attachment (due to restrictions some characters (eg backslashes) are removed from the article.)




What's Related

Story Options

A simple keygenerator template | 0 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
 Copyright © 2020 BiW Reversing
 All trademarks and copyrights on this page are owned by their respective owners.
Powered By Geeklog 
Created this page in 0.76 seconds