Page 1 of 1

Simple Security

Posted: Tue Sep 09, 2008 7:10 pm
by Michael Vogel
Hi,
because some programs contain data you may want to hide from other people, it would be a good idea to crypt them. If the data will have to be changed often, it could be the easiest way to encode the exe file after being compiled.

Here is just a simple approach for doing that (because changing the "own" exe file does not work, it was realized with two parts)...

Code: Select all

; Define

	EnableExplicit
	Global i
	Global Zeile.s

	DataSection
		Preamble:
		Data.s "!!!!!!!"
		Magic:
		Data.l 'S*ME'

		SecuredData:
		Data.s "This is a small test"
		Data.s "should only be seen when secured!"
		Data.s "*"

	EndDataSection

; EndDefine

Procedure SecurInit()

	If PeekL(?Magic)='S$ME'
		Restore SecuredData:

		Repeat
			Read Zeile
			If Zeile="*"
				Break
			Else
				i=Len(Zeile)
				While i
					i-1
					PokeB(@zeile+i,PeekB(@Zeile+i)!(((i<<2)*3+(i)*5+7)&$1f))
				Wend
				MessageRequester("...",Zeile)
			EndIf
		ForEver

	Else
		MessageRequester("!","Not Secured")
		End
	EndIf

EndProcedure

SecurInit()
Here the Secure-Program (drag and drop compiled program to this exe):

Code: Select all

; Define

	EnableExplicit

	#AutoDialog=1

	Global Datei.s
	Global Zeile.s

	#Version="SecurMe 1.oo"

	CompilerIf #AutoDialog
		#IconQ=#IDI_QUESTION		; (?)
		#IconH=#IDI_HAND				; (X)
		#IconE=#IDI_EXCLAMATION	; /!\
		#IconI=#IDI_ASTERISK		; (i)
	CompilerElse
		#IconQ=#MB_ICONQUESTION		; (?)
		#IconH=#MB_ICONERROR				; (X)
		#IconE=#MB_ICONEXCLAMATION	; /!\
		#IconI=#MB_ICONINFORMATION	; (i)
	CompilerEndIf

	Enumeration 1
		#NotSecure
		#Secured
	EndEnumeration

; EndDefine

Procedure.w limit(low.w,med.w,high.w)
	If med<low
		ProcedureReturn low
	ElseIf med>high
		ProcedureReturn high
	Else
		ProcedureReturn med
	EndIf
EndProcedure
Procedure Information(text.s,icon.w)

	Protected wait.w
	Protected x,y
	Protected quit
	Protected event

	CompilerIf #AutoDialog
		Select icon
		Case #IconH ; (X)
			wait.w=300
		Case #IconE ; /!\
			wait.w=50
		Case #IconI ; (i)
			wait.w=25
		Default
			wait.w=#MAXSHORT
		EndSelect

		Define mouse.point

		#Hoehe=64 ; mit Ok-Knopf: 96

		GetCursorPos_(@mouse)
		x=limit(0,(mouse\x)-130,GetSystemMetrics_(#SM_CXSCREEN)-260)
		y=limit(0,(mouse\y)-100,GetSystemMetrics_(#SM_CYSCREEN)-#hoehe)

		;#PB_Window_ScreenCentered | #PB_Window_SystemMenu
		; mit Ok-Button: 260,96
		If OpenWindow(1,x,y,272,#hoehe,#Version,#PB_Window_SystemMenu|#PB_Window_Invisible)

			CreateGadgetList(WindowID(1))
			ImageGadget  (1, 12, 14, 0, 0, LoadIcon_(0,icon)); LoadImage(0,"c:\ico.ico"))

			TextGadget(2,60,16,250,30,text)
			;		ButtonGadget(3,90,64,80,22,"&Ok")
			DisableGadget(1,1)
			;		SetActiveGadget(3)
			HideWindow(1, 0)

			quit=0
			SetTimer_(WindowID(1),1,80,0) ; Default: 100ms
			Repeat
				event=WaitWindowEvent()
				Select event
				Case #PB_Event_CloseWindow,#PB_Event_Gadget
					quit=#MAXSHORT
				Case #WM_TIMER
					quit+1
					;SetGadgetText(2,Str(quit))
				Case #WM_CHAR
					Select  EventwParam()
					Case 13,32,27
						quit=#MAXSHORT
					EndSelect
					;Default
					;SetGadgetText(2,Str(event))
				EndSelect
			Until quit>wait

			CloseWindow(1)
		EndIf
		;End
	CompilerEndIf

EndProcedure
Procedure Meldung(nr.w,typ.w=0)

	Enumeration
		#Ok
		#ErrorWriteprotected
		#ErrorNoParameter
		#ErrorIllegalParameter
		#ErrorKeyNotFound
		#ErrorAlreadySecure
	EndEnumeration

	Protected icon.w=#IconH				; (X)

	Protected text.s="Die Datei '"+GetFilePart(Datei)+"'"+#CR$

	Select nr

	Case #Ok
		icon=#IconI		; (i)
		text+"wurde nun verschlüsselt!"

	Case #ErrorAlreadySecure
		icon=#IconE		; /i\
		text+"ist bereits verschlüsselt!"

	Case #ErrorKeyNotFound
		text+"enthält keinen SecurMe-Schlüssel!"

	Case #ErrorWriteprotected
		text+"ist schreibgeschützt!"

	Case #ErrorIllegalParameter
		icon=#IconE
		text+"ist nicht vorhanden oder ungültig!"

	Case #ErrorNoParameter
		icon=#IconE
		text="SecurMe benötigt einen gültigen"+#CR$+"Dateinamen als Parameter..."

	EndSelect

	CompilerIf #AutoDialog
		Information(text,icon)
	CompilerElse
		MessageBox_(0,text,#Version, icon | #MB_OK)
	CompilerEndIf

EndProcedure

Procedure Main()

	Protected i
	Protected pos
	Protected found
	Protected hash
	Protected size

	If CountProgramParameters()=1

		datei=ProgramParameter()
		size=FileSize(Datei)
		pos=0
		found=0
		hash=0

		If (Right(LCase(datei),4)=".exe") And (size>10)

			If OpenFile(0,Datei)
				Repeat

					hash=ReadLong(0)
					pos+4

					If hash='!!!!'
						Repeat
							hash=ReadByte(0)
							pos+1
						Until hash<>'!'

						hash=ReadLong(0)
						pos+4
						If hash='S*ME'
							found=#NotSecure
						ElseIf hash='S$ME'
							found=#Secured
						EndIf
					EndIf
				Until found Or (pos>=size)

				Select found

				Case #Secured
					meldung(#ErrorAlreadySecure)


				Case #NotSecure

					FileSeek(0,Loc(0)-4)
					WriteLong(0,'S$ME')

					Repeat
						pos=Loc(0)
						zeile=ReadString(0)

						If zeile="*"
							Break
						Else
							i=Len(Zeile)
							While i
								i-1
								PokeB(@zeile+i,PeekB(@Zeile+i)!(((i<<2)*3+(i)*5+7)&$1f))
							Wend
							FileSeek(0,pos)
							WriteString(0,zeile)
							WriteByte(0,0)
						EndIf
					ForEver

					meldung(#Ok)

				Default
					meldung(#ErrorKeyNotFound)

				EndSelect

				CloseFile(0)

			Else
				meldung(#ErrorWriteprotected)
			EndIf

		Else
			meldung(#ErrorIllegalParameter)
		EndIf

	Else
		meldung(#ErrorNoParameter)
	EndIf

EndProcedure

Main()

Posted: Tue Sep 09, 2008 8:36 pm
by Mistrel
This only prevents people from hex editing your program, doesn't it? The data is still unencrypted at runtime.

Posted: Wed Sep 10, 2008 7:18 am
by Michael Vogel
Mistrel wrote:This only prevents people from hex editing your program, doesn't it? The data is still unencrypted at runtime.
No, no (at least here :?)

When you drag the compiled example program over the SecurMe exe, it will be encrypted and can't be (easily) encoded with a hex editor.

But you're right: if you have something like a memory explorer, you will see all data - so this should be only possible after entering a password etc.

I know, it is not high secure but should prevent easily removing copyright messages by changing the exe file with a resource/hex editor.

Michael