Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breakout writer #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/java/gadgetinspector/GadgetChain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package gadgetinspector;

import java.util.List;
import java.util.ArrayList;
import java.io.Writer;
import java.io.IOException;

class GadgetChain {
public final List<GadgetChainLink> links;

public GadgetChain(List<GadgetChainLink> links) {
this.links = links;
}

public GadgetChain(GadgetChain gadgetChain, GadgetChainLink link) {
List<GadgetChainLink> links = new ArrayList<GadgetChainLink>(gadgetChain.links);
links.add(link);
this.links = links;
}

public void write(Writer writer) throws IOException {
writer.write(String.format("%s.%s%s (%d)%n",
this.links.get(0).method.getClassReference().getName(),
this.links.get(0).method.getName(),
this.links.get(0).method.getDesc(),
this.links.get(0).taintedArgIndex));
for (int i = 1; i < this.links.size(); i++) {
writer.write(String.format(" %s.%s%s (%d)%n",
this.links.get(i).method.getClassReference().getName(),
this.links.get(i).method.getName(),
this.links.get(i).method.getDesc(),
this.links.get(i).taintedArgIndex));
}
writer.write("\n");
}

}
69 changes: 2 additions & 67 deletions src/main/java/gadgetinspector/GadgetChainDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public GadgetChainDiscovery(GIConfig config) {
this.config = config;
}

public void discover() throws Exception {
public Set<GadgetChain> discover() throws Exception {
Map<MethodReference.Handle, MethodReference> methodMap = DataLoader.loadMethods();
InheritanceMap inheritanceMap = InheritanceMap.load();
Map<MethodReference.Handle, Set<MethodReference.Handle>> methodImplMap = InheritanceDeriver.getAllMethodImplementations(
Expand Down Expand Up @@ -128,72 +128,7 @@ public void discover() throws Exception {
}
}

try (OutputStream outputStream = Files.newOutputStream(Paths.get("gadget-chains.txt"));
Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
for (GadgetChain chain : discoveredGadgets) {
printGadgetChain(writer, chain);
}
}

LOGGER.info("Found {} gadget chains.", discoveredGadgets.size());
}

private static void printGadgetChain(Writer writer, GadgetChain chain) throws IOException {
writer.write(String.format("%s.%s%s (%d)%n",
chain.links.get(0).method.getClassReference().getName(),
chain.links.get(0).method.getName(),
chain.links.get(0).method.getDesc(),
chain.links.get(0).taintedArgIndex));
for (int i = 1; i < chain.links.size(); i++) {
writer.write(String.format(" %s.%s%s (%d)%n",
chain.links.get(i).method.getClassReference().getName(),
chain.links.get(i).method.getName(),
chain.links.get(i).method.getDesc(),
chain.links.get(i).taintedArgIndex));
}
writer.write("\n");
}

private static class GadgetChain {
private final List<GadgetChainLink> links;

private GadgetChain(List<GadgetChainLink> links) {
this.links = links;
}

private GadgetChain(GadgetChain gadgetChain, GadgetChainLink link) {
List<GadgetChainLink> links = new ArrayList<GadgetChainLink>(gadgetChain.links);
links.add(link);
this.links = links;
}
}

private static class GadgetChainLink {
private final MethodReference.Handle method;
private final int taintedArgIndex;

private GadgetChainLink(MethodReference.Handle method, int taintedArgIndex) {
this.method = method;
this.taintedArgIndex = taintedArgIndex;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

GadgetChainLink that = (GadgetChainLink) o;

if (taintedArgIndex != that.taintedArgIndex) return false;
return method != null ? method.equals(that.method) : that.method == null;
}

@Override
public int hashCode() {
int result = method != null ? method.hashCode() : 0;
result = 31 * result + taintedArgIndex;
return result;
}
return discoveredGadgets;
}

/*
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/gadgetinspector/GadgetChainLink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gadgetinspector;

import gadgetinspector.data.MethodReference;

class GadgetChainLink {
public final MethodReference.Handle method;
public final int taintedArgIndex;

GadgetChainLink(MethodReference.Handle method, int taintedArgIndex) {
this.method = method;
this.taintedArgIndex = taintedArgIndex;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

GadgetChainLink that = (GadgetChainLink) o;

if (taintedArgIndex != that.taintedArgIndex) return false;
return method != null ? method.equals(that.method) : that.method == null;
}

@Override
public int hashCode() {
int result = method != null ? method.hashCode() : 0;
result = 31 * result + taintedArgIndex;
return result;
}
}
20 changes: 16 additions & 4 deletions src/main/java/gadgetinspector/GadgetInspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Set;
import java.io.Writer;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

/**
* Main entry point for running an end-to-end analysis. Deletes all data files before starting and writes discovered
Expand Down Expand Up @@ -118,12 +123,19 @@ public static void main(String[] args) throws Exception {
sourceDiscovery.save();
}

{
LOGGER.info("Searching call graph for gadget chains...");
GadgetChainDiscovery gadgetChainDiscovery = new GadgetChainDiscovery(config);
gadgetChainDiscovery.discover();
LOGGER.info("Searching call graph for gadget chains...");
GadgetChainDiscovery gadgetChainDiscovery = new GadgetChainDiscovery(config);
Set<GadgetChain> discoveredGadgets = gadgetChainDiscovery.discover();

try (OutputStream outputStream = Files.newOutputStream(Paths.get("gadget-chains.txt"));
Writer writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
for (GadgetChain chain : discoveredGadgets) {
chain.write(writer);
}
}

LOGGER.info("Found {} gadget chains.", discoveredGadgets.size());

LOGGER.info("Analysis complete!");
}

Expand Down