forked from fooinha/nginx-ssl-ja3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nginx.1.20.0.ssl.extensions.patch
175 lines (164 loc) · 5.64 KB
/
nginx.1.20.0.ssl.extensions.patch
1
2
3
4
5
6
7
8
9
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
diff -Naurp nginx-1.18.0.orig/src/event/ngx_event_openssl.c nginx-1.18.0/src/event/ngx_event_openssl.c
--- nginx-1.18.0.orig/src/event/ngx_event_openssl.c 2020-04-21 17:09:01.000000000 +0300
+++ nginx-1.18.0/src/event/ngx_event_openssl.c 2020-11-11 23:02:35.972851578 +0300
@@ -1697,6 +1697,107 @@ ngx_ssl_set_session(ngx_connection_t *c,
return NGX_OK;
}
+/* ----- JA3 HACK START -----------------------------------------------------*/
+#ifdef NGX_JA3_ENABLED
+
+void
+ngx_SSL_client_features(ngx_connection_t *c) {
+
+ unsigned short *ciphers_out = NULL;
+ int *curves_out = NULL;
+ int *point_formats_out = NULL;
+ size_t len = 0;
+ SSL *s = NULL;
+
+ if (c == NULL) {
+ return;
+ }
+ s = c->ssl->connection;
+
+ /* Cipher suites */
+ c->ssl->ciphers = NULL;
+ c->ssl->ciphers_sz = SSL_get0_raw_cipherlist(s, &ciphers_out);
+ c->ssl->ciphers_sz /= 2;
+
+ if (c->ssl->ciphers_sz && ciphers_out) {
+ len = c->ssl->ciphers_sz * sizeof(unsigned short);
+ c->ssl->ciphers = ngx_pnalloc(c->pool, len);
+ ngx_memcpy(c->ssl->ciphers, ciphers_out, len);
+ }
+
+ /* Elliptic curve points */
+ c->ssl->curves_sz = SSL_get1_curves(s, NULL);
+ if (c->ssl->curves_sz) {
+ curves_out = OPENSSL_malloc(c->ssl->curves_sz * sizeof(int));
+ if (curves_out != NULL) {
+ SSL_get1_curves(s, curves_out);
+ len = c->ssl->curves_sz * sizeof(unsigned short);
+ c->ssl->curves = ngx_pnalloc(c->pool, len);
+ if (c->ssl->curves != NULL) {
+ for (size_t i = 0; i < c->ssl->curves_sz; i++) {
+ c->ssl->curves[i] = curves_out[i];
+ }
+ }
+ OPENSSL_free(curves_out);
+ }
+ }
+
+ /* Elliptic curve point formats */
+ c->ssl->point_formats_sz = SSL_get0_ec_point_formats(s, &point_formats_out);
+ if (c->ssl->point_formats_sz && point_formats_out != NULL) {
+ len = c->ssl->point_formats_sz * sizeof(unsigned char);
+ c->ssl->point_formats = ngx_pnalloc(c->pool, len);
+ if (c->ssl->point_formats != NULL) {
+ ngx_memcpy(c->ssl->point_formats, point_formats_out, len);
+ }
+ }
+}
+
+/* should *ALWAYS return 1
+ * # define SSL_CLIENT_HELLO_SUCCESS 1
+ *
+ * otherwise
+ * A failure in the ClientHello callback terminates the connection.
+ */
+int
+ngx_SSL_early_cb_fn(SSL *s, int *al, void *arg) {
+
+ int got_extensions;
+ int *ext_out;
+ size_t ext_len;
+ ngx_connection_t *c;
+
+ c = arg;
+
+ if (c == NULL) {
+ return 1;
+ }
+
+ if (c->ssl == NULL) {
+ return 1;
+ }
+
+ c->ssl->extensions_size = 0;
+ c->ssl->extensions = NULL;
+ got_extensions = SSL_client_hello_get1_extensions_present(s,
+ &ext_out,
+ &ext_len);
+ if (got_extensions) {
+ if (ext_out && ext_len) {
+ c->ssl->extensions =
+ ngx_palloc(c->pool, sizeof(int) * ext_len);
+ if (c->ssl->extensions != NULL) {
+ c->ssl->extensions_size = ext_len;
+ ngx_memcpy(c->ssl->extensions, ext_out, sizeof(int) * ext_len);
+ OPENSSL_free(ext_out);
+ }
+ }
+ }
+
+ return 1;
+}
+#endif
+/* ----- JA3 HACK END -------------------------------------------------------*/
ngx_int_t
ngx_ssl_handshake(ngx_connection_t *c)
@@ -1717,6 +1818,10 @@ ngx_ssl_handshake(ngx_connection_t *c)
ngx_ssl_clear_error(c->log);
+#ifdef NGX_JA3_ENABLED
+ SSL_CTX_set_client_hello_cb(c->ssl->session_ctx, ngx_SSL_early_cb_fn, c);
+#endif
+
n = SSL_do_handshake(c->ssl->connection);
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_do_handshake: %d", n);
@@ -1735,6 +1840,12 @@ ngx_ssl_handshake(ngx_connection_t *c)
ngx_ssl_handshake_log(c);
#endif
+/* ----- JA3 HACK START -----------------------------------------------------*/
+#ifdef NGX_JA3_ENABLED
+ ngx_SSL_client_features(c);
+#endif
+/* ----- JA3 HACK END -------------------------------------------------------*/
+
c->recv = ngx_ssl_recv;
c->send = ngx_ssl_write;
c->recv_chain = ngx_ssl_recv_chain;
diff -Naurp nginx-1.18.0.orig/src/event/ngx_event_openssl.h nginx-1.18.0/src/event/ngx_event_openssl.h
--- nginx-1.18.0.orig/src/event/ngx_event_openssl.h 2020-04-21 17:09:01.000000000 +0300
+++ nginx-1.18.0/src/event/ngx_event_openssl.h 2020-11-11 23:04:06.108916060 +0300
@@ -63,6 +63,12 @@
#define SSL_is_server(s) (s)->server
#endif
+#if OPENSSL_VERSION_NUMBER >= 0x10101000L
+#ifdef NGX_JA3
+#define NGX_JA3_ENABLED
+#endif
+#endif
+
typedef struct ngx_ssl_ocsp_s ngx_ssl_ocsp_t;
@@ -106,6 +112,23 @@ struct ngx_ssl_connection_s {
unsigned in_ocsp:1;
unsigned early_preread:1;
unsigned write_blocked:1;
+
+/* ----- JA3 HACK START -----------------------------------------------------*/
+#ifdef NGX_JA3_ENABLED
+
+ size_t ciphers_sz;
+ unsigned short *ciphers;
+
+ size_t extensions_size;
+ int *extensions;
+
+ size_t curves_sz;
+ unsigned short *curves;
+
+ size_t point_formats_sz;
+ unsigned char *point_formats;
+#endif
+/* ----- JA3 HACK END -------------------------------------------------------*/
};