GitHub Enterprise SQL Injection
The GitHub Enterprise API endpoint for organisation pre-receive hooks passed the user-supplied sort parameter into an ActiveRecord order method, allowing SQL injection. An attacker holding a token with the admin:pre_receive_hook scope could inject SQL through that parameter.
Published on blog.orange.tw
GitHub Enterprise is the on-premises version of GitHub.com that you can deploy a whole GitHub service in your private network for businesses. You can get 45-days free trial and download the VM from enterprise.github.com.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
GitHub Enterprise is the on-premises version of GitHub.com that you can deploy a whole GitHub service in your private network for businesses. A 45-day free trial VM can be downloaded from enterprise.github.com. After deployment the VM presents the familiar GitHub interface.



The researcher found the VM interesting and decided to look deeper into it.
Setup
The starting point was port scanning. Nmap showed six exposed ports on the VM. With a little knocking and service grabbing, the picture looked like this:
22/tcpand9418/tcpseem likehaproxyand it forwards connections to a backend service calledbabeld80/tcpand443/tcpare the main GitHub services122/tcpis just a SSH service8443/tcpis management console of GitHub
The GitHub management console needs a password to log in. Once you have the password, you can add your SSH key and connect into the VM through 122/tcp.
With SSH into the VM, the whole system was examined and the service code base was found under the directory /data/. Changing to /data/ and trying to review the source code showed that it seemed encrypted.

Finding it
GitHub uses a custom library to obfuscate their source code. Searching for ruby_concealer.so on Google leads to a gist by a kind man. It simply replaces rb_f_eval with rb_f_puts in ruby_concealer.so and it works.
But to be a hacker, you can't just use it without knowing how it works, so the next step was IDA Pro.


As can be seen, it just uses Zlib::Inflate::inflate to decompress data and XOR with a following key. The vendor's own comment on this obfuscation reads:
This obfuscation is intended to discourage GitHub Enterprise customers from making modifications to the VM. We know this 'encryption' is easily broken.
So the scheme can easily be implemented independently.
After de-obfuscating all the code, the code reviewing process could start. Most of the code is written in Ruby (Ruby on Rails and Sinatra):
/data/github/looks like the application run under port80/tcp443/tcpand it looks like the real code base ofgithub.com,gist.github.comandapi.github.com/data/render/looks like real code base ofrender.githubusercontent.com/data/enterprise-manage/seems like the application run under port8443/tcp
GitHub Enterprise uses enterprise? and dotcom? to check whether the application is running under Enterprise Mode or GitHub dot com mode.
Finding the vulnerability took about one week, with the researcher not being familiar with Ruby. The rough schedule of the week:
- Day 1 - Setting VM
- Day 2 - Setting VM
- Day 3 - Learning Rails by code reviewing
- Day 4 - Learning Rails by code reviewing
- Day 5 - Learning Rails by code reviewing
- Day 6 - Yeah, I found a SQL Injection!
The SQL Injection vulnerability was found under the GitHub Enterprise PreReceiveHookTarget model. The root cause is in /data/github/current/app/model/pre_receive_hook_target.rb line 45.
Although there is a built-in ORM (called ActiveRecord in Rails) in Rails that prevents you from SQL Injection, there are so many misuse cases of ActiveRecord that may cause SQL Injection. More examples can be checked at Rails-sqli.org, which is good to learn about SQL Injection on Rails.
In this case, if the parameter of the method order can be controlled, a malicious payload can be injected into SQL.
Tracing up, sorted_by is called by /data/github/current/app/api/org_pre_receive_hooks.rb in line 61. There, params[:sort] is passed to scope.sorted_by, so a malicious payload can be injected into params[:sort].
Exploitation
Step 1: Obtain an access token
Before triggering this vulnerability, a valid access_token with admin:pre_receive_hook scope is needed to access the API. Fortunately, it can be obtained by following command:
Step 2: Trigger the injection
Once you get an access_token, the vulnerability can be triggered by:

Root cause
The mistake is a misuse of ActiveRecord's order method in the PreReceiveHookTarget model (/data/github/current/app/model/pre_receive_hook_target.rb, line 45). ActiveRecord protects against SQL Injection when used as intended, but passing user-controlled input into order allows the payload to reach the SQL statement. The API endpoint in /data/github/current/app/api/org_pre_receive_hooks.rb (line 61) passes params[:sort] straight to scope.sorted_by, so the sort parameter is the injection point.
Impact
An attacker who holds a valid access_token with the admin:pre_receive_hook scope can inject SQL through the sort parameter of the organisation pre-receive hooks API. The report does not describe the data that could be read or changed beyond the injection itself.
Fix
GitHub validated the issue and worked on a fix, and responded that the fix would be included with the next release of GitHub Enterprise. GitHub Enterprise 2.8.5 was released on 2017/01/05.
Timeline
-
2016/12/26 05:48 Report vulnerability to GitHub via HackerOne
-
2016/12/26 08:39 GitHub response that have validated issue and are working on a fix.
-
2016/12/26 15:48 Provide more vulneraiblity detail.
-
2016/12/28 02:44 GitHub response that the fix will included with next release of GitHub Enterprise.
-
2017/01/04 06:41 GitHub response that offer $5,000 USD reward.
-
2017/01/05 02:37 Asked Is there anything I should concern about if I want to post a blog?
-
2017/01/05 03:06 GitHub is very open mind and response that it's OK!
-
2017/01/05 07:06 GitHub Enterprise 2.8.5 released!
-
Look for other ActiveRecord
ordercalls that take a parameter name from the request (for example asortquery parameter). A positive result is a response whose ordering changes in a way that reflects injected SQL, or a database error. -
Review other ActiveRecord methods that accept raw SQL fragments (such as
wherewith string interpolation) in the same code base. A positive result is a query that returns rows it should not, or an error revealing the query. -
Check API endpoints that require elevated scopes such as
admin:pre_receive_hookfor similar parameter pass-through. A positive result is an injection reachable with a token of that scope.
What happened, in plain words
GitHub is a website where people keep and share computer code. Big companies often want the same thing but inside their own building, on their own machines, not on the public internet. GitHub sells a product for that, called GitHub Enterprise: a complete copy of GitHub that a company installs on its own server. The researcher downloaded the free trial version of that product, which arrives as a virtual machine (a whole computer packed into a single file that runs inside your own computer), and spent a week reading the code inside it. On day six they found a bug: a way to slip extra database instructions into a request through a sorting option, a class of bug called SQL Injection. They reported it to GitHub, GitHub worked on a fix and offered a reward, and a new release shipped.
This article walks through the same journey for someone who has never done security work: what was set up, how the code was made readable, how the bug was found, and how it was triggered.
Before
GitHub Enterprise is the on-premises version of GitHub.com. "On-premises" means the software runs on computers the customer owns, inside the customer's own network, instead of on GitHub's servers. A business can deploy a whole GitHub service this way. A 45-day free trial is available, and the virtual machine can be downloaded from enterprise.github.com.
After deployment, the virtual machine shows the familiar GitHub interface:



The screenshots show the deployed instance presenting the usual GitHub look. At this point the researcher had the entire GitHub environment running inside one virtual machine, which made it interesting enough to look deeper into.
Environment
Everything starts with port scanning. A computer on a network offers services through numbered doors called ports; a port scan is simply asking a machine, door by door, which ones are open and who answers. The researcher used Nmap, a well-known free tool for exactly this.
Nmap found six exposed ports on the virtual machine. With a little knocking and service grabbing (connecting to each open port and looking at what it says back, to guess which program is behind it), the picture looked like this:
22/tcpand9418/tcpseem likehaproxyand it forwards connections to a backend service calledbabeld80/tcpand443/tcpare the main GitHub services122/tcpis just a SSH service8443/tcpis management console of GitHub
What those lines mean:
haproxyis a piece of software that stands at the front door and passes visitors on to the program that actually does the work, like a receptionist forwarding calls.babeldis the name of the program behind it.- Ports
80and443are the standard doors for ordinary web traffic (unencrypted and encrypted respectively), so these are the main GitHub pages. - SSH is a way to open a text-only remote control session on another computer, the way a system administrator works on a server without sitting in front of it. Port
122offers that. - Port
8443is the management console, a private control panel for the product.
The GitHub management console needs a password to log in. Once you have the password, you can add your SSH key and connect into the virtual machine through 122/tcp. An SSH key is a pair of files, one kept secret on your computer and one registered with the server, that prove who you are without typing a password; adding your key to the console is how you authorise yourself for the SSH door.
With SSH into the virtual machine, the researcher examined the whole system and found that the service code base looks like it is under the directory /data/. A directory is just a folder. Changing to /data/ and trying to review the source code showed that it seemed encrypted:

The screenshot shows the contents of the code directory looking scrambled rather than readable. Source code is the human-written text that a program is built from; if you can read it, you can look for mistakes in it. Here the text was deliberately made unreadable.
Finding it
GitHub uses a custom library to obfuscate their source code. Obfuscation means deliberately making something hard to read while keeping it working, like writing a note in a private shorthand. Searching for ruby_concealer.so on Google leads to a gist by a kind man. A gist is a small public snippet of code hosted by GitHub itself. That snippet simply replaces rb_f_eval with rb_f_puts in ruby_concealer.so and it works.
In plain words: ruby_concealer.so is a small helper file that the Ruby interpreter loads to unscramble the code on the fly. The snippet swaps one internal instruction for another so that, instead of running the unscrambled code, the program prints it out. That turns the hidden source into readable text.
But to be a hacker, you can't just use a tool without knowing how it works, so the next step was IDA Pro. IDA Pro is a program for taking a compiled file apart and viewing its internal instructions, a bit like opening a sealed appliance to see the wiring.


As can be seen, it just uses Zlib::Inflate::inflate to decompress data and XOR with a following key. Two ideas are needed here:
- Decompression is the reverse of zipping a file: packed data is expanded back to its original size.
Zlib::Inflate::inflateis the Ruby function that does this. - XOR is a simple mathematical mixing operation on numbers. If you mix data with a secret number (the key) once, it becomes unreadable; mix it with the same key again and you get the original back. It is like a lock that opens with the same turn that closed it.
So the protection was: pack the code, then mix it with a key. The vendor's own comment on this obfuscation reads:
This obfuscation is intended to discourage GitHub Enterprise customers from making modifications to the VM. We know this 'encryption' is easily broken.
In other words, GitHub knew this was only a deterrent, not real protection. Because the recipe was now known, the scheme could easily be implemented independently, and all the code could be turned back into readable text.
After de-obfuscating all the code, the code reviewing process could start. Code review means reading source code line by line looking for mistakes, the way a proofreader reads a document looking for errors. Most of the code is written in Ruby, using two web frameworks called Ruby on Rails and Sinatra. A framework is a ready-made scaffolding that saves programmers from writing every part of a web application from scratch.
The directories mapped out like this:
/data/github/looks like the application run under port80/tcp443/tcpand it looks like the real code base ofgithub.com,gist.github.comandapi.github.com/data/render/looks like real code base ofrender.githubusercontent.com/data/enterprise-manage/seems like the application run under port8443/tcp
So each folder holds the program behind one set of doors: the main site and its API, the service that displays rendered files, and the management console.
GitHub Enterprise uses enterprise? and dotcom? to check whether the application is running under Enterprise Mode or GitHub dot com mode. These are simply yes/no checks inside the code that let one code base behave slightly differently depending on whether it is the customer-installed copy or the public website.
Finding the vulnerability took about one week, with the researcher not being familiar with Ruby. The rough schedule of the week:
- Day 1 - Setting VM
- Day 2 - Setting VM
- Day 3 - Learning Rails by code reviewing
- Day 4 - Learning Rails by code reviewing
- Day 5 - Learning Rails by code reviewing
- Day 6 - Yeah, I found a SQL Injection!
Two days went on getting the virtual machine running, three on learning the framework by reading its code, and the bug turned up on the sixth day.
The vulnerability
The SQL Injection vulnerability was found under the GitHub Enterprise PreReceiveHookTarget model. A model, in Rails, is the part of the code that represents one kind of thing stored in the database, such as a user or a repository; PreReceiveHookTarget is the model for the targets that pre-receive hooks apply to. A pre-receive hook is a check a company can configure so that certain pushes of code are validated before they are accepted.
The root cause is in /data/github/current/app/model/pre_receive_hook_target.rb line 45. The file path names the folder, then the file; line 45 is the exact line where the mistake sits.
What is SQL Injection? A database is an organised store of information, and programs talk to it in a language called SQL, for example "give me the rows sorted by name". SQL Injection means tricking the program into treating part of the user's input as SQL instructions rather than as plain data. It is like a form that asks for your name, where writing "and also open the safe" makes the clerk actually open the safe, because the clerk cannot tell your name from an order.
Although there is a built-in ORM (called ActiveRecord in Rails) in Rails that prevents you from SQL Injection, there are so many misuse cases of ActiveRecord that may cause SQL Injection. An ORM, object-relational mapper, is a layer that lets a programmer work with database rows as ordinary program objects and builds the SQL automatically. Used as intended, it keeps data and instructions separate, which is what stops injection. Misuse means handing it a piece of text that it will paste into the SQL as if it were an instruction.
More examples can be checked at Rails-sqli.org, which is good to learn about SQL Injection on Rails.
In this case, if the parameter of the method order can be controlled, a malicious payload can be injected into SQL. The order method is the one that decides how results are sorted, and a parameter is a value handed to a piece of code. If that value comes from the visitor, the visitor is effectively writing part of the SQL.
Tracing up, sorted_by is called by /data/github/current/app/api/org_pre_receive_hooks.rb in line 61. There, params[:sort] is passed to scope.sorted_by, so a malicious payload can be injected into params[:sort].
Unpacking that: params[:sort] is the value of the sort field in the incoming request, the same kind of value that appears in a web address after ?sort=. The API file takes that value and hands it to sorted_by, which eventually reaches the order method. So the sort field is the injection point, the exact place where a visitor's text flows into the database query.
Exploitation
Step 1: Obtain an access token
Before triggering this vulnerability, a valid access_token with admin:pre_receive_hook scope is needed to access the API. An API, application programming interface, is a set of web addresses meant for programs rather than people: you send a request and get structured data back. An access token is a long secret string that acts like a temporary pass card, proving which account is making the request. A scope is the list of permissions written on that pass card; admin:pre_receive_hook is the permission needed to manage pre-receive hooks.
Fortunately, the token can be obtained by following command:
Step 2: Trigger the injection
Once you get an access_token, the vulnerability can be triggered by:

The screenshot accompanies the trigger step. The report does not reproduce the exact command or request text, so there is nothing further to copy here.
Root cause
The mistake is a misuse of ActiveRecord's order method in the PreReceiveHookTarget model (/data/github/current/app/model/pre_receive_hook_target.rb, line 45). ActiveRecord protects against SQL Injection when used as intended, but passing user-controlled input into order allows the payload to reach the SQL statement. The API endpoint in /data/github/current/app/api/org_pre_receive_hooks.rb (line 61) passes params[:sort] straight to scope.sorted_by, so the sort parameter is the injection point.
In everyday terms: the sorting feature was built to accept a column name from the visitor and paste it into the database instruction. A visitor who sends something other than a column name gets their own text executed as part of the instruction.
Impact
An attacker who holds a valid access_token with the admin:pre_receive_hook scope can inject SQL through the sort parameter of the organisation pre-receive hooks API. The report does not describe the data that could be read or changed beyond the injection itself.
Fix
GitHub validated the issue and worked on a fix, which was included with the next release of GitHub Enterprise. GitHub Enterprise 2.8.5 was released on 2017/01/05.
Timeline
- 2016/12/26 05:48 Report vulnerability to GitHub via HackerOne
- 2016/12/26 08:39 GitHub response that have validated issue and are working on a fix.
- 2016/12/26 15:48 Provide more vulneraiblity detail.
- 2016/12/28 02:44 GitHub response that the fix will included with next release of GitHub Enterprise.
- 2017/01/04 06:41 GitHub response that offer $5,000 USD reward.
- 2017/01/05 02:37 Asked Is there anything I should concern about if I want to post a blog?
- 2017/01/05 03:06 GitHub is very open mind and response that it's OK!
- 2017/01/05 07:06 GitHub Enterprise 2.8.5 released!
HackerOne is a platform where security researchers report bugs to companies and the reports are tracked. The same day the report was filed, GitHub confirmed the issue and started on a fix; GitHub responded that the fix would be included with the next release, and offered a 5,000 US dollar reward. The researcher then asked whether there was anything to be concerned about before writing a public blog post, and GitHub said it was fine.
- Look for other ActiveRecord
ordercalls that take a parameter name from the request (for example asortquery parameter). A positive result is a response whose ordering changes in a way that reflects injected SQL, or a database error. - Review other ActiveRecord methods that accept raw SQL fragments (such as
wherewith string interpolation) in the same code base. A positive result is a query that returns rows it should not, or an error revealing the query. - Check API endpoints that require elevated scopes such as
admin:pre_receive_hookfor similar parameter pass-through. A positive result is an injection reachable with a token of that scope.
Glossary
- API: a set of web addresses meant for programs rather than people, returning structured data.
- Access token: a long secret string used like a pass card to prove which account is making a request.
- ActiveRecord: the part of the Ruby on Rails framework that builds database queries for you.
- Code review: reading source code line by line to find mistakes.
- Database: an organised store of information that programs read from and write to.
- Decompression: expanding packed data back to its original form.
- Directory: a folder on a computer.
- Framework: ready-made scaffolding for building a web application.
- Gist: a small public snippet of code hosted by GitHub.
- haproxy: software that receives connections at the front door and forwards them to the program doing the work.
- HackerOne: a platform for reporting and tracking security bugs to companies.
- IDA Pro: a program for taking a compiled file apart and viewing its internal instructions.
- Management console: a private control panel for administering a product.
- Model: in Rails, the part of the code representing one kind of thing stored in the database.
- Nmap: a free tool that scans a machine's ports to see which are open and what answers.
- Obfuscation: deliberately making code hard to read while keeping it working.
- On-premises: software that runs on computers the customer owns, inside the customer's own network.
- ORM: object-relational mapper, a layer that turns database rows into program objects and builds SQL automatically.
- Parameter: a value handed to a piece of code, often taken from the visitor's request.
- Port: a numbered door on a computer through which a network service is reached.
- Port scan: asking a machine, door by door, which ports are open.
- Pre-receive hook: a configurable check that validates code before a push is accepted.
- Ruby: a programming language; Ruby on Rails and Sinatra are web frameworks written in it.
- Scope: the list of permissions attached to an access token.
- Source code: the human-written text a program is built from.
- SQL: the language programs use to talk to a database.
- SQL Injection: tricking a program into treating user input as SQL instructions instead of plain data.
- SSH: a way to open a text-only remote control session on another computer.
- SSH key: a pair of files, one secret and one registered with the server, that prove your identity without a password.
- Virtual machine: a whole computer packed into a single file that runs inside your own computer.
- XOR: a simple mixing operation on numbers; mixing data with a key hides it, mixing again with the same key restores it.