作为安全CS课程的一部分,我班的任务是利用漏洞利用堆栈/缓冲区溢出来击败密码检查。带有漏洞的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
int main(int argc, char **argv) {
char correct_hash[16] = {
0xd0, 0xf9, 0x19, 0x94, 0x4a, 0xf3, 0x10, 0x92,
0x32, 0x98, 0x11, 0x8c, 0x33, 0x27, 0x91, 0xeb
};
char password[16];
printf("Insert your password: ");
scanf("%29s", password);
MD5(password, strlen(password), password);
if (memcmp(password, correct_hash, 16) == 0) {
printf("Correct Password!\n");
} else {
printf("Wrong Password, sorry!\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我了解经典的“堆栈粉碎”原理(我认为),并且这里存在一个明显的溢出漏洞,该漏洞correct_hash可以通过在提示时输入长度超过15个字符的密码来覆盖数组的前14个字节。但是,我不知道如何利用此功能使memcmp检查通过,从而完成挑战。我发现/尝试过的一些东西:
设置password为等价是correct_hash行不通的,因为password使用MD5()进行了哈希处理(无论如何都不可能将两者相等),因为 …
正如标题所述,我想知道具有灵活数组成员的C结构数组如何表现.这是一个例子:
struct vector {
size_t length;
double array[];
};
Run Code Online (Sandbox Code Playgroud)
维基百科的文章说:
sizeof这种结构上的操作符需要给出柔性阵列成员的偏移量.
在我的机器上,这对应于8个字节(sizeof(size_t)).但是,当我执行以下操作时会发生什么:
显然,数组不能保存向量数据v0,因为它只有3*8字节= 24字节宽.我该如何处理这样的情况?
#define LENGTH 10
int main() {
struct vector arr[3];
struct vector *v0 = calloc(1, sizeof(*v0) + LENGTH * sizeof(v0->array[0]));
v0->length = LENGTH;
size_t i;
for (i = 0; i < v0->length; i++) {
v0->array[i] = (double) i;
}
struct vector v1;
struct vector v2;
arr[0] = *v0;
arr[1] = v1;
arr[2] = v2;
for (i = 0; …Run Code Online (Sandbox Code Playgroud) 我尝试使用C 实现一些纯通用的算法.我坚持使用3向快速排序但不知何故实现不能提供正确的输出.输出几乎排序,但有些键不在应有的位置.代码如下.提前致谢.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
static void swap(void *x, void *y, size_t size) {
void *tmp = malloc(size);
memcpy(tmp, x, size);
memcpy(x, y, size);
memcpy(y, tmp, size);
free(tmp);
}
static int cmpDouble(const void *i, const void *j) {
if (*(double *)i < *(double *)j)
return 1;
else if (*(double *)i == *(double *)j)
return 0;
else
return -1;
}
void qsort3way(void *base, int lo, int hi, size_t size,
int (*cmp)(const void *, const void …Run Code Online (Sandbox Code Playgroud) 这是一段似乎被接受而没有错误的代码:
#include <stdio.h>
#include <string.h>
int main() {
if (strcmp(1, 2))
printf(3);
}
Run Code Online (Sandbox Code Playgroud)
编译时clang -std=c11 -Weverything会产生4个警告:
badstrcmp.c:5:16: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
if (strcmp(1, 2))
^
/usr/include/string.h:77:25: note: passing argument to parameter '__s1' here
int strcmp(const char *__s1, const char *__s2);
^
badstrcmp.c:5:19: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
if (strcmp(1, 2))
^
/usr/include/string.h:77:43: note: passing argument …Run Code Online (Sandbox Code Playgroud) 所以我通读了其他问题,他们被告知放在#define _GNU_SOURCE任何包含之前,它会起作用,但对我不起作用。我也尝试添加#define _GNU_SOURCE char *strcasestr(const char *haystack, const char *needle);但仍然不起作用。我找不到关于此的任何其他信息,也许有人可以提供帮助?提前致谢。
错误:函数“strcasestr”的隐式声明
/**
*
* Description: This is code for Lab 3 Task 2.
* Reads data from file and gives opportunity to search by cities
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
printf("Please input the city you want to find employees in:");
scanf("%s", input);
maxline = i;
for (i = 0; i <= maxline; i++) {
if (strcasestr(employee[i].city, input) != 0) { // PROBLEM …Run Code Online (Sandbox Code Playgroud) 考虑以下代码段:
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
int main() {
for (;;) {
char *buf = readline(">>> ");
if (!buf)
break;
free(buf);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的系统上,使用valgrinds进行编译-lreadline,在其下执行程序valgrind并输入一些行会导致巨大的内存泄漏,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
int main() {
for (;;) {
char *buf = readline(">>> ");
if (!buf)
break;
free(buf);
}
}
Run Code Online (Sandbox Code Playgroud)
运行--show-leak-kinds=all类似这样的结果(整个过程长数百行,我只显示开始):
==7651== LEAK SUMMARY:
==7651== definitely lost: 0 bytes in 0 blocks
==7651== indirectly lost: 0 bytes in 0 blocks
==7651== possibly lost: 0 bytes in …Run Code Online (Sandbox Code Playgroud) I'm trying to port a knn (k nearest neighbor search ) on a kd-tree that I wrote in Java to C.
The Java output, as expected:
Nearest to Key: 6.0,5.0,4.0
Key:6.0,5.0,4.0,min distance:0.0
Key:5.0,4.0,3.0,min distance:3.0
Key:7.0,6.0,5.0,min distance:3.0
Key:4.0,3.0,2.0,min distance:12.0
Key:3.0,2.0,1.0,min distance:27.0
Run Code Online (Sandbox Code Playgroud)
Java code, class (Its a quick implementation just to get the algorithm working before I start my port):
Nearest to Key: 6.0,5.0,4.0
Key:6.0,5.0,4.0,min distance:0.0
Key:5.0,4.0,3.0,min distance:3.0
Key:7.0,6.0,5.0,min distance:3.0
Key:4.0,3.0,2.0,min distance:12.0
Key:3.0,2.0,1.0,min distance:27.0
Run Code Online (Sandbox Code Playgroud)
Java knn method:
class kd_tree {
public int …Run Code Online (Sandbox Code Playgroud) 对于以下每一对
scanf格式字符串,请指出这两个字符串是否等效。如果不是,请说明如何区分它们:(b)
"%d-%d-%d"与"%d -%d -%d"
所以在这种情况下,我的回答是它们不等价。因为除了以%,开头的转换说明符以外的非空白字符不能以空格开头,所以它不会与非空白字符匹配。所以在第一种情况下,第一个和第二个整数后面不允许有空格,而在第二种情况下,前两个整数后面允许有任意数量的空格。
但我看到这本书有不同的答案。它说它们彼此等效。这是书的错吗?或者我只是对scanf函数中格式字符串的概念有误?
这段代码让我很困惑。我无法理解%0里面在做什么printf!
代码:
#include <stdio.h>
int main() {
int a = 100;
printf("%0 %x", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
%x
Run Code Online (Sandbox Code Playgroud)