Module: Msf::Exploit::Remote::HTTP::Gitlab::Form::AccessTokens

Included in:
AccessTokens
Defined in:
lib/msf/core/exploit/remote/http/gitlab/form/access_tokens.rb

Overview

Create a Gitlab Access Token via form

Instance Method Summary collapse

Instance Method Details

#gitlab_create_personal_access_token ⇒ String?

Create a GitLab access token, trying the current and legacy forms in order.

Returns:

  • (String, nil) —

    GitLab personal access token if created, nil if the successful response omits it

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/msf/core/exploit/remote/http/gitlab/form/access_tokens.rb', line 10

def gitlab_create_personal_access_token
  name = Rex::Text.rand_text_alphanumeric(8)
  current_fields = { 'name' => name, 'expires_at' => '', 'scopes[]' => 'api' }
  legacy_fields = {
    'personal_access_token[name]' => name,
    'personal_access_token[expires_at]' => '',
    'personal_access_token[scopes][]' => 'api',
    'commit' => 'Create personal access token'
  }
  # The UI route moved in GitLab 16.7; GitLab 19.2 later switched the controller
  # to flat parameters. Intermediate releases require nested parameters at the
  # new URL, so falling straight back to the old URL would miss those releases.
  # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/139306
  # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/241749
  forms = [
    [
      '/-/user_settings/personal_access_tokens',
      [current_fields, legacy_fields]
    ],
    [
      '/-/profile/personal_access_tokens',
      [legacy_fields]
    ]
  ]
  unavailable_statuses = [404, 405, 410]
  failures = []

  forms.each do |path, field_formats|
    begin
      csrf_token = gitlab_helper_extract_csrf_token(path: path, regex: /name="csrf-token" content="(.*)"/)
    rescue Msf::Exploit::Remote::HTTP::Gitlab::Error::CsrfError => e
      # A missing form or CSRF meta tag can indicate an older UI. Authentication,
      # rate-limit and server errors do not establish that the route is absent.
      raise unless e.http_status.nil? || unavailable_statuses.include?(e.http_status)

      failures << "#{path}: #{e.message}"
      next
    end

    field_formats.each_with_index do |vars_post, format_index|
      res = send_request_cgi({
        'method' => 'POST',
        'uri' => normalize_uri(target_uri.path, path),
        'keep_cookies' => true,
        'vars_post' => vars_post,
        'headers' => { 'X-CSRF-Token' => csrf_token }
      })

      # A missing response cannot establish whether the token was created. Stop
      # on a transport timeout instead of possibly creating another token.
      unless res
        raise Msf::Exploit::Remote::HTTP::Gitlab::Error::RequestTimeoutError.new(
          message: "GitLab did not respond while creating a personal access token through #{path}"
        )
      end

      if res.code == 200
        json = res.get_json_document
        # Preserve the existing nil contract, but do not submit another POST:
        # a successful response may mean a token exists even if we cannot read it.
        return json.is_a?(Hash) ? json['token'].presence || json['new_token'].presence : nil
      end

      failure = "#{path}: HTTP #{res.code}"
      # Older controllers require params[:personal_access_token]; Rails rejects
      # the flat request with HTTP 400 before the token-creation service runs.
      if res.code == 400 && format_index < field_formats.length - 1
        failures << failure
        next
      end

      # These statuses indicate an unavailable POST route. Skip any remaining
      # formats for this URL and try the legacy route. Do not retry arbitrary
      # failures (especially 5xx), which can occur after a token was persisted.
      if unavailable_statuses.include?(res.code)
        failures << failure
        break
      end

      raise Msf::Exploit::Remote::HTTP::Gitlab::Error::ClientError.new(message: "Failed to create a personal access token: #{failure}")
    end
  end

  raise Msf::Exploit::Remote::HTTP::Gitlab::Error::ClientError.new(message: "Failed to create a personal access token: #{failures.join('; ')}")
end