[問題] struct緩衝區溢位問題

作者: owasp (醜小人愛捧人懶趴)   2020-02-22 11:47:34
開發平台(Platform):Win10
編譯器:GCC
額外使用到的函數庫(Library Used):無
問題(Question):在「密碼」處的緩衝區溢位,為什麼要輸入ddaa,而不是aadd
餵入的資料(Input):使用者輸入帳號密碼。
預期的正確結果(Expected Output):無
錯誤結果(Wrong Output):無
程式碼(Code):
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#define ADMINUID 0x61616464
struct user {
char name[24] ;
char password[8];
unsigned int uid ;
};
struct user cur_user;
long long read_choice(){
char buf[24];
long long choice ;
__read_chk(0,buf,23,24);
choice = atoll(buf);
return choice;
}
void read_input(char *buf,unsigned int size){
int ret ;
ret = __read_chk(0,buf,size,size);
if(ret <= 0){
puts("read error");
_exit(1);
}
if(buf[ret-1] == '\n')
buf[ret-1] = '\x00';
}
void login(){
char name_[24];
char pass_[12];
unsigned int uid_ ;
bool found ;
puts("歡迎使用【上下班打卡系統】\n請輸入您的帳號:");
read_input(cur_user.name,23);
//假設這裡有「讀取檔案系統中的帳號、密碼、使用者ID對照表」的程式碼...
if(!strcmp(cur_user.name, name_)){
found = true ;
printf("請輸入您的密碼:");
read_input(cur_user.password,8);
if(!strncmp(cur_user.password,pass_,8)){
cur_user.uid = uid_ ;
}else{
puts("認證失敗!");
exit(-2);
}
}
if(!found){
cur_user.uid = rand();
printf("\n您是新使用者,對吧?\n 新朋友,歡迎您 :)\n\n我將幫您建立新帳號
,\n請輸入您的密碼:\n");
gets(cur_user.password);
}
}
//顯示功能選單
void menu(){
puts("\n\n【上下班打卡系統】");
puts(" 1. 打上班卡");
puts(" 2. 打下班卡");
puts(" 3. 登出 ");
puts("===============");
if(cur_user.uid == ADMINUID)
puts("
作者: Lipraxde (Lipraxde)   2020-02-22 14:07:00
未看先猜 endian
作者: Schottky (順風相送)   2020-02-22 14:26:00
你在講啥......
作者: b0920075 (Void)   2020-02-22 18:05:00
little endian 吧
作者: jaid (jaid)   2020-02-24 08:32:00
直接猜Little Endian

Links booklink

Contact Us: admin [ a t ] ucptt.com