Archive for the ‘stack’ tag
迷宫找路径——栈的练习
今天下午花了很久的时候做了一个走迷宫的题目。其实这个题目以前上数据结构课的时候都已经写过了,但是再写却又花了很多时间,虽然代码结构和以前写的不一样,但是这却不是最花时间的地方。大部分的时间都花在Debug一些很低级的bug上面,是我状态不好呢,还是写程序实在太烂了呢 Orz。
近来烦心事很多,确实很影响状态。不过就这次的练习来说,还是可以挖出自己很多不好的编程习惯的。总有来说,我要注意:
1. 训练对问题的抽象能力。看到一个问题时,要能够选择出合适的数据结构去把这个问题转化成计算机可以处理的形式。
2. 加强对数据结构的记忆,写逻辑已经先写出数据结构的定义及其上的操作,以后可以方便直接用。
3. 先把问题分析透,用纸笔或者伪代码先把流程写清楚再编码,急于求成只会引入更多的bug。
下面是代码,可以找出所有的路径,很简单哦。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Z 0
#define E 1
#define S 2
#define W 3
#define N 4
#define F 5
#define MAX_STACK_SIZE 1000
/* 0 means clear, 1 means blocked */
#define X 5
#define Y 4
int maze[X][Y] = {
{0, 0, 1, 0},
{1, 0, 0, 1},
{1, 0, 1, 1},
{0, 0, 0, 1},
{1, 0, 0, 0}
};
typedef struct {
int x, y;
int direction;
} coord;
int top = -1;
coord stack[MAX_STACK_SIZE];
#define PUSH(c) \
do { \
if (++top > MAX_STACK_SIZE) { \
printf("Stack overflowed\n"); \
exit(-1); \
} \
stack[top] = c; \
} while (0)
#define POP(c) \
do { \
if (top == -1) { \
printf("Stack underflowed\n"); \
exit(-1); \
} \
c = stack[--top]; \
} while (0)
#define EMPTY() (top == -1)
#define EQUAL(c1, c2) (c1.x==c2.x && c1.y==c2.y)
#define EXISTS(c) exists(c)
#define IS_VALID_MOVE(c) \
( !maze11 && c.x >= 0 && c.x < X && c.y >= 0 && c.y < Y )
int exists(coord c)
{
int i, res = 0;
for (i = 0; i <= top; ++i) {
if (EQUAL(stack[i], c)) {
res = 1;
break;
}
}
return res;
}
int main()
{
coord fini_coord = { 4, 3, 0} , start_coord = { 0, 0, 0 };
int found = 0;
coord t, t1;
int i;
PUSH(start_coord);
while (!EMPTY()) {
t = stack[top];
t1 = t;
if ((EQUAL(t, fini_coord))) {
found++;
printf("Path %d:\n", found);
for (i = 0; i <= top; i++)
printf("(%d, %d)\n", stack[i].x, stack[i].y);
printf("\n\n");
POP(t1);
continue;
}
switch (t.direction) {
case Z:
t.direction = E;
stack[top] = t;
t1 = t;
t1.y += 1;
t1.direction = 0;
if (IS_VALID_MOVE(t1) && !EQUAL(stack[top-1], t1) && !EXISTS(t1))
PUSH(t1);
break;
case E:
t.direction = S;
stack[top] = t;
t1 = t;
t1.x += 1;
t1.direction = 0;
if (IS_VALID_MOVE(t1) && !EQUAL(stack[top-1], t1) && !EXISTS(t1))
PUSH(t1);
break;
case S:
t.direction = W;
stack[top] = t;
t1 = t;
t1.y -= 1;
t1.direction = 0;
if (IS_VALID_MOVE(t1) && !EQUAL(stack[top-1], t1) && !EXISTS(t1))
PUSH(t1);
break;
case W:
t.direction = N;
stack[top] = t;
t1 = t;
t1.x -= 1;
t1.direction = 0;
if (IS_VALID_MOVE(t1) && !EQUAL(stack[top-1], t1) && !EXISTS(t1))
PUSH(t1);
break;
case N:
t.direction = 5;
stack[top] = t;
break;
case F:
POP(t1);
break;
default:
printf("BUG: unknown direction\n");
exit(-1);
}
}
if (!found)
printf("No path found\n");
return 0;
}
Easier said than done
在看PicoWebServer的一个security flaw分析,嗯,典型的栈溢出。虽然多了一个Unicode,但是还是可以做到DoS攻击的。作者的分析也挺不错,把出现问题的地方说得清楚了。
不过末尾有一句话有点大雷了:
An attacker has full control over the device if he is able to let the overwritten return address point to a “0D F0 A0 E1″ (“MOV PC, SP”) equivalent byte sequence. Since SP is the only register pointing into the potential shellcode supplied by an attacker, the aim of an attacker is to let PC equal SP.
还好作者用的是if he is able to,还有点不确定的意思。我是老老实实地用IDA Pro搜索了整个地址空间(是的,“整个”),也没有发现有这样的序列。首先,编译器不会产生这样的代码,那么在代码段内找是徒劳的了。其次,数据段有的话,那真是纯粹巧合了,看人品,而且数据段能不能执行,还是未知之数。
所以啊,总是easier said than done,虽然现在有这样的possibility,但是实际上做不做得到,那还真是认真考证一下才清楚。
师兄也和我说了,搞个什么MessageBox出来,那是不难,但是我们的目标还是要再远一点,要有点实际的东西出来。我也挺认同的,理念上的东西,大家都清楚,但是到底能不能用,可不可以,那还真是要做过才知道。不产生实际危害的漏洞,不是好漏洞(DoS不在考虑之列,这是比较次的危害)。
提一下我们现在的问题是什么:某个软件,接收ANSI字符(0×00-0xFF),然后把接收到的字符转换成Unicode再存储到缓冲区,然后造成了缓冲区溢出。问题是,我们虽然可以改写栈上PC的值,但是这是有限制的(Unicode转换的存在)。如何把控制流改到我们注入到栈上的shellcode中,这需要很多工夫了。目前这是第一步,先不考虑shellcode是否有效,就是要把PC的值搞到栈上就OK了。
这一周基本都在考虑这个问题,也有不少的想法,但是最后都被自己否掉了。罗列如下吧:
- 把要发过去的ANSI串A看作是Unicode,先进行Unicode到ANSI的变换得到B,把B发过去,这样在Server端变换之后,就变成了A。这个想法的致命点是,ANSI只能映射Unicode的一个很小的子集,也就是说,变换过程不是满射的。而且从比例来看,只有1%左右有情况可以逆变换成功。于是有第二个想法。
- 寻找Unicode编码中的闭包子集S,发过去S中的A变换后得到的B仍然在S之内,这样可以很容易通过控制A的生成来得到理想的B。但是现在我还没有找到这样的闭包。即使找到,也只有万里长征第一步,因为这样的S估计不会很大,变换出来的编码有限的话,对于地址的选择就很有讲究了。
- 在其他段寻找如MOV PC, SP这样的序列(也就是上面文章的想法)。不用说,目前失败。
- 构造某个系统调用/API的输入,然后跳转到那个系统调用/API。要求是这个系统调用/API足够强大,可以完成很多功能。但是矛盾的地方是,功能强大的API通常参数是十分复杂的,很难构造。而且,Windows CE的calling convention中,首先使用R0-R4去传送参数,不够才使用栈。怎么让R0-R4符合API的要求?看运气?否掉。
- 从转换函数入手,当然,这个想法很烂。但是也不是说完全没有可能,先留着呗。
现在基本是能想到的都想了,但是总是有这样那样的限制,使得这些想法成为不可能。明天和师兄讨论一下,看他有没有什么其他想法吧。
Security Cookie检测栈溢出
过来中科院的,做的是从来都没有接触过的移动平台安全。师兄给了我两个方向,一个是漏洞挖掘,一个是漏洞利用,叫我先看看资料,看看自己喜欢哪个。
挖掘,主要使用的方法是fuzzing,其实从原理上说还不是特别的难,要写程序也挺容易,所以花在上面的时间不多。利用,师兄说其实和x86上的漏洞利用是差不多的,无非也是溢出。不过因为手机一般是使用ARM的处理器,所以汇编是不大一样的。于是上周基本在资料和小程序实验中试过,主要方面都花在钻手机漏洞利用方法了。
现在要讲的,倒也不是ARM汇编,而是在做实验中发现的一个有趣的东西,叫security cookie,可以检测是否有栈溢出发生。
在返回之前,调用了一个security_check_cookie的函数。
security cookie是编译器的一个feature(当然是可以关掉的)。原理是这样的,在每次进入函数的时候,在缓冲区边界处留出一个存储空间,复制一份security cookie的值,然后在函数返回之前,检查此地址的内容与security cookie是否一致。假如不一致,就说明栈发生了溢出。security cookie是一个magic number,基本上程序每次运行时都是不一样的,要猜中是很难的。即使猜中了,此cookie也会影响shellcode的编写。security cookie的一套代码,都是由编译器自动生成的,因而程序是不需要作什么修改的。
Smashing the stack for fun and profit
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Smashing The Stack For Fun And Profit
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
by Aleph One
aleph1@underground.org
`smash the stack` [C programming] n. On many C implementations
it is possible to corrupt the execution stack by writing past
the end of an array declared auto in a routine. Code that does
this is said to smash the stack, and can cause return from the
routine to jump to a random address. This can produce some of
the most insidious data-dependent bugs known to mankind.
Variants include trash the stack, scribble the stack, mangle
the stack; the term mung the stack is not used, as this is
never done intentionally. See spam; see also alias bug,
fandango on core, memory leak, precedence lossage, overrun screw.

