我的课没有教科书,也没有任何结构化的学习材料。我所有的学习都是通过谷歌搜索完成的,当我边做边学的时候,说实话会很慢。
课堂作业:
文件 prog-bad.c 中有一个程序(有点伪代码)。这个程序不是为了编译或运行而编写的,而是为了阅读。
找出漏洞是什么,以及这些漏洞可能导致什么,并简要说明您发现的内容。
#include <stdio.h>
int main() {
int studentId;
char studentName[100], buffer[100], percentage[10];
char* firstName = malloc(sizeof(char)*50);
char* lastName = malloc(sizeof(char)*50);
char* graduateLevel = NULL;
printf("\n**********Welcome to Student Registration Service**********\n");
printf("\nPlease enter your 10 digit Student ID: ");
scanf("%d", &studentId);
printf("\nPlease enter your first name: ");
gets(firstName);
printf("\nPlease enter your last name: ");
gets(lastName);
{
char level[2];
printf("\nPlease enter your graduate level (UG/PG): ");
gets(level);
graduateLevel = &level;
}
strcpy(studentName, firstName);
strcat(studentName, lastName);
printf("\nPlease enter your percentage in Highschool (like 85.50%): ");
gets(percentage);
// Student record saving in file
updateStudentRecord();
printf("\nStudent record saved with the following details: \n");
printf("\nStudent ID: %d", studentId);
printf("\nStudent Name: %s", studentName);
printf("\nStudent percentage in Highschool: ");
printf(percentage);
free(firstName);
free(firstName);
return(0);
}
对于我的回答,我说的是使用所有打开程序以缓冲溢出攻击的scanf、gets和函数。printf但是我有点矛盾,因为我看到程序使用了malloc,我认为这是一种通过将所有内容移动到不可执行堆来保护堆栈的方法。
那么这是否意味着该程序并不像我想象的那样不安全,这是一个棘手的问题?