C编程字节数据类型

C programming byte data type

我正在做一个 ctf,我已经反向和 elf 文件,找到了对标志进行编码的函数,我已经做了一个解码器,但是因为它在 c 中,所以我不能使用字节数据类型。有没有我可以添加的库,如果没有,这段代码是如何使用字节数据类型的。我已经对这位作者进行了一些挑战,我通过在 c 中解压解决了这个问题,我认为这就是所谓的动态字符串转换。


// 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;
}

这是我的编码器:

#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


评论已经为您提供了两个很好的答案(使用 stdint.h 或字符),但除此之外...

  • 如果您无法访问 stdint header 并且不想使用字符,Boost 等库也可以为您提供 uint8_t 数据类型。

  • 在 C++ 中,您可以 std::byte 访问(具体来说,不确定您是否会从中得到帮助,但其他人可能)

如果您想确保 char 是 8 位长度,您可以检查 .

中定义的 CHAR_BIT 值

所以你的选择(从好到坏排序)是:

  1. (如果你可以使用cpp)使用std::byte
  2. 使用 <stdint.h>
  3. 中定义的 uint8_t 数据类型
  4. 使用 char/unsigned 字符
  5. 使用外部库

请注意,除非您已经拥有该库,否则使用外部库来完成这种微不足道的任务可能有点矫枉过正。

希望对您有所帮助。

您的解码器似乎有一些错误,例如:local_38[local_40] = local_38[local_40] - 2;

应该是这样的:local_38[local_40] = local_38[local_40] + 2;

我已经在 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)

希望这会有所帮助