溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

二進(jìn)制文件每?jī)蓚€(gè)的字節(jié)位置交換

發(fā)布時(shí)間:2020-07-04 05:07:48 來(lái)源:網(wǎng)絡(luò) 閱讀:2168 作者:daye8ku 欄目:安全技術(shù)

(一).寫(xiě)作緣由:

        寫(xiě)這篇博客的目的是是為了方便下次使用或者幫助其他需要的人。在打CTF的時(shí)候,偶爾會(huì)遇到還原一些文件,筆者遇到的是分析數(shù)據(jù)流量的時(shí)候,提取出了一個(gè)未知文件,用二進(jìn)制編輯器打開(kāi),搜所文件頭,發(fā)現(xiàn)和某個(gè)文件頭有點(diǎn)相似,但是每?jī)蓚€(gè)字節(jié)位置顛倒了,于是就想到把每?jī)蓚€(gè)字節(jié)交換位置,就像下面這種:


二進(jìn)制文件每?jī)蓚€(gè)的字節(jié)位置交換二進(jìn)制文件每?jī)蓚€(gè)的字節(jié)位置交換


(二).演示及效果:

在命令行執(zhí)行前后如下圖:

二進(jìn)制文件每?jī)蓚€(gè)的字節(jié)位置交換二進(jìn)制文件每?jī)蓚€(gè)的字節(jié)位置交換二進(jìn)制文件每?jī)蓚€(gè)的字節(jié)位置交換



(三).貼上代碼:

        代碼是C寫(xiě)的,有點(diǎn)多不太美觀,功能太單一, 也沒(méi)弄啥模塊化,編寫(xiě)環(huán)境是windows。

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

int main(int argc, const char** argv)
{
	//The pointer of input file and output file
	FILE *fin;
	FILE* fout;
	
	//The file name of input file and output
	char* inFile;
	char* outFile;

	//The pointer of single and double byte type
	unsigned short* pDouble = NULL;
	unsigned char* pSingle = NULL;

	//The memery buffer of header and tail pointer about input file
	void* fBuffStart = NULL;
	void* fBuffEnd = NULL;

	//The size of input file (Byte)
	unsigned long fileSize = 0;

	//Judge the count of parameter and the file name limit 
	if(argc != 3 || strlen(argv[1]) > 255 || strlen(argv[2]) > 255)
	{
		printf("\n[-]Usage: %s infile outfile\n", argv[0]);
		printf("[-]Filename limited: 255 Byte\n");
		exit(-1);
	}


	inFile = argv[1];
	outFile = argv[2];


	//Exception handling
	if(!(fin = fopen(inFile, "rb")))
	{
		printf("Error: open %s failed!\n", inFile);
		exit(-1);
	}


	//Obtain file size
	fseek(fin, 0, SEEK_END);
	fileSize = ftell(fin);
	fseek(fin, 0, SEEK_SET);

	//Alloc memery  for input file and read its data to memery
	fBuffStart = (unsigned char*)malloc(fileSize);
	memset(fBuffStart, 0, fileSize);
	fread(fBuffStart, 1, fileSize, fin);
	fclose(fin);


	//The position of start and end input file memery 
	fBuffEnd = (unsigned char*)fBuffStart + fileSize;
	pDouble = (unsigned short*)fBuffStart;

	//Exception handling
	if(!(fout = fopen(outFile,"wb")))
	{
		printf("Error: open %s failed!\n", outFile);
		exit(-1);
	}

	//Exchange position of each two byte
	while(pDouble != fBuffEnd)
	{
		pSingle = pDouble;
		fwrite(pSingle+1, 1, 1, fout);
		fwrite(pSingle, 1, 1, fout);
		pDouble++;
	}

	fclose(fout);

	return 0;
}


(四).若有不足之處,還請(qǐng)斧正。


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI