DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. HTTP Server + Groovlet = A Back-Door Type of Trick When Using A Java App

HTTP Server + Groovlet = A Back-Door Type of Trick When Using A Java App

Pavel Bernshtam user avatar by
Pavel Bernshtam
·
Aug. 13, 12 · Interview
Like (0)
Save
Tweet
Share
7.30K Views

Join the DZone community and get the full member experience.

Join For Free
recently i saw an article about simple groovlet . groovlet allows you to execute any code in your server. it looks very useful for creating a backdoor - for debugging, troubleshooting, etc.

my problem, however, is that we don't just have a server, but we also have a swing-rich client who is unable to execute groovlets. so i tried to create something like this in a standalone application without adding to the application-embedded servlet engine (jetty or tomcat).

luckily, i just read an article about a simple http server inside a standard java library. i decided to use some of its ideas.

let's start from main:
public class main {
    public static void main(string[] args) {
        // you can set port number from command line or leave it hardcoded
        httpbackdoorrunner runner = new httpbackdoorrunner(18999, true);
        runner.start();
    }
}
let's continue in groovy because it is simpler :)

import com.sun.net.httpserver.httpserver
import java.util.concurrent.executors

/**
* this class starts the server with our handler (embedded server is not servlet container!)
*/
class httpbackdoorrunner {

    final int port
    final boolean silent

    httpbackdoorrunner(int port, boolean silent) {
        this.port = port
        this.silent = silent
    }


    def start() {
        try {
            inetsocketaddress addr = new inetsocketaddress(port);
            httpserver server = httpserver.create(addr, 0);

            server.createcontext("/", new backdoorhandler());
            server.setexecutor(executors.newcachedthreadpool(
            ));
            server.start();
        }
        catch(exception e) {
            if (silent) {
                // ignore
            }
            else throw new runtimeexception(e)
        }
    }
}

now - the http requests handler:

/**
*  i need this abstraction level to reuse script running code in groovlet and standalone backdoor
* the way, you receive script text is different in those two situations
*/
class backdoorscriptrunner {

    void runscript(script, responsebody, uri) {
        def scriptoutput = new bytearrayoutputstream()

        if (script) {
            // redirect output
            def saveout = system.out

            def stream = new printstream(scriptoutput)
            system.out = stream
            try{
                def result = new groovyshell().run(script, "dynamic.groovy");
            }
            catch (throwable e) {
                e.printstacktrace(stream);
            }
            system.out = saveout

        }
        responsebody.println createhtml(uri, script, scriptoutput)
        responsebody.close();
    }

    string createhtml(uri, script, scriptoutput) {
        """
                   < form action="${uri}" method="post">
                   < h2>backdoor
                   code comes here:   
< textarea cols="120" rows="5" name="groovyscript"> ${script ? script : ""}< /textarea> < br> < input type="submit" value="go!" /> < /form> < br> ${scriptoutput.tostring() ? "< h2>output< /h2>< pre>${scriptoutput}< /pre>" : ""} """ } }

and now - start it up!
Java (programming language) app

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Public Cloud-to-Cloud Repatriation Trend
  • A Real-Time Supply Chain Control Tower Powered by Kafka
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • The Top 3 Challenges Facing Engineering Leaders Today—And How to Overcome Them

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: