发新话题
打印

[讨论]一个让我很难解决的问题

[讨论]一个让我很难解决的问题

今天在写程序的时候需要从一个二进制文件中读取数据,可是奇怪的是。。居然老是读不完整。。以前读文件用的都是ReadFile函数。。这次我用的是c运行时函数。。。代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

int main(int argc,char* argv[])
{
     FILE* fp;
     struct stat st;
     int sLen;
     if(argc!=2)
     {usage(argv[0]);exit(0);}
          fp=fopen(argv[1],"rb");
          if(fp==NULL)
          {
              printf("open file error!\n");
                   exit(0);}

                   fstat(fileno(fp),&st);
              if(st.st_size>1024)
              {
              printf("shellcode is too long!\n");
              exit(0);
          }
          printf("the size of %s is:%d\n",argv[1],st.st_size);

          char* shellcode=(char*)(malloc(st.st_size));
      if(!fread(shellcode,st.st_size,1,fp))
              {
                   printf("read file error!\n");
                   exit(0);
              }

fclose(fp);

              sLen=strlen(shellcode);
              printf("The source code are:\n%s\n",shellcode);
        printf("The source code are:\n%s\n",shellcode);
        printf("the length of source code is:%d\n",sLen);
return 0;
}

运行结果如下:
E:\lcc\lcc>csc test.bin
the size of test.bin is:57
The source code are:
U夊侅
the length of source code is:6


只读出了前6个字节,仔细想了一下估计是test.bin文件中有”\x00",因此fread函数一碰到这个就以为是到了文件的末尾。因此就不读了。。。

怎么才能把所有的内容都度出来呢。。。我一时没想出来。。还请各位明白人帮我看看。。。谢谢!!急阿。。程序都快要完工了。。拜托
俺是mika!别叫错了! 俺的QQ:794773 http://hi.baidu.com/stealthwalker/ my private area ------------------------------------------------------------ <a href=http://hi.baidu.com/stealthwalker target=_blank></a>

TOP

是我弄错了。。。我太笨。。唉。。天下第一号大笨蛋。。。

多谢无缝兄的教诲。。。错误不是在于fread而是在于strlen函数,它碰到“\x00"就不会再继续统计了。。。。

我去跳楼去了。。。
俺是mika!别叫错了! 俺的QQ:794773 http://hi.baidu.com/stealthwalker/ my private area ------------------------------------------------------------ <a href=http://hi.baidu.com/stealthwalker target=_blank></a>

TOP

sLen=strlen(shellcode);
          printf("The source code are:\n%s\n",shellcode);
凭啥断定没有读完?
strlen当然是到00就结束了
但并不代表数据没有读完

TOP

strlen只是统计长度 输出并不依赖它 怎么会是strlen的问题?

问题还是在fread上。。。你遍历一下 看看第6个字节是不是\0就知道了

fread内部也应该调用的ReadFile  所以我觉得用哪个都一样

只不过C运行时库需要初始化。。。
请加47809945   100%通过!每个月总有那么几天,您的网络会受到黑客的攻击--坐立不安,烦躁无力,使用虎虎开发的"月月舒"防火墙,超轻超薄,易于携带,提供由内到外的全方位保护,即使流量再大,也可以冲浪自如,再也不用担心侧漏啦。

TOP

不是的虎子,我试过了。。。fread是能把需要的字节数都读出来的。但是strlen在统计的时候遇到”\x00“认为字符串已经到了结尾,错误就是使用了strlen函数造成的。
俺是mika!别叫错了! 俺的QQ:794773 http://hi.baidu.com/stealthwalker/ my private area ------------------------------------------------------------ <a href=http://hi.baidu.com/stealthwalker target=_blank></a>

TOP

原来strlen会改变源字符串。。而不是单纯的统计长度

我一直以为strlen只是统计长度 遇到\0停止 但不改变

字符串。。。

#include <string.h>
#include <stdio.h>

int main (void)
{
   char *Test="Error\0Success";
   int Len=strlen(Test);

   fprintf(stdout,"%s\n%d\n",Test,Len);

   system("pause");
   return 0;
}
请加47809945   100%通过!每个月总有那么几天,您的网络会受到黑客的攻击--坐立不安,烦躁无力,使用虎虎开发的"月月舒"防火墙,超轻超薄,易于携带,提供由内到外的全方位保护,即使流量再大,也可以冲浪自如,再也不用担心侧漏啦。

TOP

虎子你的说法还是不对的,strlen函数并没有改变长度。。。你看看下面的代码:
引用:
#include <string.h>
#include <stdio.h>

int main (void)
{
  char Test[]="Error\0Success";
  printf("%c\n",Test[6]);//应该输出的是S。。。
  int Len=strlen(Test);

  fprintf(stdout,"%s\n%d\n",Test,Len);
  fprintf(stdout,"%c\n",Test[6]);//如果原数组被截断的话就不应该输出S。。。
  return 0;
}
输出的结果是这样的:
引用:
E:\lcc\lcc>test
S
Error
5
S
说明还是原长度,并没有改变!因为你用fprintf的"%s"格式来输出Test的数组,所以它会认为碰到\0时就是字符串的结尾。。所以只输出了Error。。。
俺是mika!别叫错了! 俺的QQ:794773 http://hi.baidu.com/stealthwalker/ my private area ------------------------------------------------------------ <a href=http://hi.baidu.com/stealthwalker target=_blank></a>

TOP

说了半天。。。是输出的问题?!  

我们的方向都想错了。。。晕倒中。。。
请加47809945   100%通过!每个月总有那么几天,您的网络会受到黑客的攻击--坐立不安,烦躁无力,使用虎虎开发的"月月舒"防火墙,超轻超薄,易于携带,提供由内到外的全方位保护,即使流量再大,也可以冲浪自如,再也不用担心侧漏啦。

TOP

呵呵~的确~printf("%s",shellcode)
遇到 \x00 就会自动截断,所以就照成了读取不完的假象~
无敌说得对
strlen不会改变长度
本人觉得strlen的函数是这样实现的

int strlen(char *buf)
{
  int i;
  for(i=0;buf;i++);
  return i;
}
应该是这样子的,属于不会改变字符和长度

TOP

呵呵
不小心少写了点东西
应该是这样
int strlen(char *buf)
{
  int i;
  for(i=0;buf;i++);
  return i;
}

TOP

郁闷
怎么把我的给过滤了啊

再试一下

for(i=0;  buf  ;i++)

TOP

真的给过滤了
buf 后面应该加
[  i  ]

这样应该不会过滤了吧

TOP

发新话题