龙芯开源社区

 找回密码
 注册新用户(newuser)
查看: 1891|回复: 1

gera提供的一组不安全编程实例

[复制链接]
发表于 2015-8-14 20:42:35 | 显示全部楼层 |阅读模式
gera提供的一组不安全编程实例

http://community.corest.com/~gera/InsecureProgramming/


Insecure Programming by example

Here you can find a collection of exercises that will help you teach yourself the art of insecure programs exploitation. It's not complete, but it's minted to open your mind. The idea is NOT to use any human help. In case you doubt it, we could exploit all but two of them, stay calm and good luck.
get them all
a friend's site with tons of info
pages pointing here
WARMING UP on STACK:
Stack #1:
/* stack1.c                                     *
* specially crafted to feed your brain by gera */

int main() {
        int cookie;
        char buf[80];

        printf("buf: %08x cookie: %08x\n", &buf, &cookie);
        gets(buf);

        if (cookie == 0x41424344)
                printf("you win!\n");
}
Stack #2:
/* stack2.c                                     *
* specially crafted to feed your brain by gera */

int main() {
        int cookie;
        char buf[80];

        printf("buf: %08x cookie: %08x\n", &buf, &cookie);
        gets(buf);

        if (cookie == 0x01020305)
                printf("you win!\n");
}
Stack #3:
/* stack3.c                                     *
* specially crafted to feed your brain by gera */

int main() {
        int cookie;
        char buf[80];

        printf("buf: %08x cookie: %08x\n", &buf, &cookie);
        gets(buf);

        if (cookie == 0x01020005)
                printf("you win!\n");
}
Stack #4:
/* stack4.c                                     *
* specially crafted to feed your brain by gera */

int main() {
        int cookie;
        char buf[80];

        printf("buf: %08x cookie: %08x\n", &buf, &cookie);
        gets(buf);

        if (cookie == 0x000a0d00)
                printf("you win!\n");
}
Stack #5:
/* stack5.c                                     *
* specially crafted to feed your brain by gera */

int main() {
        int cookie;
        char buf[80];

        printf("buf: %08x cookie: %08x\n", &buf, &cookie);
        gets(buf);

        if (cookie == 0x000a0d00)
                printf("you lose!\n");
}
ADVANCED BUFFER OVERFLOWS
Advanced Buffer Overflow #1:
blind obedience
What would happen if you store 512 characters where there is only space for 256? You may claim that you can't, and you'll be right, but still, there are situations that, unconsciously, you tell the micro to do so, and he can only but obey you... and he'll do his best without thinking of side effects. Now is when we get technical, fasten your seat belts, this turbulence will last forever.

What defines a buffer overflow is the copy of a memory region into another region not big enough to contain it.
/* abo1.c                                       *
* specially crafted to feed your brain by gera */

/* Dumb example to let you get introduced...    */

int main(int argv,char **argc) {
        char buf[256];

        strcpy(buf,argc[1]);
}
This is a good and simple abo: on execution this program will copy the contents of argc[1]1, whatever it is, into the reserved 256 bytes named buf, strcpy() will not do any checks of any kind, it will just copy bytes from source to destination, from argc[1] to buf, until it finds a zero. Here, a chance is given for us to supply a longer-than-expected argc[1] to write in memory past the end of the reserved space named buf. Why is this a security problem? becouse we can change data that we shouldn't be able to, and usually, this data we can change has a very special meaning for the micro, and by exploiting this meaning, we can confuse the micro and make it do what we want. That's the secret, go get a debugger, a compiler, and all the tools you think you'll need, and find out what's the data after buf and why it's so important to be able to modify it.

1 - argc and argv are just names for main's arguments, they just name chunks of bits in memory, their names are not meaningful by their own but for their context.

Advanced Buffer Overflow #2:
execution flow
Did you get the previous abo? Is the key to it overwriting sensible data or something else? Again, it is important to know who will read the data, and how will it be interpreted.
/* abo2.c                                       *
* specially crafted to feed your brain by gera */

/* This is a tricky example to make you think   *
* and give you some help on the next one       */

int main(int argv,char **argc) {
        char buf[256];

        strcpy(buf,argc[1]);
        exit(1);
}
In this new abo, as you can see, we added an exit(). Go and find out what's the difference, what new possibilities this exit() adds, or what constrains it puts on the exploitation of the buffer overflow... good luck, take your time, and keep thinking until you are absolutly sure of what you think...

Advanced Buffer Overflow #3:
microprocessor ownership
How to make the microprocessor make what you want? Who owns the Instruction Pointer, owns the execution flow, and that's what we need. All bytes are composed of bits, but some of them are just numbers, and some of them are addresses to code. Jump! Geronimoooooooooo...
/* abo3.c                                       *
* specially crafted to feed your brain by gera */

/* This'll prepare you for The Next Step        */

int main(int argv,char **argc) {
        extern system,puts;
        void (*fn)(char*)=(void(*)(char*))&system;
        char buf[256];

        fn=(void(*)(char*))&puts;
        strcpy(buf,argc[1]);
        fn(argc[2]);
        exit(1);
}
buf is in the stack, and after it are some bits you can change, that you've learnt in abo1.

In case you wonder why we put that there, is so the linker doesn't remove it.

Advanced Buffer Overflow #4:
oh pointers, pointers!
Do you remember when you had problems with * and &? everybody has that kind of problems at least once when learning C, what about poiners to pointers? let's see...
/* abo4.c                                           *
* specially crafted to feed your brain by gera     */

/* After this one, the next is just an Eureka! away */

extern system,puts;
void (*fn)(char*)=(void(*)(char*))&system;

int main(int argv,char **argc) {
        char *pbuf=malloc(strlen(argc[2])+1);
        char buf[256];

        fn=(void(*)(char*))&puts;
        strcpy(buf,argc[1]);
        strcpy(pbuf,argc[2]);
        fn(argc[3]);
        while(1);
}
Advanced Buffer Overflow #5:
ch-ch-ch-changes
/* abo5.c                                       *
* specially crafted to feed your brain by gera */

/* You take the blue pill, you wake up in your bed,    *
*     and you believe what you want to believe        *
* You take the red pill,                              *
*     and I'll show you how deep goes the rabbit hole */

int main(int argv,char **argc) {
        char *pbuf=malloc(strlen(argc[2])+1);
        char buf[256];

        strcpy(buf,argc[1]);
        for (;*pbuf++=*(argc[2]++););
        exit(1);
}
Use your sixth sense, will you be able to gain control given the possibility of writing wherever you wish in memory?

Advanced Buffer Overflow #6:
/* abo6.c                                       *
/* specially crafted to feed your brain by gera */

/* wwwhat'u talkin' about? */

int main(int argv,char **argc) {
        char *pbuf=malloc(strlen(argc[2])+1);
        char buf[256];

        strcpy(buf,argc[1]);
        strcpy(pbuf,argc[2]);
        while(1);
}
Advanced Buffer Overflow #7:
/* abo7.c                                       *
* specially crafted to feed your brain by gera */

/* sometimes you can,       *
* sometimes you don't      *
* that's what life's about */

char buf[256]={1};

int main(int argv,char **argc) {
        strcpy(buf,argc[1]);
}
Advanced Buffer Overflow #8:
Don't stay static
/* abo8.c                                       *
* specially crafted to feed your brain by gera */

/* spot the difference */



char buf[256];

int main(int argv,char **argc) {
        strcpy(buf,argc[1]);
}
From the top of your head, what do you think is generally more safe, a program dynamically linked to its libraries or one statically linked to them? Now go and try it out!

Advanced Buffer Overflow #9:
/* abo9.c                                       *
* specially crafted to feed your brain by gera */

/* free(your mind) */

/* I'm not sure in what operating systems it can be done */

int main(int argv,char **argc) {
        char *pbuf1=(char*)malloc(256);
        char *pbuf2=(char*)malloc(256);

        gets(pbuf1);
        free(pbuf2);
        free(pbuf1);
}
Advanced Buffer Overflow #10:
/* abo10.c                                      *
* specially crafted to feed your brain by gera */

/* Deja-vu*/

char buf[256];

int main(int argv,char **argc) {
        char *pbuf=(char*)malloc(256);

        gets(buf);
        free(pbuf);
}

FORMAT STRINGS
Format Strings #1:
  1. /* fs1.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. /* Don't forget,                                *
  4. * more is less,                                *
  5. * here's a proof                               */

  6. int main(int argv,char **argc) {
  7.         short int zero=0;
  8.         int *plen=(int*)malloc(sizeof(int));
  9.         char buf[256];

  10.         strcpy(buf,argc[1]);
  11.         printf("%s%hn\n",buf,plen);
  12.         while(zero);
  13. }
复制代码


 楼主| 发表于 2015-8-14 20:43:26 | 显示全部楼层
Format Strings #2:
  1. /* fs2.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. /* Can you tell me what's above the edge?       */
  4. int main(int argv,char **argc) {
  5.         char buf[256];

  6.         snprintf(buf,sizeof buf,"%s%c%c%hn",argc[1]);
  7.         snprintf(buf,sizeof buf,"%s%c%c%hn",argc[2]);
  8. }
复制代码


Format Strings #3:
  1. /* fs3.c                                       *
  2. * specially crafted to feed your brain by riq */

  3. /* Not enough resources?                       */

  4. int main(int argv,char **argc) {
  5.         char buf[256];

  6.         snprintf(buf,sizeof buf,"%s%c%c%hn",argc[1]);
  7. }
复制代码


Format Strings #4:
  1. /* fs4.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. /* Have you ever heard about code reusability?  */

  4. int main(int argv,char **argc) {
  5.         char buf[256];

  6.         snprintf(buf,sizeof buf,"%s%6$hn",argc[1]);
  7.         printf(buf);
  8. }
复制代码


Format Strings #5:
  1. /* fs5.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. /* go, go, go!                                  */
  4. int main(int argv,char **argc) {
  5.         char buf[256];
  6.         snprintf(buf,sizeof buf,argc[1]);

  7.                       /* this line'll make your life easier */
  8. //        printf("%s\n",buf);            
  9. }
复制代码


SIGNALS
Signals #1:
  1. /* s1.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. /* now I've got it!                             */

  4. int main(int argv,char **argc) {
  5.         char *pbuf=(char*)malloc(strlen(argc[2])+1);
  6.         char buf[256];

  7.         signal(10,main);
  8.         strcpy(buf,argc[1]);
  9.         for (;*pbuf++=*(argc[2]++););
  10.         while(1);
  11. }
复制代码


Signals #2:
  1. /* s2.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. /* do you resign? */

  4. int main(int argv,char **argc) {
  5.         char *pbuf=(char*)malloc(strlen(argc[2])+1);
  6.         char buf[256];

  7.         signal(10,10);
  8.         strcpy(buf,argc[1]);
  9.         for (;*pbuf++=*(argc[2]++););
  10.         while(1);
  11. }
复制代码


Signals #3:
  1. /* s3.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. /* Give me a sign!!!!                           */

  4. int main(int argv,char **argc) {
  5.         char *pbuf=(char*)malloc(strlen(argc[2])+1);
  6.         char buf[256];

  7.         alarm(1);
  8.         strcpy(buf,argc[1]);
  9.         for (;*pbuf++=*(argc[2]++););
  10.         while(1);
  11. }
复制代码


Signals #4:
  1. /* s4.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. /* recurring nightmare */

  4. int main(int argv,char **argc) {
  5.         char *pbuf=(char*)malloc(strlen(argc[2])+1);
  6.         char buf[256];

  7.         strcpy(buf,argc[1]);
  8.         for (;*pbuf++=*(argc[2]++););
  9.         while(1);
  10. }
复制代码


ESOTERIC
Esoteric #1:
  1. /* e1.c                                         *
  2. /* specially crafted to feed your brain by gera */

  3. /* jumpy vfprintf, Batman! */

  4. int main(int argv,char **argc) {
  5.                       /* Can you do it changing the stack?  */
  6.                       /* Can you do it without changing it? */
  7.         printf(argc[1]);
  8.         while(1);
  9. }
复制代码


Esoteric #2:
  1. /* e2.c                                         *
  2. /* specially crafted to feed your brain by gera */

  3. /* Now, your misson is to make abo1 act like this other program:
  4. *
  5.         char buf[100];

  6.         while (1) {
  7.                 scanf("%100s",buf);
  8.                 system(buf);
  9.         }

  10. * But, you cannot execute code in stack.
  11. */

  12. int main(int argv,char **argc) {
  13.         char buf[256];

  14.         strcpy(buf,argc[1]);
  15. }
复制代码


Esoteric #3:
  1. /* e3.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. /* are you an enviromental threat               */

  4. char buf[256];

  5. int main(int argv,char **argc) {
  6.         strcpy(buf,argc[1]);
  7.         setenv("ABO",argc[2],1);
  8.         while(1);
  9. }
复制代码


Esoteric #4:
  1. /* e4.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. /* %what the hell?                              */

  4. char buf[256];

  5. int main(int argv,char **argc) {
  6.         strcpy(buf,argc[1]);
  7.         printf("live at 100%!");
  8.         while(1);
  9. }
复制代码


Esoteric #5:
  1. /* e5.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. /* is this possible?                            */

  4. char buf[256];

  5. int main(int argv,char **argc) {
  6.         strcpy(buf,argc[1]);
  7.         perror(argc[2]);
  8.         while(1);
  9. }
复制代码


StackGuarded
StackGuarded #1:
  1. /* sg1.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. int func(char *msg) {
  4.     char buf[80];

  5.     strcpy(buf,msg);
  6.     // toupper(buf);        // here just to give func() "some" sence
  7.     strcpy(msg,buf);
  8.     exit(1);
  9. }

  10. int main(int argv, char** argc) {
  11.         func(argc[1]);
  12. }
复制代码


StackGuarded #2:
  1. /* sg2.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. void func(char *msg) {
  4.    char buf[80];
  5.    strcpy(buf,msg);
  6. }

  7. int main(int argv, char** argc) {
  8.    func(argc[1]);
  9. }
复制代码


StackGuarded #3:
  1. /* sg3.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. char *read_it(char *msg) {
  4.     char buf[128];
  5.     int count;

  6.     buf[read(0,buf,sizeof buf)]=0;
  7.     return strdup(buf);
  8. }

  9. int main(int argv, char **argc) {
  10.     char *msg = malloc(1000);

  11.     snprintf(msg,1000,"User: %s",read_it(msg));
  12. }
复制代码


StackGuarded #4:
  1. /* sg4.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. // XXX: Add real encryption here
  4. #define decrypt(dest,src)     strcpy(dest,src)  

  5. int check(char *user) {
  6.         char temp[80];

  7.         decrypt(temp,user);

  8.         // XXX: add some real checks in the future
  9.         return !strcmp(temp,"gera");
  10. }

  11. // XXX: Add real support for internationalization
  12. #define LANG_MSG(dest,pattern) strcpy(dest,pattern);

  13. int main(int argv, char **argc) {
  14.     char msg[100];

  15.     LANG_MSG(msg,"Get out of here!\n");
  16.     if (!check(argc[1])) {
  17.             printf(msg);
  18.             exit(1);
  19.     }
  20.     exit(0);
  21. }
复制代码


StackGuarded #5:
  1. /* sg5.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. int need_to_check = 1;        // XXX: Add global configuration

  4. // XXX: Add real encryption here
  5. #define decrypt(dest,src)     strcpy(dest,src)  

  6. int check(char *user) {
  7.         char temp[80];

  8.         decrypt(temp,user);
  9.        
  10.         // XXX: add some real checks in the future
  11.         return !strcmp(temp,"gera");
  12. }

  13. int main(int argv, char **argc) {
  14.     int user_ok;

  15.     user_ok = check(argc[1]);
  16.     if (!user_ok && need_to_check) exit(1);
  17.     exit(0);
  18. }
复制代码


StackGuarded #6:
  1. /* sg6.c                                        *
  2. * specially crafted to feed your brain by gera */

  3. // XXX: Add real encryption here
  4. #define decrypt(dest,src)     strcpy(dest,src)  

  5. int get_username(char *user) {
  6.         char temp[80];

  7.         decrypt(temp,user);
  8.        
  9.         return strdup(temp);
  10. }

  11. int main(int argv, char **argc) {
  12.     char *user_name;

  13.     user_name = get_username(argc[1]);
  14.     printf("User name is '%s'\n",user_name);
  15.     return 0;
  16. }
复制代码


Numeric
Numeric #1:
  1. /* n1.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. #include
  4. #include
  5. #include

  6. #define MAX_SIZE 80

  7. unsigned int atoul(char *str) {
  8.         unsigned int answer=0;
  9.         for (;*str && isdigit(*str);
  10.                 answer *= 10, answer += *str++-'0');
  11.         return answer;
  12. }

  13. int main(int argv, char **argc) {
  14.         char buf[MAX_SIZE],*pbuf=buf;
  15.         int count = atoul(argc[1]);
  16.        
  17.         if (count >= MAX_SIZE) count = MAX_SIZE-1;

  18.         while (count--) *pbuf++=getchar();
  19.         *pbuf=0;
  20. }
复制代码


Numeric #2:
  1. /* n2.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. #include
  4. #include
  5. #include

  6. #define MAX_SIZE 80

  7. unsigned int atoul(char *str) {
  8.         unsigned int answer=0;
  9.         for (;*str && isdigit(*str);
  10.                 answer *= 10, answer += *str++-'0');
  11.         return answer;
  12. }

  13. int main(int argv, char **argc) {
  14.         char *pbuf,buf[MAX_SIZE];
  15.         int count = atoul(argc[1]);
  16.        
  17.         if (count >= MAX_SIZE) count = MAX_SIZE-1;

  18.         pbuf=buf;
  19.         while (count--) *pbuf++=getchar();
  20.         *pbuf=0;
  21. }
复制代码


Numeric #3:
  1. /* n3.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. #include
  4. #include

  5. unsigned int count;
  6. char **args;

  7. int main(int argv, char **argc) {
  8.         char buf[80];

  9.         fscanf(stdin, "%u", &count);

  10.         args = alloca(count*sizeof(char*));
  11.         while (count--) {
  12.                 if (!fgets(buf,sizeof buf,stdin)) break;
  13.                 *args++=strdup(buf);
  14.         }
  15. }
复制代码


Numeric #4:
  1. /* n4.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. #include
  4. #include

  5. unsigned int count;

  6. int main(int argv, char **argc) {
  7.         char buf[80],**args;
  8.        
  9.         fscanf(stdin, "%u", &count);

  10.         args = alloca(count*sizeof(char*));
  11.         while (count--) {
  12.                 if (!fgets(buf,sizeof buf,stdin)) break;
  13.                 *args++=strdup(buf);
  14.         }
  15. }
复制代码


Numeric #5:
  1. /* n5.c                                         *
  2. * specially crafted to feed your brain by gera */

  3. #include

  4. int main(int argv, char **argc) {
  5.         char **args,buf[80];
  6.         unsigned int index,count;
  7.        
  8.         fscanf(stdin, "%u", &count);
  9.         args = malloc(count*sizeof(char*));

  10.         while (1) {
  11.                 fscanf(stdin,"%u %80s", &index, buf);
  12.                 if (index<count) args[index] = strdup(buf);
  13.                         else break;
  14.         }
  15. }
复制代码

本版积分规则

小黑屋|手机版|Archiver|Lemote Inc.  

GMT+8, 2019-2-21 16:44 , Processed in 0.193275 second(s), 17 queries .

快速回复 返回顶部 返回列表