obfuscatedSshConn.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. * Copyright (c) 2015, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package obfuscator
  20. import (
  21. "bytes"
  22. "context"
  23. "encoding/binary"
  24. std_errors "errors"
  25. "io"
  26. "io/ioutil"
  27. "net"
  28. "time"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/transforms"
  33. )
  34. const (
  35. SSH_MAX_SERVER_LINE_LENGTH = 1024
  36. SSH_PACKET_PREFIX_LENGTH = 5 // uint32 + byte
  37. SSH_MAX_PACKET_LENGTH = 256 * 1024 // OpenSSH max packet length
  38. SSH_MSG_NEWKEYS = 21
  39. SSH_MAX_PADDING_LENGTH = 255 // RFC 4253 sec. 6
  40. SSH_PADDING_MULTIPLE = 16 // Default cipher block size
  41. )
  42. // ObfuscatedSSHConn wraps a Conn and applies the obfuscated SSH protocol
  43. // to the traffic on the connection:
  44. // https://github.com/brl/obfuscated-openssh/blob/master/README.obfuscation
  45. //
  46. // ObfuscatedSSHConn is used to add obfuscation to golang's stock "ssh"
  47. // client and server without modification to that standard library code.
  48. // The underlying connection must be used for SSH traffic. This code
  49. // injects the obfuscated seed message, applies obfuscated stream cipher
  50. // transformations, and performs minimal parsing of the SSH protocol to
  51. // determine when to stop obfuscation (after the first SSH_MSG_NEWKEYS is
  52. // sent and received).
  53. //
  54. // WARNING: doesn't fully conform to net.Conn concurrency semantics: there's
  55. // no synchronization of access to the read/writeBuffers, so concurrent
  56. // calls to one of Read or Write will result in undefined behavior.
  57. type ObfuscatedSSHConn struct {
  58. net.Conn
  59. mode ObfuscatedSSHConnMode
  60. runCtx context.Context
  61. stopRunning context.CancelFunc
  62. obfuscator *Obfuscator
  63. readDeobfuscate func([]byte)
  64. writeObfuscate func([]byte)
  65. readState ObfuscatedSSHReadState
  66. writeState ObfuscatedSSHWriteState
  67. readBuffer *bytes.Buffer
  68. writeBuffer *bytes.Buffer
  69. transformBuffer *bytes.Buffer
  70. legacyPadding bool
  71. paddingLength int
  72. paddingPRNG *prng.PRNG
  73. }
  74. type ObfuscatedSSHConnMode int
  75. const (
  76. OBFUSCATION_CONN_MODE_CLIENT = iota
  77. OBFUSCATION_CONN_MODE_SERVER
  78. )
  79. type ObfuscatedSSHReadState int
  80. const (
  81. OBFUSCATION_READ_STATE_CLIENT_READ_PREFIX = iota
  82. OBFUSCATION_READ_STATE_IDENTIFICATION_LINES
  83. OBFUSCATION_READ_STATE_KEX_PACKETS
  84. OBFUSCATION_READ_STATE_FLUSH
  85. OBFUSCATION_READ_STATE_FINISHED
  86. )
  87. type ObfuscatedSSHWriteState int
  88. const (
  89. OBFUSCATION_WRITE_STATE_CLIENT_SEND_PREAMBLE = iota
  90. OBFUSCATION_WRITE_STATE_SERVER_SEND_PREFIX_AND_IDENTIFICATION_LINE_PADDING
  91. OBFUSCATION_WRITE_STATE_IDENTIFICATION_LINE
  92. OBFUSCATION_WRITE_STATE_KEX_PACKETS
  93. OBFUSCATION_WRITE_STATE_FINISHED
  94. )
  95. // NewObfuscatedSSHConn creates a new ObfuscatedSSHConn.
  96. // The underlying conn must be used for SSH traffic and must have
  97. // transferred no traffic.
  98. //
  99. // In client mode, NewObfuscatedSSHConn does not block or initiate network
  100. // I/O. The obfuscation seed message is sent when Write() is first called.
  101. //
  102. // In server mode, NewObfuscatedSSHConn cannot completely initialize itself
  103. // without the seed message from the client to derive obfuscation keys. So
  104. // NewObfuscatedSSHConn blocks on reading the client seed message from the
  105. // underlying conn.
  106. //
  107. // obfuscationPaddingPRNGSeed is required and used only in
  108. // OBFUSCATION_CONN_MODE_CLIENT mode and allows for optional replay of the
  109. // same padding: both in the initial obfuscator message and in the SSH KEX
  110. // sequence. In OBFUSCATION_CONN_MODE_SERVER mode, the server obtains its PRNG
  111. // seed from the client's initial obfuscator message, resulting in the server
  112. // replaying its padding as well.
  113. //
  114. // seedHistory and irregularLogger are optional ObfuscatorConfig parameters
  115. // used only in OBFUSCATION_CONN_MODE_SERVER.
  116. func NewObfuscatedSSHConn(
  117. mode ObfuscatedSSHConnMode,
  118. conn net.Conn,
  119. obfuscationKeyword string,
  120. obfuscationPaddingPRNGSeed *prng.Seed,
  121. obfuscatorSeedTransformerParameters *transforms.ObfuscatorSeedTransformerParameters,
  122. clientPrefixSpec *OSSHPrefixSpec,
  123. serverPrefixSepcs transforms.Specs,
  124. osshPrefixSplitConfig *OSSHPrefixSplitConfig,
  125. minPadding, maxPadding *int,
  126. seedHistory *SeedHistory,
  127. irregularLogger func(
  128. clientIP string,
  129. err error,
  130. logFields common.LogFields)) (*ObfuscatedSSHConn, error) {
  131. var err error
  132. var obfuscator *Obfuscator
  133. var readDeobfuscate, writeObfuscate func([]byte)
  134. var writeState ObfuscatedSSHWriteState
  135. conn = WrapConnWithSkipReader(conn)
  136. readState := ObfuscatedSSHReadState(OBFUSCATION_READ_STATE_IDENTIFICATION_LINES)
  137. if mode == OBFUSCATION_CONN_MODE_CLIENT {
  138. obfuscator, err = NewClientObfuscator(
  139. &ObfuscatorConfig{
  140. IsOSSH: true,
  141. Keyword: obfuscationKeyword,
  142. ClientPrefixSpec: clientPrefixSpec,
  143. OSSHPrefixSplitConfig: osshPrefixSplitConfig,
  144. PaddingPRNGSeed: obfuscationPaddingPRNGSeed,
  145. MinPadding: minPadding,
  146. MaxPadding: maxPadding,
  147. ObfuscatorSeedTransformerParameters: obfuscatorSeedTransformerParameters,
  148. })
  149. if err != nil {
  150. return nil, errors.Trace(err)
  151. }
  152. readDeobfuscate = obfuscator.ObfuscateServerToClient
  153. writeObfuscate = obfuscator.ObfuscateClientToServer
  154. writeState = OBFUSCATION_WRITE_STATE_CLIENT_SEND_PREAMBLE
  155. if obfuscator.osshPrefixHeader != nil {
  156. // Client expects prefix with terminator from the server.
  157. readState = OBFUSCATION_READ_STATE_CLIENT_READ_PREFIX
  158. }
  159. } else {
  160. // NewServerObfuscator reads a seed message from conn
  161. obfuscator, err = NewServerObfuscator(
  162. &ObfuscatorConfig{
  163. Keyword: obfuscationKeyword,
  164. ServerPrefixSpecs: serverPrefixSepcs,
  165. SeedHistory: seedHistory,
  166. IrregularLogger: irregularLogger,
  167. },
  168. common.IPAddressFromAddr(conn.RemoteAddr()),
  169. conn)
  170. if err != nil {
  171. // Obfuscated SSH protocol spec:
  172. // "If these checks fail the server will continue reading and discarding all data
  173. // until the client closes the connection without sending anything in response."
  174. //
  175. // This may be terminated by a server-side connection establishment timeout.
  176. io.Copy(ioutil.Discard, conn)
  177. return nil, errors.Trace(err)
  178. }
  179. readDeobfuscate = obfuscator.ObfuscateClientToServer
  180. writeObfuscate = obfuscator.ObfuscateServerToClient
  181. writeState = OBFUSCATION_WRITE_STATE_SERVER_SEND_PREFIX_AND_IDENTIFICATION_LINE_PADDING
  182. }
  183. paddingPRNG, err := obfuscator.GetDerivedPRNG("obfuscated-ssh-padding")
  184. if err != nil {
  185. return nil, errors.Trace(err)
  186. }
  187. runCtx, stopRunning := context.WithCancel(context.Background())
  188. return &ObfuscatedSSHConn{
  189. Conn: conn,
  190. mode: mode,
  191. runCtx: runCtx,
  192. stopRunning: stopRunning,
  193. obfuscator: obfuscator,
  194. readDeobfuscate: readDeobfuscate,
  195. writeObfuscate: writeObfuscate,
  196. readState: readState,
  197. writeState: writeState,
  198. readBuffer: new(bytes.Buffer),
  199. writeBuffer: new(bytes.Buffer),
  200. transformBuffer: new(bytes.Buffer),
  201. paddingLength: -1,
  202. paddingPRNG: paddingPRNG,
  203. }, nil
  204. }
  205. // NewClientObfuscatedSSHConn creates a client ObfuscatedSSHConn. See
  206. // documentation in NewObfuscatedSSHConn.
  207. func NewClientObfuscatedSSHConn(
  208. conn net.Conn,
  209. obfuscationKeyword string,
  210. obfuscationPaddingPRNGSeed *prng.Seed,
  211. obfuscatorSeedTransformerParameters *transforms.ObfuscatorSeedTransformerParameters,
  212. prefixSpec *OSSHPrefixSpec,
  213. osshPrefixSplitConfig *OSSHPrefixSplitConfig,
  214. minPadding, maxPadding *int) (*ObfuscatedSSHConn, error) {
  215. return NewObfuscatedSSHConn(
  216. OBFUSCATION_CONN_MODE_CLIENT,
  217. conn,
  218. obfuscationKeyword,
  219. obfuscationPaddingPRNGSeed,
  220. obfuscatorSeedTransformerParameters,
  221. prefixSpec,
  222. nil,
  223. osshPrefixSplitConfig,
  224. minPadding, maxPadding,
  225. nil,
  226. nil)
  227. }
  228. // NewServerObfuscatedSSHConn creates a server ObfuscatedSSHConn. See
  229. // documentation in NewObfuscatedSSHConn.
  230. func NewServerObfuscatedSSHConn(
  231. conn net.Conn,
  232. obfuscationKeyword string,
  233. seedHistory *SeedHistory,
  234. serverPrefixSpecs transforms.Specs,
  235. irregularLogger func(
  236. clientIP string,
  237. err error,
  238. logFields common.LogFields)) (*ObfuscatedSSHConn, error) {
  239. return NewObfuscatedSSHConn(
  240. OBFUSCATION_CONN_MODE_SERVER,
  241. conn,
  242. obfuscationKeyword,
  243. nil, nil,
  244. nil,
  245. serverPrefixSpecs,
  246. nil,
  247. nil, nil,
  248. seedHistory,
  249. irregularLogger)
  250. }
  251. // IsOSSHPrefixedStream returns true if client wrote a prefix to the Obfuscated SSH stream,
  252. // or the server read a prefixed Obfuscated SSH stream.
  253. func (conn *ObfuscatedSSHConn) IsOSSHPrefixStream() bool {
  254. return conn.obfuscator.osshPrefixHeader != nil
  255. }
  256. // GetDerivedPRNG creates a new PRNG with a seed derived from the
  257. // ObfuscatedSSHConn padding seed and distinguished by the salt, which should
  258. // be a unique identifier for each usage context.
  259. //
  260. // In OBFUSCATION_CONN_MODE_SERVER mode, the ObfuscatedSSHConn padding seed is
  261. // obtained from the client, so derived PRNGs may be used to replay sequences
  262. // post-initial obfuscator message.
  263. func (conn *ObfuscatedSSHConn) GetDerivedPRNG(salt string) (*prng.PRNG, error) {
  264. return conn.obfuscator.GetDerivedPRNG(salt)
  265. }
  266. // SetOSSHPrefixSplitConfig sets the OSSHPrefixSplitConfig for the server.
  267. // This must be called before any data is written.
  268. func (conn *ObfuscatedSSHConn) SetOSSHPrefixSplitConfig(minDelay, maxDelay time.Duration) error {
  269. if conn.mode != OBFUSCATION_CONN_MODE_SERVER {
  270. return errors.TraceNew("SetOSSHPrefixSplitConfig() is only valid for server connections")
  271. }
  272. if conn.writeState != OBFUSCATION_WRITE_STATE_SERVER_SEND_PREFIX_AND_IDENTIFICATION_LINE_PADDING {
  273. return errors.TraceNew("SetOSSHPrefixSplitConfig() must be called before any data is written")
  274. }
  275. seed, err := conn.obfuscator.GetDerivedPRNGSeed("obfuscated-ssh-prefix-split")
  276. if err != nil {
  277. return errors.Trace(err)
  278. }
  279. conn.obfuscator.osshPrefixSplitConfig = &OSSHPrefixSplitConfig{
  280. Seed: seed,
  281. MinDelay: minDelay,
  282. MaxDelay: maxDelay,
  283. }
  284. return nil
  285. }
  286. // GetMetrics implements the common.MetricsSource interface.
  287. func (conn *ObfuscatedSSHConn) GetMetrics() common.LogFields {
  288. logFields := make(common.LogFields)
  289. if conn.mode == OBFUSCATION_CONN_MODE_CLIENT {
  290. paddingLength := conn.obfuscator.GetPaddingLength()
  291. if paddingLength != -1 {
  292. logFields["upstream_ossh_padding"] = paddingLength
  293. }
  294. } else {
  295. if conn.paddingLength != -1 {
  296. logFields["downstream_ossh_padding"] = conn.paddingLength
  297. }
  298. }
  299. return logFields
  300. }
  301. // Read wraps standard Read, transparently applying the obfuscation
  302. // transformations.
  303. func (conn *ObfuscatedSSHConn) Read(buffer []byte) (int, error) {
  304. if conn.readState == OBFUSCATION_READ_STATE_FINISHED {
  305. return conn.Conn.Read(buffer)
  306. }
  307. n, err := conn.readAndTransform(buffer)
  308. if err != nil {
  309. err = errors.Trace(err)
  310. }
  311. return n, err
  312. }
  313. // Write wraps standard Write, transparently applying the obfuscation
  314. // transformations.
  315. func (conn *ObfuscatedSSHConn) Write(buffer []byte) (int, error) {
  316. if conn.writeState == OBFUSCATION_WRITE_STATE_FINISHED {
  317. return conn.Conn.Write(buffer)
  318. }
  319. err := conn.transformAndWrite(buffer)
  320. if err != nil {
  321. return 0, errors.Trace(err)
  322. }
  323. // Reports that we wrote all the bytes
  324. // (although we may have buffered some or all)
  325. return len(buffer), nil
  326. }
  327. func (conn *ObfuscatedSSHConn) Close() error {
  328. conn.stopRunning()
  329. return conn.Conn.Close()
  330. }
  331. // readAndTransform reads and transforms the downstream bytes stream
  332. // while in an obfucation state. It parses the stream of bytes read
  333. // looking for the first SSH_MSG_NEWKEYS packet sent from the peer,
  334. // after which obfuscation is turned off. Since readAndTransform may
  335. // read in more bytes that the higher-level conn.Read() can consume,
  336. // read bytes are buffered and may be returned in subsequent calls.
  337. //
  338. // readAndTransform also implements a workaround for issues with
  339. // ssh/transport.go exchangeVersions/readVersion and Psiphon's openssh
  340. // server.
  341. //
  342. // Psiphon's server sends extra lines before the version line, as
  343. // permitted by http://www.ietf.org/rfc/rfc4253.txt sec 4.2:
  344. //
  345. // The server MAY send other lines of data before sending the
  346. // version string. [...] Clients MUST be able to process such lines.
  347. //
  348. // A comment in exchangeVersions explains that the golang code doesn't
  349. // support this:
  350. //
  351. // Contrary to the RFC, we do not ignore lines that don't
  352. // start with "SSH-2.0-" to make the library usable with
  353. // nonconforming servers.
  354. //
  355. // In addition, Psiphon's server sends up to 512 characters per extra
  356. // line. It's not clear that the 255 max string size in sec 4.2 refers
  357. // to the extra lines as well, but in any case golang's code only
  358. // supports 255 character lines.
  359. //
  360. // State OBFUSCATION_READ_STATE_CLIENT_READ_PREFIX: the initial
  361. // state, when the client expects prefix with terminator before the
  362. // rest of the tunnel. In this state, the prefix is read and discarded.
  363. //
  364. // State OBFUSCATION_READ_STATE_IDENTIFICATION_LINES: in this
  365. // state, extra lines are read and discarded. Once the peer's
  366. // identification string line is read, it is buffered and returned
  367. // as per the requested read buffer size.
  368. //
  369. // State OBFUSCATION_READ_STATE_KEX_PACKETS: reads, deobfuscates,
  370. // and buffers full SSH packets, checking for SSH_MSG_NEWKEYS. Packet
  371. // data is returned as per the requested read buffer size.
  372. //
  373. // State OBFUSCATION_READ_STATE_FLUSH: after SSH_MSG_NEWKEYS, no more
  374. // packets are read by this function, but bytes from the SSH_MSG_NEWKEYS
  375. // packet may need to be buffered due to partial reading.
  376. func (conn *ObfuscatedSSHConn) readAndTransform(buffer []byte) (int, error) {
  377. if conn.readState == OBFUSCATION_READ_STATE_CLIENT_READ_PREFIX {
  378. skipReader, ok := conn.Conn.(*SkipReader)
  379. if !ok {
  380. return 0, errors.TraceNew("expected SkipReader")
  381. }
  382. preambleHeader := make([]byte, PREAMBLE_HEADER_LENGTH)
  383. _, err := io.ReadFull(skipReader, preambleHeader)
  384. if err != nil {
  385. return 0, errors.Trace(err)
  386. }
  387. terminator, err := makeTerminator(conn.obfuscator.keyword,
  388. preambleHeader, OBFUSCATE_SERVER_TO_CLIENT_IV)
  389. if err != nil {
  390. return 0, errors.Trace(err)
  391. }
  392. err = skipReader.SkipUpToToken(terminator, PREFIX_TERM_SEARCH_BUF_SIZE, PREFIX_MAX_LENGTH)
  393. if err != nil {
  394. return 0, errors.Trace(err)
  395. }
  396. conn.readState = OBFUSCATION_READ_STATE_IDENTIFICATION_LINES
  397. }
  398. nextState := conn.readState
  399. switch conn.readState {
  400. case OBFUSCATION_READ_STATE_IDENTIFICATION_LINES:
  401. // TODO: only client should accept multiple lines?
  402. if conn.readBuffer.Len() == 0 {
  403. for {
  404. err := readSSHIdentificationLine(
  405. conn.Conn, conn.readDeobfuscate, conn.readBuffer)
  406. if err != nil {
  407. return 0, errors.Trace(err)
  408. }
  409. if bytes.HasPrefix(conn.readBuffer.Bytes(), []byte("SSH-")) {
  410. if bytes.Contains(conn.readBuffer.Bytes(), []byte("Ganymed")) {
  411. conn.legacyPadding = true
  412. }
  413. break
  414. }
  415. // Discard extra line
  416. conn.readBuffer.Truncate(0)
  417. }
  418. }
  419. nextState = OBFUSCATION_READ_STATE_KEX_PACKETS
  420. case OBFUSCATION_READ_STATE_KEX_PACKETS:
  421. if conn.readBuffer.Len() == 0 {
  422. isMsgNewKeys, err := readSSHPacket(
  423. conn.Conn, conn.readDeobfuscate, conn.readBuffer)
  424. if err != nil {
  425. return 0, errors.Trace(err)
  426. }
  427. if isMsgNewKeys {
  428. nextState = OBFUSCATION_READ_STATE_FLUSH
  429. }
  430. }
  431. case OBFUSCATION_READ_STATE_FLUSH:
  432. nextState = OBFUSCATION_READ_STATE_FINISHED
  433. case OBFUSCATION_READ_STATE_FINISHED:
  434. return 0, errors.TraceNew("invalid read state")
  435. }
  436. n, err := conn.readBuffer.Read(buffer)
  437. if err == io.EOF {
  438. err = nil
  439. }
  440. if err != nil {
  441. return n, errors.Trace(err)
  442. }
  443. if conn.readBuffer.Len() == 0 {
  444. conn.readState = nextState
  445. if conn.readState == OBFUSCATION_READ_STATE_FINISHED {
  446. // The buffer memory is no longer used
  447. conn.readBuffer = nil
  448. }
  449. }
  450. return n, nil
  451. }
  452. // transformAndWrite transforms the upstream bytes stream while in an
  453. // obfucation state, buffers bytes as necessary for parsing, and writes
  454. // transformed bytes to the network connection. Bytes are obfuscated until
  455. // after the first SSH_MSG_NEWKEYS packet is sent.
  456. //
  457. // There are two mode-specific states:
  458. //
  459. // State OBFUSCATION_WRITE_STATE_CLIENT_SEND_SEED_MESSAGE: the initial
  460. // state, when the client has not sent any data. In this state, the seed message
  461. // is injected into the client output stream.
  462. //
  463. // State OBFUSCATION_WRITE_STATE_SERVER_SEND_PREFIX_AND_IDENTIFICATION_LINE_PADDING: the
  464. // initial state, when the server has not sent any data. In this state, the
  465. // additional lines of padding are injected into the server output stream.
  466. // This padding is a partial defense against traffic analysis against the
  467. // otherwise-fixed size server version line. This makes use of the
  468. // "other lines of data" allowance, before the version line, which clients
  469. // will ignore (http://tools.ietf.org/html/rfc4253#section-4.2).
  470. //
  471. // State OBFUSCATION_WRITE_STATE_IDENTIFICATION_LINE: before
  472. // packets are sent, the SSH peer sends an identification line terminated by CRLF:
  473. // http://www.ietf.org/rfc/rfc4253.txt sec 4.2.
  474. // In this state, the CRLF terminator is used to parse message boundaries.
  475. //
  476. // State OBFUSCATION_WRITE_STATE_KEX_PACKETS: follows the binary
  477. // packet protocol, parsing each packet until the first SSH_MSG_NEWKEYS.
  478. // http://www.ietf.org/rfc/rfc4253.txt sec 6:
  479. //
  480. // uint32 packet_length
  481. // byte padding_length
  482. // byte[n1] payload; n1 = packet_length - padding_length - 1
  483. // byte[n2] random padding; n2 = padding_length
  484. // byte[m] mac (Message Authentication Code - MAC); m = mac_length
  485. //
  486. // m is 0 as no MAC ha yet been negotiated.
  487. // http://www.ietf.org/rfc/rfc4253.txt sec 7.3, 12:
  488. // The payload for SSH_MSG_NEWKEYS is one byte, the packet type, value 21.
  489. //
  490. // SSH packet padding values are transformed to achieve random, variable length
  491. // padding during the KEX phase as a partial defense against traffic analysis.
  492. // (The transformer can do this since only the payload and not the padding of
  493. // these packets is authenticated in the "exchange hash").
  494. func (conn *ObfuscatedSSHConn) transformAndWrite(buffer []byte) error {
  495. // The preamble (client) and requested prefix with
  496. // identification line padding (server) are injected before any standard SSH traffic.
  497. if conn.writeState == OBFUSCATION_WRITE_STATE_CLIENT_SEND_PREAMBLE {
  498. preamble, prefixLen := conn.obfuscator.SendPreamble()
  499. if prefixLen > 0 {
  500. // Writes the prefix first, then the rest of the preamble after a delay.
  501. _, err := conn.Conn.Write(preamble[:prefixLen])
  502. if err != nil {
  503. return errors.Trace(err)
  504. }
  505. // Adds random delay defined by OSSH prefix split config.
  506. if config := conn.obfuscator.osshPrefixSplitConfig; config != nil {
  507. rng := prng.NewPRNGWithSeed(config.Seed)
  508. delay := rng.Period(config.MinDelay, config.MaxDelay)
  509. timer := time.NewTimer(delay)
  510. var err error
  511. select {
  512. case <-conn.runCtx.Done():
  513. err = conn.runCtx.Err()
  514. case <-timer.C:
  515. }
  516. timer.Stop()
  517. if err != nil {
  518. return errors.Trace(err)
  519. }
  520. }
  521. }
  522. _, err := conn.Conn.Write(preamble[prefixLen:])
  523. if err != nil {
  524. return errors.Trace(err)
  525. }
  526. conn.writeState = OBFUSCATION_WRITE_STATE_IDENTIFICATION_LINE
  527. } else if conn.writeState == OBFUSCATION_WRITE_STATE_SERVER_SEND_PREFIX_AND_IDENTIFICATION_LINE_PADDING {
  528. var buffer bytes.Buffer
  529. if preamble, prefixLen := conn.obfuscator.SendPreamble(); preamble != nil {
  530. // Prefix bytes are written to the underlying conn immediately, skipping the buffer.
  531. _, err := conn.Conn.Write(preamble[:prefixLen])
  532. if err != nil {
  533. return errors.Trace(err)
  534. }
  535. // Adds random delay defined by OSSH prefix split config.
  536. if config := conn.obfuscator.osshPrefixSplitConfig; config != nil {
  537. rng := prng.NewPRNGWithSeed(config.Seed)
  538. delay := rng.Period(config.MinDelay, config.MaxDelay)
  539. timer := time.NewTimer(delay)
  540. var err error
  541. select {
  542. case <-conn.runCtx.Done():
  543. err = conn.runCtx.Err()
  544. case <-timer.C:
  545. }
  546. timer.Stop()
  547. if err != nil {
  548. return errors.Trace(err)
  549. }
  550. }
  551. _, err = buffer.Write(preamble[prefixLen:])
  552. if err != nil {
  553. return errors.Trace(err)
  554. }
  555. }
  556. padding := makeServerIdentificationLinePadding(conn.paddingPRNG)
  557. conn.paddingLength = len(padding)
  558. conn.writeObfuscate(padding)
  559. _, err := buffer.Write(padding)
  560. if err != nil {
  561. return errors.Trace(err)
  562. }
  563. _, err = conn.Conn.Write(buffer.Bytes())
  564. if err != nil {
  565. return errors.Trace(err)
  566. }
  567. conn.writeState = OBFUSCATION_WRITE_STATE_IDENTIFICATION_LINE
  568. }
  569. // writeBuffer is used to buffer bytes received from Write() until a
  570. // complete SSH message is received. transformBuffer is used as a scratch
  571. // buffer for size-changing tranformations, including padding transforms.
  572. // All data flows as follows:
  573. // conn.Write() -> writeBuffer -> transformBuffer -> conn.Conn.Write()
  574. conn.writeBuffer.Write(buffer)
  575. switch conn.writeState {
  576. case OBFUSCATION_WRITE_STATE_IDENTIFICATION_LINE:
  577. hasIdentificationLine := extractSSHIdentificationLine(
  578. conn.writeBuffer, conn.transformBuffer)
  579. if hasIdentificationLine {
  580. conn.writeState = OBFUSCATION_WRITE_STATE_KEX_PACKETS
  581. }
  582. case OBFUSCATION_WRITE_STATE_KEX_PACKETS:
  583. hasMsgNewKeys, err := extractSSHPackets(
  584. conn.paddingPRNG,
  585. conn.legacyPadding,
  586. conn.writeBuffer,
  587. conn.transformBuffer)
  588. if err != nil {
  589. return errors.Trace(err)
  590. }
  591. if hasMsgNewKeys {
  592. conn.writeState = OBFUSCATION_WRITE_STATE_FINISHED
  593. }
  594. case OBFUSCATION_WRITE_STATE_FINISHED:
  595. return errors.TraceNew("invalid write state")
  596. }
  597. if conn.transformBuffer.Len() > 0 {
  598. sendData := conn.transformBuffer.Next(conn.transformBuffer.Len())
  599. conn.writeObfuscate(sendData)
  600. _, err := conn.Conn.Write(sendData)
  601. if err != nil {
  602. return errors.Trace(err)
  603. }
  604. }
  605. if conn.writeState == OBFUSCATION_WRITE_STATE_FINISHED {
  606. if conn.writeBuffer.Len() > 0 {
  607. // After SSH_MSG_NEWKEYS, any remaining bytes are un-obfuscated
  608. _, err := conn.Conn.Write(conn.writeBuffer.Bytes())
  609. if err != nil {
  610. return errors.Trace(err)
  611. }
  612. }
  613. // The buffer memory is no longer used
  614. conn.writeBuffer = nil
  615. conn.transformBuffer = nil
  616. }
  617. return nil
  618. }
  619. func readSSHIdentificationLine(
  620. conn net.Conn,
  621. deobfuscate func([]byte),
  622. readBuffer *bytes.Buffer) error {
  623. // TODO: less redundant string searching?
  624. var oneByte [1]byte
  625. var validLine = false
  626. readBuffer.Grow(SSH_MAX_SERVER_LINE_LENGTH)
  627. for i := 0; i < SSH_MAX_SERVER_LINE_LENGTH; i++ {
  628. _, err := io.ReadFull(conn, oneByte[:])
  629. if err != nil {
  630. return errors.Trace(err)
  631. }
  632. deobfuscate(oneByte[:])
  633. readBuffer.WriteByte(oneByte[0])
  634. if bytes.HasSuffix(readBuffer.Bytes(), []byte("\r\n")) {
  635. validLine = true
  636. break
  637. }
  638. }
  639. if !validLine {
  640. return errors.TraceNew("invalid identification line")
  641. }
  642. return nil
  643. }
  644. func readSSHPacket(
  645. conn net.Conn,
  646. deobfuscate func([]byte),
  647. readBuffer *bytes.Buffer) (bool, error) {
  648. prefixOffset := readBuffer.Len()
  649. readBuffer.Grow(SSH_PACKET_PREFIX_LENGTH)
  650. n, err := readBuffer.ReadFrom(io.LimitReader(conn, SSH_PACKET_PREFIX_LENGTH))
  651. if err == nil && n != SSH_PACKET_PREFIX_LENGTH {
  652. err = std_errors.New("unxpected number of bytes read")
  653. }
  654. if err != nil {
  655. return false, errors.Trace(err)
  656. }
  657. prefix := readBuffer.Bytes()[prefixOffset : prefixOffset+SSH_PACKET_PREFIX_LENGTH]
  658. deobfuscate(prefix)
  659. _, _, payloadLength, messageLength, err := getSSHPacketPrefix(prefix)
  660. if err != nil {
  661. return false, errors.Trace(err)
  662. }
  663. remainingReadLength := messageLength - SSH_PACKET_PREFIX_LENGTH
  664. readBuffer.Grow(remainingReadLength)
  665. n, err = readBuffer.ReadFrom(io.LimitReader(conn, int64(remainingReadLength)))
  666. if err == nil && n != int64(remainingReadLength) {
  667. err = std_errors.New("unxpected number of bytes read")
  668. }
  669. if err != nil {
  670. return false, errors.Trace(err)
  671. }
  672. remainingBytes := readBuffer.Bytes()[prefixOffset+SSH_PACKET_PREFIX_LENGTH:]
  673. deobfuscate(remainingBytes)
  674. isMsgNewKeys := false
  675. if payloadLength > 0 {
  676. packetType := int(readBuffer.Bytes()[prefixOffset+SSH_PACKET_PREFIX_LENGTH])
  677. if packetType == SSH_MSG_NEWKEYS {
  678. isMsgNewKeys = true
  679. }
  680. }
  681. return isMsgNewKeys, nil
  682. }
  683. // From the original patch to sshd.c:
  684. // https://bitbucket.org/psiphon/psiphon-circumvention-system/commits/f40865ce624b680be840dc2432283c8137bd896d
  685. func makeServerIdentificationLinePadding(prng *prng.PRNG) []byte {
  686. paddingLength := prng.Intn(OBFUSCATE_MAX_PADDING - 2 + 1) // 2 = CRLF
  687. paddingLength += 2
  688. padding := make([]byte, paddingLength)
  689. // For backwards compatibility with some clients, send no more than 512 characters
  690. // per line (including CRLF). To keep the padding distribution between 0 and OBFUSCATE_MAX_PADDING
  691. // characters, we send lines that add up to padding_length characters including all CRLFs.
  692. minLineLength := 2
  693. maxLineLength := 512
  694. lineStartIndex := 0
  695. for paddingLength > 0 {
  696. lineLength := paddingLength
  697. if lineLength > maxLineLength {
  698. lineLength = maxLineLength
  699. }
  700. // Leave enough padding allowance to send a full CRLF on the last line
  701. if paddingLength-lineLength > 0 &&
  702. paddingLength-lineLength < minLineLength {
  703. lineLength -= minLineLength - (paddingLength - lineLength)
  704. }
  705. padding[lineStartIndex+lineLength-2] = '\r'
  706. padding[lineStartIndex+lineLength-1] = '\n'
  707. lineStartIndex += lineLength
  708. paddingLength -= lineLength
  709. }
  710. return padding
  711. }
  712. func extractSSHIdentificationLine(writeBuffer, transformBuffer *bytes.Buffer) bool {
  713. index := bytes.Index(writeBuffer.Bytes(), []byte("\r\n"))
  714. if index != -1 {
  715. lineLength := index + 2 // + 2 for \r\n
  716. transformBuffer.Write(writeBuffer.Next(lineLength))
  717. return true
  718. }
  719. return false
  720. }
  721. func extractSSHPackets(
  722. prng *prng.PRNG,
  723. legacyPadding bool,
  724. writeBuffer, transformBuffer *bytes.Buffer) (bool, error) {
  725. hasMsgNewKeys := false
  726. for writeBuffer.Len() >= SSH_PACKET_PREFIX_LENGTH {
  727. packetLength, paddingLength, payloadLength, messageLength, err := getSSHPacketPrefix(
  728. writeBuffer.Bytes()[:SSH_PACKET_PREFIX_LENGTH])
  729. if err != nil {
  730. return false, errors.Trace(err)
  731. }
  732. if writeBuffer.Len() < messageLength {
  733. // We don't have the complete packet yet
  734. break
  735. }
  736. packet := writeBuffer.Next(messageLength)
  737. if payloadLength > 0 {
  738. packetType := int(packet[SSH_PACKET_PREFIX_LENGTH])
  739. if packetType == SSH_MSG_NEWKEYS {
  740. hasMsgNewKeys = true
  741. }
  742. }
  743. transformedPacketOffset := transformBuffer.Len()
  744. transformBuffer.Write(packet)
  745. transformedPacket := transformBuffer.Bytes()[transformedPacketOffset:]
  746. // Padding transformation
  747. extraPaddingLength := 0
  748. if !legacyPadding {
  749. // This does not satisfy RFC 4253 sec. 6 constraints:
  750. // - The goal is to vary packet sizes as much as possible.
  751. // - We implement both the client and server sides and both sides accept
  752. // less constrained paddings (for plaintext packets).
  753. possibleExtraPaddingLength := (SSH_MAX_PADDING_LENGTH - paddingLength)
  754. if possibleExtraPaddingLength > 0 {
  755. // extraPaddingLength is integer in range [0, possiblePadding + 1)
  756. extraPaddingLength = prng.Intn(possibleExtraPaddingLength + 1)
  757. }
  758. } else {
  759. // See RFC 4253 sec. 6 for constraints
  760. possiblePaddings := (SSH_MAX_PADDING_LENGTH - paddingLength) / SSH_PADDING_MULTIPLE
  761. if possiblePaddings > 0 {
  762. // selectedPadding is integer in range [0, possiblePaddings)
  763. selectedPadding := prng.Intn(possiblePaddings)
  764. extraPaddingLength = selectedPadding * SSH_PADDING_MULTIPLE
  765. }
  766. }
  767. extraPadding := prng.Bytes(extraPaddingLength)
  768. setSSHPacketPrefix(
  769. transformedPacket,
  770. packetLength+extraPaddingLength,
  771. paddingLength+extraPaddingLength)
  772. transformBuffer.Write(extraPadding)
  773. }
  774. return hasMsgNewKeys, nil
  775. }
  776. func getSSHPacketPrefix(buffer []byte) (int, int, int, int, error) {
  777. packetLength := int(binary.BigEndian.Uint32(buffer[0 : SSH_PACKET_PREFIX_LENGTH-1]))
  778. if packetLength < 1 || packetLength > SSH_MAX_PACKET_LENGTH {
  779. return 0, 0, 0, 0, errors.TraceNew("invalid SSH packet length")
  780. }
  781. paddingLength := int(buffer[SSH_PACKET_PREFIX_LENGTH-1])
  782. payloadLength := packetLength - paddingLength - 1
  783. messageLength := SSH_PACKET_PREFIX_LENGTH + packetLength - 1
  784. return packetLength, paddingLength, payloadLength, messageLength, nil
  785. }
  786. func setSSHPacketPrefix(buffer []byte, packetLength, paddingLength int) {
  787. binary.BigEndian.PutUint32(buffer, uint32(packetLength))
  788. buffer[SSH_PACKET_PREFIX_LENGTH-1] = byte(paddingLength)
  789. }