Development Guide

PDU

PDUs (Protocol Data Units) are units used to transmit data through PETEP. They contain operational data (tags, destination, connection, ...), data bytes and you can expand them by adding your own protocol-specific data.

PDU class

com.warxim.petep.core.pdu.PDU
/*
 * PEnetration TEsting Proxy (PETEP)
 * 
 * Copyright (C) 2020 Michal Válka
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */
/** PDU (Protocol Data Unit) is core unit for data processed in PETEP. */
@PetepAPI
public abstract class PDU {
  /** Parent proxy. */
  protected Proxy proxy;

  /** Parent connection. */
  protected Connection connection;

  /** PDU destination. */
  protected PduDestination destination;

  /** Interceptor in which was the PDU processed last time. */
  protected Interceptor lastInterceptor;

  /** PDU tags. */
  protected Set<String> tags;

  /** PDU constructor */
  public PDU(Proxy proxy, Connection connection, PduDestination destination, Set<String> tags) {
    this.proxy = proxy;
    this.destination = destination;
    this.connection = connection;
    this.tags = tags;
  }

  /*
   * TAGS
   */
  /** Does the PDU contain specified tag? */
  public boolean hasTag(String tag) {
    return tags.contains(tag);
  }

  /** Adds tag to the PDU. */
  public void addTag(String tag) {
    tags.add(tag);
  }

  /** Removes tag from the PDU. */
  public void removeTag(String tag) {
    tags.remove(tag);
  }

  /** Removes tag from the PDU. */
  public void addTags(Collection<String> tags) {
    this.tags.addAll(tags);
  }

  /*
   * GETTERS
   */
  /** Returns set of tags. */
  public Set<String> getTags() {
    return tags;
  }

  /** Returns parent proxy. */
  public Proxy getProxy() {
    return proxy;
  }

  /** Returns PDU destination. */
  public PduDestination getDestination() {
    return destination;
  }

  /** Returns connection. */
  public Connection getConnection() {
    return connection;
  }

  /** Returns interceptor. */
  public Interceptor getLastInterceptor() {
    return lastInterceptor;
  }

  /*
   * SETTERS
   */
  /** Sets parent proxy. */
  public void setProxy(Proxy proxy) {
    this.proxy = proxy;
  }

  /** Sets PDU destination. */
  public void setDestination(PduDestination destination) {
    this.destination = destination;
  }

  /** Sets connection. */
  public void setConnection(Connection connection) {
    this.connection = connection;
  }

  /** Sets interceptor. */
  public void setLastInterceptor(Interceptor interceptor) {
    this.lastInterceptor = interceptor;
  }

  /*
   * ABSTRACT METHODS
   */
  /** Returns buffer. */
  public abstract byte[] getBuffer();

  /** Returns size of data in the buffer. */
  public abstract int getSize();

  /** Returns the buffer and size of data in the buffer. */
  public abstract void setBuffer(byte[] buffer, int size);

  /** Resizes the buffer. */
  public abstract void resize(int size);

  /** Returns the charset of the PDU. */
  public abstract Charset getCharset();

  /** Sets the charset of the PDU. */
  public abstract void setCharset(Charset charset);

  /** Creates deep copy of the PDU. */
  public abstract PDU copy();
}

Default PDU class

You do not have to create your own PDU class and you can use or extend DefaultPdu class that builds PDU on top of byte arrays.

Do not forget to override copy method when you extend DefaultPdu.

com.warxim.petep.core.pdu.DefaultPdu
/*
 * PEnetration TEsting Proxy (PETEP)
 * 
 * Copyright (C) 2020 Michal Válka
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */
/** Default PDU implementation. */
@PetepAPI
public class DefaultPdu extends PDU {
  protected byte[] buffer;
  protected int size;
  protected Charset charset;

  /** Default PDU implementation constructor. */
  public DefaultPdu(
      Proxy proxy,
      Connection connection,
      PduDestination destination,
      byte[] buffer,
      int size,
      Set<String> tags) {
    super(proxy, connection, destination, tags);
    this.buffer = buffer;
    this.size = size;
    this.charset = Constant.DEFAULT_CHARSET;
  }

  public DefaultPdu(
      Proxy proxy,
      Connection connection,
      PduDestination destination,
      byte[] buffer,
      int size) {
    super(proxy, connection, destination, new HashSet<>());
    this.buffer = buffer;
    this.size = size;
    this.charset = Constant.DEFAULT_CHARSET;
  }

  /** Returns the buffer. */
  @Override
  public byte[] getBuffer() {
    return buffer;
  }

  /** Returns size of data in the buffer. */
  @Override
  public int getSize() {
    return size;
  }

  /** Sets the buffer and size of data inside the buffer. */
  @Override
  public void setBuffer(byte[] buffer, int size) {
    this.buffer = buffer;
    this.size = size;
  }

  /** Resizes the buffer (expands the buffer when needed, but does not shrink the buffer). */
  @Override
  public void resize(int size) {
    if (size <= buffer.length) {
      return;
    }

    byte[] newBuffer = new byte[size];

    // Copy content of original buffer to new one.
    System.arraycopy(buffer, 0, newBuffer, 0, this.size);

    buffer = newBuffer;
  }

  @Override
  public PDU copy() {
    PDU pdu = new DefaultPdu(proxy, connection, destination, buffer.clone(), size);

    pdu.addTags(tags);

    return pdu;
  }

  @Override
  public Charset getCharset() {
    return charset;
  }

  @Override
  public void setCharset(Charset charset) {
    this.charset = charset;
  }
}

PDU Queue class

PDU queues are blocking queues used in various places in PETEP.

com.warxim.petep.core.pdu.PduQueue
/*
 * PEnetration TEsting Proxy (PETEP)
 * 
 * Copyright (C) 2020 Michal Válka
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */
/* Queue of PDUs. */
@PetepAPI
public class PduQueue {
  private final BlockingQueue<PDU> queue;

  public PduQueue() {
    queue = new LinkedBlockingQueue<>();
  }

  /** Add PDU to queue. */
  public void add(PDU pdu) {
    queue.add(pdu);
  }

  /** Returns PDU from queue (blocks until there is PDU). */
  public PDU take() throws InterruptedException {
    return queue.take();
  }

  /** Returns queue size. */
  public int size() {
    return queue.size();
  }

  /** Clears queue. */
  public void clear() {
    queue.clear();
  }

  /** Returns true if the queue is empty. */
  public boolean isEmpty() {
    return queue.isEmpty();
  }

  /** Retrieves and removes the head of the queue. */
  public PDU poll() {
    return queue.poll();
  }
}
Získejte registraci domén s tld .online, .space, .store, .tech zdarma!
Stačí si k jedné z těchto domén vybrat hosting Plus nebo Mega a registraci domény od nás dostanete za 0 Kč!
Objednat