#----------------------------------------------------------------------- # Copyright (C) 2005 Scott L. Miller (scott.l.miller@gmail.com) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA #----------------------------------------------------------------------- # Date: # 01/28/2005 SLM Initial release # ############################################################################ ############################################################################ # The string files can contain blank lines and comments where the comment # indicator '#' is the first non-blank character on the line. # # A rotation indicator must appear before the first string line, or zero # rotation distance is assumed, and a zero rotation value line is added to # the resulting output. # # Different rotational distances within a single input set can be handled. # # If the optional rotation degree is defined as non-zero on the command # line, then all the string rotations and the rotation indicators will be # adjusted by the given degree. # # If the optional rotation degree is not given, or is functionally # equivalent to zero, all obfuscated strings will be unobfuscated, and # all rotational degree indicators will be set to 0. # # Data lines must follow the following layout # Unobfuscated-String-handle (to-be-)obfuscated-string # # The unobfuscated string HANDLE will never be obfuscated, and the white # space between the strings can be a mixture of spaces and tabs. # # Spaces and tabs are illegal within either the handle or the string to be # obfuscated. ############################################################################ sub Usage { print < or: | $0 [rotation-degree] where: [rotation-degree] is an optional positive or negative integer indicating the amount to "rotate" the printable ascii table. If left off or equivalent to zero, the resulting strings will be unobfuscated. or piped in text is a "string file" generally containing passwords or SNMP community strings that are, or will be obfuscated by this script. See script comments, or a "string file" comment header for more information about the "string file" layout. EOF } ############################################################################ # pwdrot uses the idea of rot13 and expands the characters affected by the # rotations to include all 94 normal printable ascii characters. # ie. chr(33) '!' - chr(126) '~'. The rotations are thus mod 94. # # Default rotation if not supplied is 47 ############################################################################ sub pwdrot { my $pwd = shift; my $degree = (@_ > 0) ? ((shift) % 94) : 47; if ($degree == 0) { return $pwd; } if (length($pwd) == 0) { return $pwd; } $rangestr = "\\" . sprintf("%03lo",$degree+33) . "-\\176\\041-\\" . sprintf("%03lo",$degree+32); unless ($pwdrots{$degree}) { my $rangstr = "\\" . sprintf("%03lo",$degree+33) . "-\\176\\041-\\" . sprintf("%03lo",$degree+32); $pwdrots{$degree} = eval "sub { \$_[0] =~ tr[\041-\176][$rangstr]; }" } $pwdrots{$degree}->($pwd); return $pwd; } ############################################################################ # Replicate blank lines & comments in output, find rotation notations # characterized by a decimal number appearing alone on a line, make # adjustments to those notations, and either unobfuscate, or change the # strings' rotation as directed. ############################################################################ sub process_line { s/(^\s+)//; #remove indentation if any if(/^$/) { print $_; return; } #ignore blank lines if(/^#/) { print $_; return; } #ignore comments chomp; my ($result,$handle,$whitespace,$snmpstring); ($handle,$whitespace,$snmpstring) = split /(\s+)/; if(!defined($snmpstring)) { $curdist = int($_); if($deltadist == 0) { print "0\n"; } else { printf "%d\n",($curdist+$deltadist) % 94; } return; } if(!defined($curdist)) { print "# Rotational distance was undefined, assuming 0\n"; print "0\n"; $curdist = 0; } if($deltadist == 0) { $result = pwdrot( $snmpstring, $curdist * -1 ); } else { $result = pwdrot( $snmpstring, $deltadist); } print $handle . $whitespace . $result . "\n"; return; } ########################################################################### # Program begins here # # Read an optional rotation degree and process "string files" given via the # command line, or piped in. # ########################################################################### if( -t && scalar(@ARGV) == 0 ) { print " No file supplied!\n"; &Usage; exit 0; } local $deltadist; if(! -t) { $deltadist = (scalar(@ARGV) > 0) ? ((shift) % 94) : 0; } else { $deltadist = (scalar(@ARGV) > 1) ? ((shift) % 94) : 0; } if(! -t) { while() { &process_line; } } if( scalar(@ARGV) > 0 ) { while (<>) { &process_line; } } exit;