我正在学习缓冲区溢出,并试图制作一个.我有这个代码:
#include <stdio.h>
char *secret = "password";
void go_shell() {
char *shell = "/bin/sh";
char *cmd[] = { "/bin/sh", 0 };
setreuid(0);
execve(shell,cmd,0);
}
int authorize() {
char password[64];
printf("Enter Password: ");
gets(password);
if (!strcmp(password,secret)) {
return 1;
}
else {
return 0;
}
}
int main() {
if (authorize()) {
printf("login successful\n");
go_shell();
} else {
printf("Incorrect password\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我用gcc编译它,然后在gdb中运行它
我输入大约100"A"作为密码,程序崩溃.
问题是没有寄存器被覆盖 0x4141414141414141
我用谷歌搜索并添加了-fno-stack-protector标志gcc,这允许RBP被覆盖0x4141414141414141但没有别的.
我想知道是否有一种编译代码的方法,以便可以覆盖RIP.
我有一个问题,使用Android的MediaRecorder记录从麦克风到.m4a文件(AAC-LC,MPEG-4容器)的声音.从API级别18开始,默认采样率从44.1或48 kHz(取决于设备)降至仅8 Hz.如果我使用MediaRecorder.setAudioSamplingRate指定采样率,它会使用指定的速率,但录制中会有很多奇怪的噪音.
在LogCat中,以下警告会不时发生:
(1)标签:AudioSource文本:AudioRecord报告超限
(2)标签:AudioFlinger文本:RecordThread:缓冲区溢出
这是代码:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioChannels(2);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioSamplingRate(48000); // if not specified, defaults to 8kHz, if specified 44.1 or 48 kHz, lots of noise
recorder.setOutputFile("test.m4a");
try {
recorder.prepare();
recorder.start();
} catch (IOException ioe) {
Log.e(TAG, "IOException", ioe);
} catch (IllegalStateException ise) {
Log.e(TAG, "IllegalStateException", ise);
} catch (Exception e) {
Log.e(TAG, "Exception", e);
}
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏.
android buffer-overflow audio-recording sampling mediarecorder
问题实际上是关于C中的堆栈溢出.我有一个不能完成我的生活的分配,我已经查看了gdb中的所有内容,我只是无法想象它.
问题如下:
int i,n;
void confused()
{
printf("who called me");
exit(0);
}
void shell_call(char *c)
{
printf(" ***Now calling \"%s\" shell command *** \n",c);
system(c);
exit(0);
}
void victim_func()
{
int a[4];
printf("[8]:%x\n", &a[8]);
printf("Enter n: "); scanf("%d",&n);
printf("Enter %d HEX Values \n",n);
for(i=0;i<n;i++) scanf("%x",&a[i]);
printf("Done reading junk numbers\n");
}
int main()
{
printf("ls=736c --- ps = 7370 --- cal = 6c6163\n");
printf("location of confused %x \n", confused);
printf("location of shell_call %x \n", shell_call);
victim_func();
printf("Done, thank you\n");
}
Run Code Online (Sandbox Code Playgroud)
好的,所以我设法正确地得到了第一个问题,即任意调用主路径中未明确调用的两个函数之一.顺便说一下,这必须在运行程序时完成而不做任何修改.我这样做是通过运行程序,设置 …
我和其他几千人正在收到Microsoft Visual C++运行时抛出的错误:
替代文字http://i46.tinypic.com/2enceus.png
为了搜索引擎的利益,它说:
Microsoft Visual C++ Runtime Library
Buffer overrun detected!
Program: %s
A buffer overrun has been detected which has corrupted the program's
internal state. The program cannot safely continue execution and must
now be terminated.
Run Code Online (Sandbox Code Playgroud)
现在我明白缓冲区溢出是什么,以及为什么它是一件坏事.鉴于微软新近强调" 它刚刚破解 ",MSVCRT中的额外缓冲区检查可能是一件好事.
另一方面,我不在乎.这不是程序无法继续,而是程序无法安全继续.好吧,我宁愿不安全,因为它总比没有好.我喜欢危险地生活.
所以有人可以建议吗?我在想这样的事情:
我尝试搜索编写Microsoft Visual C++运行时库的公司的支持站点,但他们没有提到哪些函数可能溢出,或者如何禁用溢出检查.
void function(int a, int b, int c) {
char buffer1[5];
char buffer2[10];
int *ret;
ret = buffer1 + 12;
(*ret) += 8;//why is it 8??
}
void main() {
int x;
x = 0;
function(1,2,3);
x = 1;
printf("%d\n",x);
}
Run Code Online (Sandbox Code Playgroud)
上面的演示来自这里:
http://insecure.org/stf/smashstack.html
但它在这里不起作用:
D:\test>gcc -Wall -Wextra hw.cpp && a.exe
hw.cpp: In function `void function(int, int, int)':
hw.cpp:6: warning: unused variable 'buffer2'
hw.cpp: At global scope:
hw.cpp:4: warning: unused parameter 'a'
hw.cpp:4: warning: unused parameter 'b'
hw.cpp:4: warning: unused parameter 'c' …Run Code Online (Sandbox Code Playgroud) 什么是防止缓冲区溢出攻击的想法?我听说过Stackguard,但到目前为止,这个问题是通过应用stackguard还是将它与其他技术结合起来完全解决的?
热身后,作为一名经验丰富的程序员
为什么你认为 为缓冲区溢出攻击提供足够的防御是如此困难?
编辑:感谢所有答案并保持安全标签活跃:)
下面代码的输出是"溢出",但我没有明确调用该func函数.它是如何工作的?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int copy(char *input)
{
char var[20];
strcpy(var, input);
return 0;
}
int func(void)
{
printf("Overflow\n");
return 0;
}
int main(int argc, char *argv[])
{
char str[] = "AAAABBBBCCCCDDDDEEEEFFFFGGGG";
int *p = (int *)&str[24];
*p = (int)func;
copy(str);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有以下两个用于发送和接收数据包的功能.
void send(std::string protocol)
{
char *request=new char[protocol.size()+1];
request[protocol.size()] = 0;
memcpy(request,protocol.c_str(),protocol.size());
request_length = std::strlen(request);
boost::asio::write(s, boost::asio::buffer(request, request_length));
}
void receive()
{
char reply[max_length];
size_t reply_length = boost::asio::read(s, boost::asio::buffer(reply, request_length));
std::cout << "Reply is: ";
std::cout.write(reply, reply_length);
std::cout << "\n";
}
Run Code Online (Sandbox Code Playgroud)
问题与此部分有关,boost::asio::buffer(reply, request_length)其中请求长度是在发送数据包时最初设置的字符串的长度.如何在不知情的情况下检查缓冲区的大小request_length?另一个问题是如何防止缓冲区溢出?
来自GNU C编程教程:
该
fgets("文件中获取字符串")功能类似于获取功能.这个函数已被弃用 - 这意味着它已经过时,强烈建议你不要使用它 - 因为它很危险.这很危险,因为如果输入数据包含空字符,则无法分辨.fgets除非您知道数据不能包含null,否则请勿使用.不要使用它来读取用户编辑的文件,因为如果用户插入空字符,您应该正确处理它或打印清晰的错误消息.如果可以,请始终使用getline或getdelim代替fgets.
我觉得这个fgets功能在遇到a \0或者时会停止\n; 为什么当fgets应该正确处理输入时,这个手册页建议空字节是"危险的" ?此外,之间有什么区别getline和fgets,是fgets真正视为功能过时的C99或将来的C标准?
由于-fstack-protector-stronggcc中有一个选项可以检测堆栈粉碎.但是,它无法始终检测到堆栈缓冲区溢出.对于第一个函数func,当我输入10个字符串更多字符串时,程序并不总是崩溃.我的问题是有一种方法来检测堆栈缓冲区溢出.
void func()
{
char array[10];
gets(array);
}
void func2()
{
char buffer[10];
int n = sprintf(buffer, "%s", "abcdefghpapeas");
printf("aaaa [%d], [%s]\n", n, buffer);
}
int main ()
{
func();
func2();
}
Run Code Online (Sandbox Code Playgroud)