XCTF攻防世界--(新手賽區)第2題快來圍觀學習!

極安御信發表於2022-05-20

解題方法寫在程式碼註釋裡了
題目:

 

#include

#include

//大概閱讀整體程式碼,透過最後幾行程式碼我們得知我們需要的flag就是程式碼中的hash變數

//此時我們逆向推到hash變數的來源

//1.hash值由fist、second和argv[3]三個變數來決定,那我們就跟蹤這三個變數

//2.fist變數在第二個if語句中,提示如果first不等於0xcafe的話就退出程式,那我們按照作者意思肯定要讓程式執行到顯示flag

//那就必須使first=0xcafe

//3.接下來我們再找著second變數,發現second在第三個if語句,提示如果second % 5 == 3 || second % 17 != 8的話就退出,我們目的

//就是執行到最後,那我們就直接用窮舉法找到一個數滿足second % 5 != 3 && second % 17 == 8

//函式:                unsigned int GetNum = 0;

//                                do{

//                                        if(GetNum % 5 != 3 && GetNum % 17 == 8)

//                                                        {

//                                                                printf("GetNum:%d",GetNum);

//                                                                break;

//                                                        }

//                                        GetNum++;

//                                        }while(1);

//最後得到GetNum = 25,也就是second是25

//4.最後argv[3]可從最後一個if得知argv[3]=h4cky0u

//5.那我們帶進hash中去算

//unsigned int hash =0xcafe * 31337 + (25 % 17) * 11 + strlen("h4cky0u") - 1615810207;

//注意最後一行程式碼要以十六進位制輸出才能得到flag

//z最後flag為c0ffee

//6.提交顯示正確了

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

        if (argc != 4) {

                printf("what?\n");

                exit(1);

        }

 

        unsigned int first = atoi(argv[1]);

        if (first != 0xcafe) {

                printf("you are wrong, sorry.\n");

                exit(2);

        }

 

        unsigned int second = atoi(argv[2]);

        if (second % 5 == 3 || second % 17 != 8) {

                printf("ha, you won't get it!\n");

                exit(3);

        }

 

        if (strcmp("h4cky0u", argv[3])) {

                printf("so close, dude!\n");

                exit(4);

        }

 

        printf("Brr wrrr grr\n");

 

        unsigned int hash = first * 31337 + (second % 17) * 11 + strlen(argv[3]) - 1615810207;

 

        printf("Get your key: ");

        printf("%x\n", hash);

        return 0;

}


相關文章