BlackJack in Python

2013. 5. 12. 19:04 from Programming


#BlackJack rules can be found here http://www.114pda.com/game/card/blackjack_rule.htm

import os import random def game(): os.system("cls") print "WELCOME TO PYTHON BLACKJACK" Cards = [1,2,3,4,5,6,7,8,9,10,11,12,13,14] computerHand = deal() playerHand = deal() print "-------START GAME-------" print "Computer Hand: " + str(computerHand[0]) print "Player Hand: " + str(playerHand) print "\n-------Current Score-----" if(computeScore(playerHand) == 21): print "21! You win!" message() elif(computeScore(computerHand) == 21): print "21! Computer Wins!" message() print computeScore(playerHand) cardHit = raw_input("Do you want to hit (Y/N): ") while(cardHit == "y" or cardHit == "Y" ): hit(playerHand) print "\n------NEW HAND SCORE------" print playerHand print computeScore(playerHand) if(len(playerHand) == 5 and computeScore(playerHand) < 21): print "Less than 5 and Under 21: YOU WIN " message() elif(bust(playerHand) == 1): print "BUST. You lose" message() else: cardHit = raw_input("Do you want to hit (Y/N): ") print "\n-------Computer Turn------" if(computeStrategy(computerHand) == "Hit"): hit(computerHand) print computerHand else: print "I'm done." print computerHand print "\n--------DETERMINE WINNER------" print "COMPUTER: " + str(computeScore(computerHand)) print "PLAYER : " + str(computeScore(playerHand)) if(winner(computerHand,playerHand) == "hand1" and bust(computerHand)==0): print "Computer won" elif(winner(computerHand,playerHand) == "hand2" and bust(playerHand) == 1): print "Computer won" elif(winner(computerHand,playerHand) == "hand1" and bust(computerHand) == 1): print "Player Won" elif(winner(computerHand,playerHand) == "hand2" and bust(playerHand) == 0): print "Player Won" message() def message(): again = raw_input("Do you want to play again? (Y/N) : ") if(again == "Y" or again == "y"): game() else: print "\n\n-------Thank you for playing!--------\n\n" exit() def deal(): random1 = random.randint(1,14) random2 = random.randint(1,14) if (random1 == 11):random1 = "J" if (random1 == 12):random1 = "Q" if (random1 == 13):random1 = "K" if (random1 == 14):random1 = "A" if (random2 == 11):random2 = "J" if (random2 == 12):random2 = "Q" if (random2 == 13):random2 = "K" if (random2 == 14):random2 = "A" hand = [random1,random2] return hand def computeScore(hand): total = 0 for cards in hand: if cards == "J" or cards == "Q" or cards == "K": total+= 10 elif cards == "A": if total == 11: total+= 1 else:total+= 11 else: total += cards return total def hit(hand): newCard = random.randint(1,14) if (newCard == 11):newCard = "J" if (newCard == 12):newCard = "Q" if (newCard == 13):newCard = "K" if (newCard == 14):newCard = "A" hand.append(newCard) return hand def computeStrategy(hand): strategy = "" if (computeScore(hand) > 15): strategy = "Stay" if (computeScore(hand) <=15): strategy = "Hit" return strategy def winner(hand1,hand2): score1 = computeScore(hand1) score2 = computeScore(hand2) if score1 > 21: return "hand2" if score2 > 21: return "hand1" if(score1 > score2): return "hand1" else: return "hand2" def bust(hand): if computeScore(hand) >= 21: return 1 else: return 0 if __name__ == "__main__": game()


'Programming' 카테고리의 다른 글

[dovelet]-crypt  (0) 2014.01.20
os 판별 코드  (0) 2013.10.11
Binary Search  (0) 2013.07.18
좋은 사이트..  (0) 2013.07.13
c++ 급여 관리 시스템  (0) 2013.07.09
Posted by xer0s :

유용한 자료들..

2013. 4. 22. 23:59 from Security

최근들어 시험기간에다가 여러 바쁜 일들이 겹쳐서 포스팅을 못했습니다..

SQLI 감을 잃지 않으려고 자료들을 뒤져보면서 읽던 도중에 괜찮은 자료를 발견해서 공유합니다.


http://packetstormsecurity.org/files/view/105600/beyond-sqli.txt

http://websec.ca/kb/sql_injection


sqli2.pdf


시험 후에 포스팅 한번 하겠습니다.

감사합니다. 


'Security' 카테고리의 다른 글

허..  (0) 2013.10.17
php extract vulnerability  (0) 2013.10.13
RTL시 pop pop ret, exit 함수 호출의 원리  (2) 2013.09.26
[Ollydbg 1.x] OllyMSDN plugin  (0) 2013.07.11
Duelist crackme #1 풀이  (0) 2013.03.10
Posted by xer0s :

Duelist crackme #1 풀이

2013. 3. 10. 19:59 from Security

리버싱 연습도 할겸 크랙미를 풀고 있습니다.

많은 크랙미들중에서도 이 Duelist crakme series는 초보자들에게 매우 도움이 될꺼 같은 크랙미더군요.

아래는 크랙미 파일 원본입니다.


압축을 풀고 exe파일이라서 먼저 실행을시켜 봤습니다.

특정 코드의 입력을 요구하고 있고 안봐도 아시겠지만 코드의 값이 틀리면 다시 시도하라는 메세지박스가 뜹니다.


본격적인 분석을 위해 OllyDbg로 크랙미를 열어보았습니다.

처음 열었을때 나타나느 코드들입니다.

스크롤바를 내리면서 쭉 코드들을 봐봤을때 다음과 같은 API를 확인할수 있었습니다.

저는 처음 리버싱을 배울때는 API들을 하나하나 다 알아가면서 크랙미를 풀 필요가 있다고 생각하기 때문에 우선 이 API를 알아

봤습니다. 

WM_GETTEXT 메시지를 컨트롤로 보내 컨트롤의 텍스트를 읽어 lpString 버퍼에 채워준다. 이때 컨트롤은 버튼, 에디트, 스태틱 등의 텍스트 표현이 가능한 컨트롤이어야 한다. 만약 버퍼 길이(nMaxCount)보다 문자열이 더 길면 문자열은 잘려진다.

GetDlgltemText A 에 대한 설명중 일부분입니다.

이 설명대로라면 버퍼의 크기는 0x24이고 버퍼의 시작주소는 004020f7입니다.

실제로 004020f7에 있는 값들을 봐보면 저희가 입력햇던 값이 저장이 되는군요


GetDlgltemText A 이후부터는 본격적인 연산이 시작되는 코드입니다.

입력한 값을 가지고 연산을 하는 부분인데 연산에 관한 설명은 다 아시리라 믿고 생략하겠습니다.

위의 연산들을 다 수행하고 eax랑 0이랑 같을때 jnz short due-cm1, 0040114C 명령을 수행하게 되는데 

아래가 0040114c에 있는 코드들입니다.

00401158의 call 명령을 수행하기전에 3개의 인자를 전해주는것 같은데 004020f7이 버퍼의 주소라는 걸 눈치챌수 있었습니다.

0040114e의 값이 뭔지 알아보기위해 call 함수를 따라 들어가봤습니다.

이 부분을 유심히 바라보다가 call명령어가 끝나는부분을 봐봤었습니다.

이 코드에서 eax를 0과 다르게 해줘야 된다는걸 알수 있었는데 eax가 최근에 변한곳을 찾아보니 004011d8이였습니다.

이 부분을 건너뛸수 있는 방법이 004011d3에서 문자열을 비교할때 틀리지 않고 비교를 마친다면 jcxz 명령어때문에 1인 eax를 0

으로 바꿔줄 필요 없이 바로 함수의 종착점으로 가는것입니다.

004011d3 명령어의 비교대상이 edi와 esi이고 edi는 arg1 esi는 arg2이므로 최종적으로는 004020f7의 데이터와 004020d3의 데이

터를 비교해줫을때 같게 해주면 되는것입니다. 

이에 맞게 코딩을 해주면

#include <stdio.h>

#include <stdlib.h>


int main(void)

{


    int i;

    int a[35] = {0x7B, 0x61, 0x65, 0x78, 0x64, 0x6D, 0x26, 0x6B, 0x7A, 0x69, 0x6B, 0x63, 0x65, 0x6D, 0x26, 0x3C, 0x26,

    0x66, 0x6D, 0x7F, 0x6A, 0x61, 0x6D, 0x7B, 0x26, 0x6A, 0x71, 0x26, 0x6C, 0x7D, 0x6D, 0x64, 0x61, 0x7B, 0x7c};

    int b[35];

    

    printf("##########################\n");

    printf("#  Duelist crackme #1    #\n");

    printf("#              by xer0s  #\n");

    printf("##########################\n\n\n");

    printf("The Key is : ");


    for (i=0; i<35; i++)

    {

        b[i] = a[i] ^ 0x43;

        b[i] = b[i] ^ 0x1E;

        b[i] = b[i] ^ 0x55;

        printf("%c", b[i]);

    }

    system("pause");

    return 0;

}


The Key is : simple.crackme.4.newbies.by.duelist

코딩 잘못해서 한참 해멧엇네요..

풀어서 기분은 좋군요 ㅎ


'Security' 카테고리의 다른 글

허..  (0) 2013.10.17
php extract vulnerability  (0) 2013.10.13
RTL시 pop pop ret, exit 함수 호출의 원리  (2) 2013.09.26
[Ollydbg 1.x] OllyMSDN plugin  (0) 2013.07.11
유용한 자료들..  (0) 2013.04.22
Posted by xer0s :