Skip to content

Commit

Permalink
Move the Moodifications from SSH-Fuzzer to ModifiableVaraible.
Browse files Browse the repository at this point in the history
SSH-Fuzzer hat defined some modificiations using the
explicitValueModificationGenerator() method. It makes not much sense for
me to define them not well structured in the SSH-Fuzzer (and abusing explicit
modifications for this).

The Mutations that got moved over are:
- Append
- Insert
- Prepend
for Integer, BigInteger, ByteArray and Long
These Modifications do currently produce no usefull values for negative
parameters, but if someone needs this, he should revise this. For our
work, we mostly need only positiv values.

I also added the missing modifications for long, since SFTP uses longs.
And also added multiply modifiaction to integer.
  • Loading branch information
C0D3D3V committed Nov 21, 2024
1 parent 8599859 commit 9789ee7
Show file tree
Hide file tree
Showing 37 changed files with 1,660 additions and 104 deletions.
77 changes: 51 additions & 26 deletions src/main/java/de/rub/nds/modifiablevariable/ModifiableVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,12 @@
*/
package de.rub.nds.modifiablevariable;

import de.rub.nds.modifiablevariable.biginteger.BigIntegerAddModification;
import de.rub.nds.modifiablevariable.biginteger.BigIntegerExplicitValueModification;
import de.rub.nds.modifiablevariable.biginteger.BigIntegerInteractiveModification;
import de.rub.nds.modifiablevariable.biginteger.BigIntegerMultiplyModification;
import de.rub.nds.modifiablevariable.biginteger.BigIntegerShiftLeftModification;
import de.rub.nds.modifiablevariable.biginteger.BigIntegerShiftRightModification;
import de.rub.nds.modifiablevariable.biginteger.BigIntegerSubtractModification;
import de.rub.nds.modifiablevariable.biginteger.BigIntegerXorModification;
import de.rub.nds.modifiablevariable.biginteger.*;
import de.rub.nds.modifiablevariable.bool.BooleanExplicitValueModification;
import de.rub.nds.modifiablevariable.bool.BooleanToggleModification;
import de.rub.nds.modifiablevariable.bytearray.ByteArrayDeleteModification;
import de.rub.nds.modifiablevariable.bytearray.ByteArrayDuplicateModification;
import de.rub.nds.modifiablevariable.bytearray.ByteArrayExplicitValueModification;
import de.rub.nds.modifiablevariable.bytearray.ByteArrayInsertModification;
import de.rub.nds.modifiablevariable.bytearray.ByteArrayShuffleModification;
import de.rub.nds.modifiablevariable.bytearray.ByteArrayXorModification;
import de.rub.nds.modifiablevariable.integer.IntegerAddModification;
import de.rub.nds.modifiablevariable.integer.IntegerExplicitValueModification;
import de.rub.nds.modifiablevariable.integer.IntegerShiftLeftModification;
import de.rub.nds.modifiablevariable.integer.IntegerShiftRightModification;
import de.rub.nds.modifiablevariable.integer.IntegerSubtractModification;
import de.rub.nds.modifiablevariable.integer.IntegerXorModification;
import de.rub.nds.modifiablevariable.longint.LongAddModification;
import de.rub.nds.modifiablevariable.longint.LongExplicitValueModification;
import de.rub.nds.modifiablevariable.longint.LongSubtractModification;
import de.rub.nds.modifiablevariable.longint.LongXorModification;
import de.rub.nds.modifiablevariable.bytearray.*;
import de.rub.nds.modifiablevariable.integer.*;
import de.rub.nds.modifiablevariable.longint.*;
import de.rub.nds.modifiablevariable.singlebyte.ByteAddModification;
import de.rub.nds.modifiablevariable.singlebyte.ByteExplicitValueModification;
import de.rub.nds.modifiablevariable.singlebyte.ByteSubtractModification;
Expand Down Expand Up @@ -88,6 +68,15 @@ public abstract class ModifiableVariable<E> implements Serializable {
@XmlElement(
type = BigIntegerMultiplyModification.class,
name = "BigIntegerMultiplyModification"),
@XmlElement(
type = BigIntegerAppendValueModification.class,
name = "BigIntegerAppendValueModification"),
@XmlElement(
type = BigIntegerInsertValueModification.class,
name = "BigIntegerInsertValueModification"),
@XmlElement(
type = BigIntegerPrependValueModification.class,
name = "BigIntegerPrependValueModification"),
@XmlElement(
type = BooleanToggleModification.class,
name = "BooleanToggleModification"),
Expand All @@ -101,8 +90,14 @@ public abstract class ModifiableVariable<E> implements Serializable {
type = ByteArrayShuffleModification.class,
name = "ByteArrayShuffleModification"),
@XmlElement(
type = ByteArrayInsertModification.class,
name = "ByteArrayInsertModification"),
type = ByteArrayAppendValueModification.class,
name = "ByteArrayAppendValueModification"),
@XmlElement(
type = ByteArrayInsertValueModification.class,
name = "ByteArrayInsertValueModification"),
@XmlElement(
type = ByteArrayPrependValueModification.class,
name = "ByteArrayPrependValueModification"),
@XmlElement(
type = ByteArrayExplicitValueModification.class,
name = "ByteArrayExplicitValueModification"),
Expand All @@ -116,6 +111,9 @@ public abstract class ModifiableVariable<E> implements Serializable {
@XmlElement(
type = IntegerSubtractModification.class,
name = "IntegerSubtractModification"),
@XmlElement(
type = IntegerMultiplyModification.class,
name = "IntegerMultiplyModification"),
@XmlElement(
type = IntegerShiftRightModification.class,
name = "IntegerShiftRightModification"),
Expand All @@ -126,13 +124,40 @@ public abstract class ModifiableVariable<E> implements Serializable {
type = IntegerExplicitValueModification.class,
name = "IntegerExplicitValueModification"),
@XmlElement(type = IntegerAddModification.class, name = "IntegerAddModification"),
@XmlElement(
type = IntegerAppendValueModification.class,
name = "IntegerAppendValueModification"),
@XmlElement(
type = IntegerInsertValueModification.class,
name = "IntegerInsertValueModification"),
@XmlElement(
type = IntegerPrependValueModification.class,
name = "IntegerPrependValueModification"),
@XmlElement(type = LongXorModification.class, name = "LongXorModification"),
@XmlElement(
type = LongSubtractModification.class,
name = "LongSubtractModification"),
@XmlElement(
type = LongExplicitValueModification.class,
name = "LongExplicitValueModification"),
@XmlElement(
type = LongAppendValueModification.class,
name = "LongAppendValueModification"),
@XmlElement(
type = LongInsertValueModification.class,
name = "LongInsertValueModification"),
@XmlElement(
type = LongPrependValueModification.class,
name = "LongPrependValueModification"),
@XmlElement(
type = LongMultiplyModification.class,
name = "LongMultiplyModification"),
@XmlElement(
type = LongShiftLeftModification.class,
name = "LongShiftLeftModification"),
@XmlElement(
type = LongShiftRightModification.class,
name = "LongShiftRightModification"),
@XmlElement(type = LongAddModification.class, name = "LongAddModification"),
@XmlElement(type = ByteXorModification.class, name = "ByteXorModification"),
@XmlElement(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* ModifiableVariable - A Variable Concept for Runtime Modifications
*
* Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH
*
* Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0
*/
package de.rub.nds.modifiablevariable.biginteger;

import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;
import java.math.BigInteger;
import java.util.Objects;
import java.util.Random;

@XmlRootElement
@XmlType(propOrder = {"appendValue", "modificationFilter"})
@XmlAccessorType(XmlAccessType.FIELD)
public class BigIntegerAppendValueModification extends VariableModification<BigInteger> {

private static final int MAX_APPEND_LENGTH = 8;

private BigInteger appendValue;

public BigIntegerAppendValueModification() {}

public BigIntegerAppendValueModification(BigInteger bi) {
this.appendValue = bi;
}

@Override
protected BigInteger modifyImplementationHook(BigInteger input) {
if (input == null) {
input = BigInteger.ZERO;
}
return input.shiftLeft(appendValue.bitLength()).add(appendValue);
}

public BigInteger getAppendValue() {
return appendValue;
}

public void setAppendValue(BigInteger appendValue) {
this.appendValue = appendValue;
}

@Override
public VariableModification<BigInteger> getModifiedCopy() {
return new BigIntegerAppendValueModification(appendValue.add(new BigInteger(MAX_APPEND_LENGTH, new Random())));
}

@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + Objects.hashCode(this.appendValue);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final BigIntegerAppendValueModification other = (BigIntegerAppendValueModification) obj;
return Objects.equals(this.appendValue, other.appendValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* ModifiableVariable - A Variable Concept for Runtime Modifications
*
* Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH
*
* Licensed under Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0
*/
package de.rub.nds.modifiablevariable.biginteger;

import de.rub.nds.modifiablevariable.VariableModification;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;
import java.math.BigInteger;
import java.util.Objects;
import java.util.Random;

@XmlRootElement
@XmlType(propOrder = {"insertValue", "startPosition", "modificationFilter"})
@XmlAccessorType(XmlAccessType.FIELD)
public class BigIntegerInsertValueModification extends VariableModification<BigInteger> {

private static final int MAX_INSERT_LENGTH = 8;
private static final int MAX_POSITION_MODIFIER = 32;

private BigInteger insertValue;
private int startPosition;

public BigIntegerInsertValueModification() {}

public BigIntegerInsertValueModification(BigInteger bi, int startPosition) {
this.insertValue = bi;
this.startPosition = startPosition;
}

@Override
protected BigInteger modifyImplementationHook(BigInteger input) {
if (input == null) {
input = BigInteger.ZERO;
}
int originalValueLength = input.bitLength();
int insertValueLength = insertValue.bitLength();
int insertPosition = startPosition;
if (startPosition > originalValueLength) {
insertPosition = originalValueLength;
} else if (startPosition < 0) {
insertPosition = 0;
}
BigInteger mask = BigInteger.valueOf((1L << insertPosition) - 1);

return input.shiftRight(insertPosition)
.shiftLeft(insertValueLength)
.and(insertValue)
.shiftLeft(insertPosition)
.add(mask.and(input));
}

public BigInteger getInsertValue() {
return insertValue;
}

public void setInsertValue(BigInteger insertValue) {
this.insertValue = insertValue;
}

public int getStartPosition() {
return startPosition;
}

public void setStartPosition(int startPosition) {
this.startPosition = startPosition;
}

@Override
public VariableModification<BigInteger> getModifiedCopy() {
Random r = new Random();

if (r.nextBoolean()) {
return new BigIntegerInsertValueModification(
insertValue.add(new BigInteger(MAX_INSERT_LENGTH, r)), startPosition);
} else {
int modifier = r.nextInt(MAX_POSITION_MODIFIER);
if (r.nextBoolean()) {
modifier *= -1;
}
modifier = startPosition + modifier;
if (modifier <= 0) {
modifier = 1;
}
return new BigIntegerInsertValueModification(insertValue, modifier);
}
}

@Override
public int hashCode() {
int hash = 7;
hash = 58 * hash + Objects.hashCode(this.insertValue);
hash = 58 * hash + Objects.hashCode(this.startPosition);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final BigIntegerInsertValueModification other = (BigIntegerInsertValueModification) obj;
if (this.startPosition != other.startPosition) {
return false;
}
return Objects.equals(this.insertValue, other.insertValue);
}
}
Loading

0 comments on commit 9789ee7

Please sign in to comment.