ディスクを安全に消去するための最短コード


9

DoD 5220.22-Mワイプメソッドの簡略化されたバリアントを実行するための最短のコードを2回の書き込みパスで記述してみましょう。

すべてのプログラミング言語が受け入れられますが、ディスクワイピング指向のライブラリの使用は禁止されています。

擬似コードで実装する方法は次のとおりです。

Set x to 0

[Start]
'Write Pass
For each sector in disk write the content of x
'Verification Pass
For each sector in disk {
    If sector does not contain the content of x then goto [Start]
}
'Check whether we already did the pass with 1
If x is not 1 then {
    Set x to 1
    GoTo [Start]
}
Else end

つまり、このコードは2回実行され、の書き込みパスと検証パス0、およびの書き込みパスと検証パスが含まれ1ます。

コードゴルフスタイルを実装するのに十分だらけの人はいますか?;)


6
テストするのがいかに難しいかということで、これがどんな答えをもらえるとは思えません。
— James

12
真剣に、この種の治療に値するためにSSDは何をしましたか?それはあなたのテディベアのコレクション全体か何かを殺しましたか?
— R. Kap

2
本当にこれに挑戦して取り組みたい...また、実際にHDDを犠牲にしてテストしたくない。
— Michelfrancis Bustillos

6
この質問は悪意のあるコードを要求するため、この質問はトピック外として締めくくります。
— AdmBorkBork

2
そうでないと私は主張します。この課題は、プライバシーと情報セキュリティを促進するコードを求めています。
— MathuSum Mut

回答:


1

x86マシンコード(Linux)、116バイト

00000000: 89cb 6a02 5931 d289 4c24 f4b0 05cd 8050  ..j.Y1..L$.....P
00000010: 5b53 6a02 5a31 c9b0 13cd 8089 c65b 5331  [Sj.Z1.......[S1
00000020: d231 c9b0 13cd 8089 f75b 53b2 018d 4c24  .1.......[S...L$
00000030: f8b0 04cd 804e 09f6 75ef 5b53 31d2 31c9  .....N..u.[S1.1.
00000040: b013 cd80 89fe 5b53 8d4c 24f4 b201 b003  ......[S.L$.....
00000050: cd80 4e09 f674 0c8a 4424 f838 4424 f474  ..N..t..D$.8D$.t
00000060: e5eb ad89 fe8a 4424 f8c6 4424 f801 3c01  ......D$..D$..<.
00000070: 759e 58c3                                u.X.

引数としてファイル名を取ります

アセンブリ(NASM):

section .text
	global func
func:			;this function uses fastcall conventions
	;seems like no enter instr needed

	;open file
	mov ebx, ecx	;first argument to func (filename)
	push 0x2	;flag O_RDWR
	pop ecx		;pushing a constant is shorter than mov
	xor edx, edx	;mode=0
	mov [esp-12], ecx ;set first byte (msg) to write to 0. msg later in esp-8, buf in esp-12
	mov al, 5	;using 8 bit regs is smaller
	int 0x80	;syscall open
	push eax	;save file descriptor

	write_verify:

	;get file size
	pop ebx		;get fd
	push ebx
	push 0x2	;flag SEEK_END
	pop edx
	xor ecx, ecx 	;offset 0
	mov al, 0x13
	int 0x80	;syscall lseek
	mov esi, eax	;save file byte count in esi
	

	;reset index of file to 0
	pop ebx		;get fd
	push ebx
	xor edx, edx	;flag SEEK_SET=0
	xor ecx, ecx	;ecx=0
	mov al, 0x13
	int 0x80	;syscall lseek

	;***write pass***
	mov edi, esi
	write_loop:	;for each byte in byte count, write [msg]
		;write to file
		pop ebx		;file descriptor
		push ebx
		mov dl, 1	;bytes to write
		lea ecx, [esp-8] ;buffer to write from
		mov al, 4
		int 0x80	;syscall write
		dec esi	;decrement esi (byte count) to 0
		or esi, esi	;cmp esi to 0
		jne write_loop	;while esi!=0, keep looping

	;reset index of file to 0
	pop ebx		;get fd
	push ebx
	xor edx, edx	;flag SEEK_SET=0
	xor ecx, ecx	;ecx=0
	mov al, 0x13
	int 0x80	;syscall lseek

	
	;***verification pass***
	mov esi, edi
	verify_loop:	;for each byte in byte count, verify written byte
		pop ebx		;get fd
		push ebx
		lea ecx, [esp-12] ;buffer to store read byte
		mov dl, 1	;read 1 byte
		mov al, 3
		int 0x80	;syscall read
		dec esi
		or esi, esi	;cmp esi to 0 
		je end_verify	;at final byte, end verification
		mov al, [esp-8]
		cmp byte [esp-12],al
		je verify_loop	 ;keep looping if expected value found
		jmp write_verify ;if byte!=expected value, restart

	end_verify:
	mov esi, edi
	mov al, [esp-8]
	mov byte [esp-8],0x1	;set new byte to write to 1
	cmp al, 0x1
	jne write_verify	;if old byte to write!=1, goto start
	
	pop eax			;reset stack
	ret

オンラインでお試しください!(一時ファイルを使用)

移動するレジスタを最適化し、メモリ内の一定の場所ではなくバッファとしてスタックを使用することにより、-11バイト。


3

Linuxシステムでは、デバイスを特別に処理する必要はありません。デバイスファイルインターフェイスを使用するだけです。

Python 3(バイト文字列)-141バイト

d=input()
f=open(d,'r+b')
z=f.seek
z(0,2)
s=f.tell()
i=0
while i<2:
 z(0);f.write([b'\0',b'\xff'][i]*s);f.flush();z(0)
 if f.read()==x:i+=1

かなり単純で、あまり最適化されていませんが、機能します。基本的な概要は次のとおりです。

  • 入力を取得(デバイスファイルパス)
  • デバイスファイルを開く
  • 最後まで探して、ファイルサイズを取得します(ブロックデバイスは常に実際のサイズです)
  • 書き込みとチェックのループに入る
  • 0ビットと1ビットの文字列(x)を作成する
  • ビット文字列を書き込む
  • フラッシュ出力(バッファリング= 0を設定することもできますが、これはより短いです)
  • xに対してファイルをテストし、通過した場合はループのステップを増分します

インクリメントが十分に高いときにループを終了する

おまけとして、より強力な上書き効果のための0x55 / 0xaaのように、任意のセットとバイト変更パターンの数に対してこれを変更できます。

ループバックを使用して、デバイスファイルで実際にテストしました。ただし、チェックが実際に機能するかどうかは100%わかりません。バッファリングの動作により、パスごとにファイルを閉じて再度開く必要がある場合があります。フラッシュがこれを防ぐことを願っています。

*コメントにいくつかの提案を組み込むように編集


1
サイトへようこそ。あなたは確か=にPythonの周りに空白を必要としません。を使用;してインデントを減らすことにより、バイト数を減らすこともできます。
— アドホックガーフハンター2018

通常、送信は文字ではなくバイトでカウントされます。関数の一部にエイリアスを設定することもできます。たとえば、f.seek(0);f.seek(0)(19バイト)はs=f.seek;s(0);s(0)(18バイト)にすることができます。さらに、if f.read()==x:i+=1することができますi+=f.read()==x。
— Jonathan Frech、2018年

また、入力の引数として空の文字列は必要ありません。
— Quintec、2018年

b'\0'代わりにb'\x00'働くべきだと思います。
— ジェイソン

重要な機能を実現しました。このプログラムは、ワイプされたデバイスのサイズに等しいRAMを消費します。
— ウィリアムシプリー

2

C(clang)、-DZ=lseek(d,0+ 139 = 152バイト

g,j,x,d,s,i,c;f(n){x=0;d=open(n,2);s=Z,2);for(j=2;j--;x++){Z,0);for(i=s;i--;write(d,&x,1));Z,0);for(i=s;i--;read(d,&c,1),c!=x&&x--&&j--);}}

オンラインでお試しください!

引数としてファイル名を取ります

非ゴルフ:

#include <unistd.h>
int g,j,x,d,s,i,c;
void f(char*n){
	x=0; /*Byte to write*/
	d=open(n,O_RDWR);
	s=lseek(d,0,SEEK_END); /*Get size of file*/
	j=0;
	for(j=0;j<2;j++){
		/*Write Pass*/
		lseek(d,0,SEEK_SET); /*Start writing from start of file*/
		for(i=0;i<s;i++){
			write(d,&x,1);
		}
		
		/*Verification Pass*/
		lseek(d,0,SEEK_SET);
		for(i=0;i<s;i++){
			read(d,&c,1);
			if(c!=x)x--,j--; /*If verification fails, repeat loop with the same value*/
		}
		x++;
	}
}

うーん... TiOはインターネットを一掃しますか?;-D
— Titus、

@Titus一時ファイルを作成してワイプします。
— Logern、2018年

1

Tcl、286バイト

proc f {o i l} {seek $o 0
set s [string repeat $i $l]
puts -nonewline $o $s
flush $o
seek $o 0
return [string equal [read $o $l] $s]}
set n [open $argv r+]
fconfigure $n -translation binary
seek $n 0 end
set m [tell $n]
while {![f $n "\0" $m]} {}
while {![f $n "\xff" $m]} {}

あまり最適化されていません。できることは試しましたが、Tclについてはあまり知りません。

「f.tcl」として保存し、Unixで実行しtclsh f.tcl "your filename"ます。引数が1つだけあることを確認してください。これをプレーンファイルでテストしましたが、デバイスファイルでも動作するはずです。

変数の設定とインデックス付けはTclでより複雑なので、パス間の共通コードを関数に入れることにしました。次に、最初に「\ 0」で呼び出し、確認に失敗するまで繰り返します。「\ xff」でも同じことをしています。

書き込み後にフラッシュしました。必要ないかもしれません。fconfigure -translation binary -buffering none長いです。

周りの引用符を削除して-2バイトr+。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.