{"id":285,"date":"2010-08-17T20:59:02","date_gmt":"2010-08-18T01:59:02","guid":{"rendered":"http:\/\/hackarandas.com\/blog\/?p=285"},"modified":"2010-08-18T17:54:51","modified_gmt":"2010-08-18T22:54:51","slug":"uncomplicated-wipe-for-nix","status":"publish","type":"post","link":"https:\/\/hackarandas.com\/blog\/2010\/08\/17\/uncomplicated-wipe-for-nix\/","title":{"rendered":"Uncomplicated File Wipe for *NIX"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/hackarandas.com\/blog\/wp-content\/uploads\/2010\/08\/file-shredding_medium.jpeg\" alt=\"\" title=\"File Shredding\" width=\"193\" height=\"240\" class=\"alignleft size-full wp-image-291\" style=\"margin: 10px 10px 0pt 0pt; float: left;\" \/> We needed to guarantee to one of our customers that a file will be securely deleted. Since the server was a HPUX Unix and we can&#8217;t compile nor install new applications, I managed to write this script to wipe the file.<\/p>\n<p>The file is overwritten 7 times as the <a href=\"http:\/\/www.usaid.gov\/policy\/ads\/500\/d522022m.pdf\">US Department of Defense clearing standard DoD 5220.22-M<\/a> specifies and renamed another 7 times before being deleted. It is written for the KSH shell as many UNIX has it by default. It doesn&#8217;t  run in bash but you can edit it to fit your needs.<\/p>\n<p>Here is the code:<\/p>\n<pre lang=\"bash\">\r\n#!\/usr\/bin\/ksh\r\n# Script by Adrian Puente Z..\r\n# Powered by Hackarandas www.hackarandas.com\r\n# Licensed by GNU GPLv3\r\n# http:\/\/www.gnu.org\/licenses\/gpl-3.0.txt\r\n\r\n# US Department of Defense clearing standard DOD 5220.22-M (ECE)\r\nPASES=7\r\n# Device to overwrite the file.\r\n# Can be:\r\n# \/dev\/random\r\n# \/dev\/urandom\r\n# \/dev\/zero (less secure, overwritten with zeros)\r\nRANDEV=\/dev\/urandom\r\nNAME=$$\r\nCOUNT=0\r\nFILE=$1\r\n\r\nif [[ $# -eq 0 ]];then\r\n\tprint \"Syntax: $0 <file to wipe>\"\r\n\texit 1\r\nfi\r\n\r\nif [[ ! -f $FILE ]]\r\nthen\r\n\tprint \"File $FILE doesn't exists\"\r\n\texit 1\r\nfi\r\n\r\nif [[ ! -w $FILE ]]\r\nthen\r\n\tprint \"Can't write on file $FILE\"\r\n\texit 1\r\nfi\r\n\r\nSIZE=$(ls -l $FILE | cut -d' ' -f5)\r\n\r\nprint -n \"About to wipe file: $FILE are you sure? \\\"N\/y\\\": \"\r\nread answer\r\nprint \"\"\r\n\r\nif [[ ! ( $answer = 'y' || $answer = 'Y' ) ]]\r\nthen\r\n\tprint \"Command canceled.\"\r\n\texit 0\r\nfi\r\n\r\nwhile [[ $COUNT -lt $PASES ]];do\r\n\t(( COUNT += 1 ))\r\n\tprint \"Pass number: $COUNT\"\r\n\tdd if=$RANDEV of=$FILE bs=$SIZE count=1\r\ndone\r\n\r\nCOUNT=0\r\necho \"Renaming...\"\r\n\r\nwhile [[ $COUNT -lt $PASES ]];do\r\n        (( COUNT += 1 ))\r\n        (( NAME += \"$NAME$COUNT\" ))\r\n\tmv -v $FILE $NAME\r\n\tFILE=$NAME\r\ndone\r\n\r\nrm -v $FILE\r\nFILE=$1\r\necho File: $FILE deleted.\r\nexit 0<\/pre>\n<p>The syntax is simple:<\/p>\n<pre lang=\"c\">\r\n      --.^       (ch0ks@xipe)*(20:38:05)*(~)      ^.--\r\n-=:)> uncomplicatedwipe.ksh \r\nSyntax: uncomplicatedwipe.ksh <file to wipe><\/pre>\n<p>You can follow this commands to test the script:<\/p>\n<pre lang=\"c\"> hexdump \/dev\/urandom > foo.txt \r\n#after some seconds press CTRL+C <\/pre>\n<p>Now we wipe the file<\/p>\n<pre lang=\"c\">\r\n      --.^       (ch0ks@xipe)*(20:36:00)*(tmp)      ^.--\r\n-=:)> uncomplicatedwipe.sh foo.txt \r\nAbout to wipe file: foo.txt are you sure? \"N\/y\": y\r\n\r\nPass number: 1\r\n1+0 records in\r\n1+0 records out\r\n15477760 bytes (15 MB) copied, 4.01637 s, 3.9 MB\/s\r\nPass number: 2\r\n1+0 records in\r\n1+0 records out\r\n15477760 bytes (15 MB) copied, 3.87637 s, 4.0 MB\/s\r\nPass number: 3\r\n1+0 records in\r\n1+0 records out\r\n15477760 bytes (15 MB) copied, 5.451 s, 2.8 MB\/s\r\nPass number: 4\r\n1+0 records in\r\n1+0 records out\r\n15477760 bytes (15 MB) copied, 4.48904 s, 3.4 MB\/s\r\nPass number: 5\r\n1+0 records in\r\n1+0 records out\r\n15477760 bytes (15 MB) copied, 3.88731 s, 4.0 MB\/s\r\nPass number: 6\r\n1+0 records in\r\n1+0 records out\r\n15477760 bytes (15 MB) copied, 3.98379 s, 3.9 MB\/s\r\nPass number: 7\r\n1+0 records in\r\n1+0 records out\r\n15477760 bytes (15 MB) copied, 3.2128 s, 4.8 MB\/s\r\nRenaming...\r\n`foo.txt' -> `69257'\r\n`69257' -> `761829'\r\n`761829' -> `8380122'\r\n`8380122' -> `92181346'\r\n`92181346' -> `1013994811'\r\n`1013994811' -> `11153942927'\r\n`11153942927' -> `122693372204'\r\nremoved `122693372204'\r\nFile: foo.txt deleted.<\/pre>\n<p>In the next release I will make a recursive version for directories and you can visit my <a href=\"http:\/\/hackarandas.com\/hacking-projects\/\">other projects here<\/a>.<\/p>\n<p><strong>Troubleshoot: <\/strong> Some Unix systems doesn&#8217;t have \/dev\/urandom device so you can play with the RANDEV variable to use the one you have.<\/p>\n<p><strong>Update: <\/strong> Some versions of HPUX doesn&#8217;t have \/dev\/[u]random so you can use as a desperate alternative the \/dev\/zero device. I found in a <a href=\"http:\/\/forums11.itrc.hp.com\/service\/forums\/questionanswer.do?threadId=75135\">forum<\/a> that some versions of HPUX doesn&#8217;t have the \/dev\/zero device so you can create it with this command:<\/p>\n<pre lang=\"bash\">\r\n#!\/bin\/sh\r\n\r\n# major\/minor for HPUX 11.X\r\nmknod \/dev\/zero c 3 4\r\nchown bin:bin \/dev\/zero\r\nchmod 666 \/dev\/zero<\/pre>\n<p><em>Adri\u00c3\u00a1n Puente Z.<\/em><\/p>\n<p><a href=\"http:\/\/www.technorati.com\/tag\/hackarandas\" rel=\"tag\">hackarandas<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/wipe\" rel=\"tag\">wipe<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/Adrian+Puente+Z.\" rel=\"tag\">Adrian Puente Z.<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/security\" rel=\"tag\">security<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/Secure+Delete\" rel=\"tag\">Secure Delete<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/unix\" rel=\"tag\">unix<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/security\" rel=\"tag\">security<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/seguridad\" rel=\"tag\">seguridad<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/ksh+code\" rel=\"tag\">ksh code<\/a><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>We needed to guarantee to one of our customers that a file will be securely deleted. Since the server was a HPUX Unix and we can&#8217;t compile nor install new applications, I managed to write this script to wipe the &hellip; <a href=\"https:\/\/hackarandas.com\/blog\/2010\/08\/17\/uncomplicated-wipe-for-nix\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,8,4],"tags":[23,50,48,79,33,49,51],"class_list":["post-285","post","type-post","status-publish","format-standard","hentry","category-code","category-hacking","category-security","tag-hackarandas","tag-ksh","tag-secure-delete","tag-79","tag-seguridad-informatica","tag-unix","tag-wipe"],"_links":{"self":[{"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/posts\/285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/comments?post=285"}],"version-history":[{"count":18,"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/posts\/285\/revisions"}],"predecessor-version":[{"id":301,"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/posts\/285\/revisions\/301"}],"wp:attachment":[{"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/media?parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/categories?post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hackarandas.com\/blog\/wp-json\/wp\/v2\/tags?post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}