-
-
[分享]pwnable.kr BlackJack day10
-
发表于: 2021-1-12 23:01 9076
-
这是一个类似 21 点的游戏。
初始现金是 500,我们输入赌注(bet),如果赌注比拥有的现金(cash)大的数会给错误提示。
现金超过一百万可以看到 flag。
源码过大,已上传至附件。
核心函数有 betting() 和 play()
betting() 是我们下注的函数
可以看到输入 bet 后,没有对 bet 值是否合法做检测,全都返回 bet。
play() 是游戏的核心函数
重点如下
可以看出当我们赢的时候,我们的现金变为现金 + 赌注,输的时候变为现金 - 赌注。
由 betting() 知,赌注的输入没有做足够的限制,所以我们可以输入一个较大负数,然后故意输掉,这样我们的金额会直接超过一百万,达到看见 flag 的要求,继续玩一局即可看到 flag。
红框内为 flag
void play()
/
/
Plays game
{
int
p
=
0
;
/
/
holds value of player_total
int
i
=
1
;
/
/
counter
for
asking user to hold
or
stay (aka game turns)
char choice3;
cash
=
cash;
cash_test();
printf(
"\nCash: $%d\n"
,cash);
/
/
Prints amount of cash user has
randcard();
/
/
Generates random card
player_total
=
p
+
l;
/
/
Computes player total
p
=
player_total;
printf(
"\nYour Total is %d\n"
, p);
/
/
Prints player total
dealer();
/
/
Computes
and
prints dealer total
betting();
/
/
Prompts user to enter bet amount
while
(i<
=
21
)
/
/
While loop used to keep asking user to hit
or
stay at most twenty
-
one times
/
/
because there
is
a chance user can generate twenty
-
one consecutive
1
's
{
if
(p
=
=
21
)
/
/
If user total
is
21
, win
{
printf(
"\nUnbelievable! You Win!\n"
);
won
=
won
+
1
;
cash
=
cash
+
bet;
printf(
"\nYou have %d Wins and %d Losses. Awesome!\n"
, won, loss);
dealer_total
=
0
;
askover();
}
if
(p>
21
)
/
/
If player total
is
over
21
, loss
{
printf(
"\nWoah Buddy, You Went WAY over.\n"
);
loss
=
loss
+
1
;
cash
=
cash
-
bet;
printf(
"\nYou have %d Wins and %d Losses. Awesome!\n"
, won, loss);
dealer_total
=
0
;
askover();
}
if
(p<
=
21
)
/
/
If player total
is
less than
21
, ask to hit
or
stay
{
printf(
"\n\nWould You Like to Hit or Stay?"
);
scanf(
"%c"
, &choice3);
while
((choice3!
=
'H'
) && (choice3!
=
'h'
) && (choice3!
=
'S'
) && (choice3!
=
's'
))
/
/
If invalid choice entered
{
printf(
"\n"
);
printf(
"Please Enter H to Hit or S to Stay.\n"
);
scanf(
"%c"
,&choice3);
}
if
((choice3
=
=
'H'
) || (choice3
=
=
'h'
))
/
/
If Hit, continues
{
randcard();
player_total
=
p
+
l;
p
=
player_total;
printf(
"\nYour Total is %d\n"
, p);
dealer();
if
(dealer_total
=
=
21
)
/
/
Is dealer total
is
21
, loss
{
printf(
"\nDealer Has the Better Hand. You Lose.\n"
);
loss
=
loss
+
1
;
cash
=
cash
-
bet;
printf(
"\nYou have %d Wins and %d Losses. Awesome!\n"
, won, loss);
dealer_total
=
0
;
askover();
}
if
(dealer_total>
21
)
/
/
If dealer total
is
over
21
, win
{
printf(
"\nDealer Has Went Over!. You Win!\n"
);
won
=
won
+
1
;
cash
=
cash
+
bet;
printf(
"\nYou have %d Wins and %d Losses. Awesome!\n"
, won, loss);
dealer_total
=
0
;
askover();
}
}
if
((choice3
=
=
'S'
) || (choice3
=
=
's'
))
/
/
If Stay, does
not
continue
{
printf(
"\nYou Have Chosen to Stay at %d. Wise Decision!\n"
, player_total);
stay();
}
}
i
+
+
;
/
/
While player total
and
dealer total are less than
21
, re
-
do
while
loop
}
/
/
End While Loop
}
/
/
End Function
void play()
/
/
Plays game
{
int
p
=
0
;
/
/
holds value of player_total
int
i
=
1
;
/
/
counter
for
asking user to hold
or
stay (aka game turns)
char choice3;
cash
=
cash;
cash_test();
printf(
"\nCash: $%d\n"
,cash);
/
/
Prints amount of cash user has
randcard();
/
/
Generates random card
player_total
=
p
+
l;
/
/
Computes player total
p
=
player_total;
printf(
"\nYour Total is %d\n"
, p);
/
/
Prints player total
dealer();
/
/
Computes
and
prints dealer total
betting();
/
/
Prompts user to enter bet amount
while
(i<
=
21
)
/
/
While loop used to keep asking user to hit
or
stay at most twenty
-
one times
/
/
because there
is
a chance user can generate twenty
-
one consecutive
1
's
{
if
(p
=
=
21
)
/
/
If user total
is
21
, win
{
printf(
"\nUnbelievable! You Win!\n"
);
won
=
won
+
1
;
cash
=
cash
+
bet;
printf(
"\nYou have %d Wins and %d Losses. Awesome!\n"
, won, loss);
dealer_total
=
0
;
askover();
}
if
(p>
21
)
/
/
If player total
is
over
21
, loss
{
printf(
"\nWoah Buddy, You Went WAY over.\n"
);
loss
=
loss
+
1
;
cash
=
cash
-
bet;
printf(
"\nYou have %d Wins and %d Losses. Awesome!\n"
, won, loss);
dealer_total
=
0
;
askover();
}
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)
赞赏
他的文章
- [原创]pwnable.kr horcruxes 10754
- [分享] pwnable.kr blukat 9974
- [分享] pwnable.kr unlink 9473
- [分享] pwnable.kr asm 10671
- [分享] pwnable.kr memcpy 10450
谁下载
无
看原图
赞赏
雪币:
留言: