[NSSCTF 2022 Spring Recruit]easy C 较为详细题解

0 阅读1分钟

[NSSCTF 2022 Spring Recruit]easy C

Problem: [NSSCTF 2022 Spring Recruit]easy C

1.题干

#include <stdio.h>
#include <string.h>

int main(){
    char a[]="wwwwwww";
    char b[]="d`vxbQd";

    //try to find out the flag
    printf("please input flag:");
    scanf(" %s",&a);

    if(strlen(a)!=7){
        printf("NoNoNo\n");
        system("pause");
        return 0;
    }

    for(int i=0;i<7;i++){
        a[i]++;
        a[i]=a[i]^2;
    }

    if(!strcmp(a,b)){
        printf("good!\n");
        system("pause");
        return 0;
    }

    printf("NoNoNo\n");
    system("pause");
    return 0;
    //flag 记得包上 NSSCTF{} 再提交!!!
}

2.对源代码进行分析

    char a[]="wwwwwww";
    char b[]="d`vxbQd";

    //try to find out the flag
    printf("please input flag:");
    scanf(" %s",&a);

因为后来又重新输入了a,所以一开始的char a[]="wwwwwww" 就是用不到的地方。这就是wp上说的干扰项为什么是干扰项。


    if(strlen(a)!=7){
        printf("NoNoNo\n");
        system("pause");
        return 0;
    }

这一步是判断个数如果不等于7就显示NoNoNo,否则就进行下一步。

for(int i=0;i<7;i++){
        a[i]++;
        a[i]=a[i]^2;
 }

这一步是解题原理。

简单来说就是你输入的东西ascii先+1,再逐个与2进行异或。

   if(!strcmp(a,b)){
        printf("good!\n");
        system("pause");
        return 0;
    }

这一步是进行比较,如果输入的a进行异或完了和b一样那就输出good!

  printf("NoNoNo\n");
    system("pause");
    return 0;

这一步说明答案不对也是NoNoNo。

3 .解题流程

分析完代码我们得知:要想得到flag就应该从后面的已知且固定的b入手。

得到flag的过程是:(a[i]+1)^2;逆向就是先^后--。

所以可写出代码

#include <stdio.h>
#include <string.h>
int main()
 {
    char b[]="d`vxbQd";
    for(int i=0;i<7;i++){
        b[i]=b[i]^2;
        b[i]--;
    }
    printf("%s",b);
 }

得到本题答案

4.运行一下exe

结束!