2

I have a string which looks something like

/6435045045/hpdqbhflyuhv_EOlXG

I want to corrupt the string by changing a few characters. I did

$string | sed 's/E/A/g' 

it worked fine but the string is dynamic and now the string is generated without an 'E' in it. Is it possible to corrupt the string by replacing first x characters with some random x characters using bash ??

msd_2
  • 1,117
  • 3
  • 16
  • 22

3 Answers3

2
> string="/6435045045/hpdqbhflyuhv_EOlXG"
> n=8
> m=`expr ${#string} - $n`
> string="`< /dev/urandom tr -dc A-Za-z0-9_ | head -c$n``echo $string | tail -c$m`"
> echo $string
oZeHdOD_45/hpdqbhflyuhv_EOlXG
Community
  • 1
  • 1
andrew cooke
  • 45,717
  • 10
  • 93
  • 143
0

How rotten do you want to make the string?

tr 'A-Za-z' 'N-ZA-Mn-za-m' <<<$string
David W.
  • 105,218
  • 39
  • 216
  • 337
0

This is one script in bash. It allows you to specify a character range and the number of times the contents of the string would be changed.

#!/bin/bash

# syntax: corrupt <string> <times> <character range in ascii codes>
#
function corrupt {
    local STRING=$1
    local STRING_LENGTH=${#1}

    local TIMES=$2

    local RANGE=("${@:3}")
    local RANGE_LENGTH="${#RANGE[@]}"

    local CHARS=()
    local C P I=0 IFS=''

    while read -rd $'\0' C; do
        CHARS[I++]=$C
    done < <(echo -ne "$(printf '\\x%x\\x00' "${RANGE[@]}")")

    for (( I = 0; I < TIMES; ++I )); do
        C=${CHARS[RANDOM % RANGE_LENGTH]}
        P=$(( RANDOM % STRING_LENGTH ))
        STRING="${STRING:0:P}${C}${STRING:P + 1}"
    done

    echo "$STRING"

    # Uncomment to save the value to variable __.
    # __=$STRING
}

corrupt "/6435045045/hpdqbhflyuhv_EOlXG" 10 {32..126}

Note: Increase the number (times) to a higher value to get more devastating results.

konsolebox
  • 72,135
  • 12
  • 99
  • 105