简体   繁体   English

C 编程字节数据类型

[英]C programming byte data type

I am doing a ctf and I have reversed and elf file and found the function that encodes the flag and i have made a decoder but because it is in ci cant use the byte data type.我正在做一个 ctf 并且我已经反转和 elf 文件并找到了对标志进行编码的函数,我已经制作了一个解码器,但是因为它无法使用字节数据类型。 Is there any library that i can add and if not how did this code use the byte data type.有没有我可以添加的库,如果没有,这段代码是如何使用字节数据类型的。 I alredy did some challanges by this author and i solved the by deocidng the in c, and i think that this is something called dynamic string traformation.我已经解决了这位作者的一些挑战,我通过在 c 中 deocidng 解决了这个问题,我认为这就是所谓的动态字符串转换。


// the original encoder
undefined8 main(void)

{
  int iVar1;
  ssize_t sVar2;
  long in_FS_OFFSET;
  int local_40;
  byte local_38 [40];
  long local_10;

  local_10 = *(long *)(in_FS_OFFSET + 0x28);
  initialize_flag();
  puts("Give me your password: ");
  sVar2 = read(0,local_38,0x1f);
  local_38[(int)sVar2 + -1] = 0;
  local_40 = 0x28;
  while (local_40 < (int)sVar2 + -1) {
    local_38[local_40] = local_38[local_40] ^ (char)local_40 + 10U;
    local_38[local_40] = local_38[local_40] - 2;
    local_40 = local_40 + 1;
  }
  iVar1 = strcmp((char *)local_38,"lp`7a<qLw\x1ekHopt(f-f*,o}V\x0f\x15J");
  if (iVar1 == 0) {
    puts("Thats the right password!");
    printf("Flag: %s",flagBuffer);
  }
  else {
    puts("Thats not the password!");
  }
  if (local_10 != *(long *)(in_FS_OFFSET + 0x28)) {
                    /* WARNING: Subroutine does not return */
    __stack_chk_fail();
  }
  return 0;
}

Here is my encoder:这是我的编码器:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int main()
{
  ssize_t sVar2;
  int local_40;
  byte local_38 [40];




  sVar2 = read(0,local_38,0x1f);

  local_38[(int)sVar2 + -1] = 0;

  local_40 = 27;// the flag lenght is 27
  while (local_40 > 0) {
//this is the reverse of the og encoding
    local_40 = local_40 - 1;
   local_38[local_40] = local_38[local_40] - 2;
    local_38[local_40] = (local_38[local_40] ^ (char)local_40) - 10U;
  }

 puts(local_38);    


  return 0;  


}


//lp`7a<qLw\x1ekHopt(f-f*,o}V\x0f\x15J this is the encoded flag
// s after the original encoding should be w


The comments already have gotten you two great answers(Using stdint.h or chars), but otherwise...评论已经为您提供了两个很好的答案(使用 stdint.h 或字符),但除此之外...

  • If you do not have access to the stdint header and do not want to use chars, libraries such as Boost can provide you the uint8_t datatype, too.如果您无权访问 stdint 标头并且不想使用字符,则 Boost 等库也可以为您提供 uint8_t 数据类型。

  • In C++, you have std::byte accessible(Not sure you, specifically, will be helped by that, but others maybe)在 C++ 中,你有 std::byte 可访问(不确定你,特别是,会得到帮助,但其他人可能)

If you wish to make sure a char is 8-bits of lenght, you can check the CHAR_BIT value defined in .如果你想确保一个char是 8 位的长度,你可以检查 中定义的 CHAR_BIT 值。

So your options(Ranking from best to worse) are:所以你的选择(从最好到最差的排名)是:

  1. (If you can use cpp) Using std::byte (如果您可以使用 cpp)使用 std::byte
  2. Using the uint8_t datatype defined in <stdint.h>使用<stdint.h>定义的 uint8_t 数据类型
  3. Using a char/unsigned char使用字符/无符号字符
  4. Using an external library使用外部库

Note it's probably overkill using an external library for such a trivial task unless you already have that said library.请注意,除非您已经拥有该库,否则使用外部库来完成这样一项微不足道的任务可能有点过头了。

Hope this helps.希望这可以帮助。

your decoder seems to have some errors like: local_38[local_40] = local_38[local_40] - 2;您的解码器似乎有一些错误,例如:local_38[local_40] = local_38[local_40] - 2;

It should be like this: local_38[local_40] = local_38[local_40] + 2;应该是这样的:local_38[local_40] = local_38[local_40] + 2;

I have written a decoder for above question in python我在python中为上述问题编写了一个解码器

key="lp`7a<qLw\x1ekHopt(f-f*,o}V\x0f\x15J"
check=list(key)
string=str()
string=''
for i in range(26,-1,-1):
    j=i+10
    k=(ord(check[i])+2)
    string=(chr(k^j)+string)
print(string)

Hope this will help希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM