programing

임의의 가비지 바이트를 파일에 쓰는 명령이 있습니까?

sourcetip 2021. 1. 17. 12:39
반응형

임의의 가비지 바이트를 파일에 쓰는 명령이 있습니까?


이제 내 응용 프로그램에 대한 몇 가지 테스트를 다시 수행하고 있습니다. 하지만 테스트 파일을 찾기가 어렵다는 것을 알았습니다.

그래서 어떤 형식의 파일에 임의 / 가비지 바이트를 쓸 수있는 기존 도구가 있는지 궁금합니다.

기본적으로이 도구가 필요합니다.

  1. 임의의 가비지 바이트를 파일에 씁니다.
  2. 파일의 형식을 알 필요가 없으며 임의의 바이트를 쓰는 것만으로도 괜찮습니다.
  3. 대상 파일의 임의의 위치에 쓰는 것이 가장 좋습니다.
  4. 일괄 처리도 보너스입니다.

감사.


/dev/urandom의사 장치와 함께 dd, 당신을 위해이 작업을 수행 할 수 있습니다 :

dd if=/dev/urandom of=newfile bs=1M count=10

newfile10M 크기 의 파일이 생성됩니다 .

/dev/random구축 충분한 임의성이없는 경우 장치는 종종 차단 urandom차단하지 않습니다. 암호화 등급 항목에 임의성을 사용하는 경우 urandom. 그 밖의 모든 경우에는 충분하고 더 빠를 가능성이 높습니다.

전체 파일이 아닌 파일의 일부만 손상 시키려면 C 스타일 임의 함수를 사용하면됩니다. 그냥 사용 rnd()오프셋 및 길이를 알아 내기 위해 n다음, 그것을 사용 n하여 파일을 덮어 쓸 수있는 임의 바이트를 잡아 번.


다음 Perl 스크립트는이 작업을 수행하는 방법을 보여줍니다 (C 코드 컴파일에 대해 걱정할 필요 없음).

use strict;
use warnings;

sub corrupt ($$$$) {
    # Get parameters, names should be self-explanatory.

    my $filespec = shift;
    my $mincount = shift;
    my $maxcount = shift;
    my $charset = shift;

    # Work out position and size of corruption.

    my @fstat = stat ($filespec);
    my $size = $fstat[7];
    my $count = $mincount + int (rand ($maxcount + 1 - $mincount));
    my $pos = 0;
    if ($count >= $size) {
        $count = $size;
    } else {
        $pos = int (rand ($size - $count));
    }

    # Output for debugging purposes.

    my $last = $pos + $count - 1;
    print "'$filespec', $size bytes, corrupting $pos through $last\n";

 

    # Open file, seek to position, corrupt and close.

    open (my $fh, "+<$filespec") || die "Can't open $filespec: $!";
    seek ($fh, $pos, 0);
    while ($count-- > 0) {
        my $newval = substr ($charset, int (rand (length ($charset) + 1)), 1);
        print $fh $newval;
    }
    close ($fh);
}

# Test harness.

system ("echo =========="); #DEBUG
system ("cp base-testfile testfile"); #DEBUG
system ("cat testfile"); #DEBUG
system ("echo =========="); #DEBUG

corrupt ("testfile", 8, 16, "ABCDEFGHIJKLMNOPQRSTUVWXYZ   ");

system ("echo =========="); #DEBUG
system ("cat testfile"); #DEBUG
system ("echo =========="); #DEBUG

It consists of the corrupt function that you call with a file name, minimum and maximum corruption size and a character set to draw the corruption from. The bit at the bottom is just unit testing code. Below is some sample output where you can see that a section of the file has been corrupted:

==========
this is a file with nothing in it except for lowercase
letters (and spaces and punctuation and newlines).
that will make it easy to detect corruptions from the
test program since the character range there is from
uppercase a through z.
i have to make it big enough so that the random stuff
will work nicely, which is why i am waffling on a bit.
==========
'testfile', 344 bytes, corrupting 122 through 135
==========
this is a file with nothing in it except for lowercase
letters (and spaces and punctuation and newlines).
that will make iFHCGZF VJ GZDYct corruptions from the
test program since the character range there is from
uppercase a through z.
i have to make it big enough so that the random stuff
will work nicely, which is why i am waffling on a bit.
==========

It's tested at a basic level but you may find there are edge error cases which need to be taken care of. Do with it what you will.


Just for completeness, here's another way to do it:

shred -s 10 - > my-file

Writes 10 random bytes to stdout and redirects it to a file. shred is usually used for destroying (safely overwriting) data, but it can be used to create new random files too. So if you have already have a file that you want to fill with random data, use this:

shred my-existing-file

You could read from /dev/random:

# generate a 50MB file named `random.stuff` filled with random stuff ...
dd if=/dev/random of=random.stuff bs=1000000 count=50

You can specify the size also in a human readable way:

# generate just 2MB ...
dd if=/dev/random of=random.stuff bs=1M count=2

ReferenceURL : https://stackoverflow.com/questions/3598622/is-there-a-command-to-write-random-garbage-bytes-into-a-file

반응형