server: implement REP_LOGIN key exchange

This commit is contained in:
CPunch 2023-03-07 01:20:36 -06:00
parent 57e681742e
commit 8f3f31d354
4 changed files with 78 additions and 31 deletions

View File

@ -1,7 +1,11 @@
package protocol package protocol
import (
"encoding/binary"
)
const ( const (
E_KEY = "m@rQn~W#" DEFAULT_KEY = "m@rQn~W#"
KEY_LENGTH = 8 KEY_LENGTH = 8
) )
@ -43,3 +47,14 @@ func DecryptData(buff, key []byte) {
size := encrypt_byte_change_A(ERSize, buff) size := encrypt_byte_change_A(ERSize, buff)
xorData(buff, key, size) xorData(buff, key, size)
} }
func CreateNewKey(uTime uint64, iv1, iv2 uint64) []byte {
num := iv1 + 1
num2 := iv2 + 1
dEKey := uint64(binary.LittleEndian.Uint64([]byte(DEFAULT_KEY)))
key := dEKey * (uTime * num * num2)
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(key))
return buf
}

View File

@ -5,21 +5,30 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"time"
"github.com/CPunch/GopenFusion/protocol" "github.com/CPunch/GopenFusion/protocol"
) )
const (
USE_E = iota
USE_FE
)
type Client struct { type Client struct {
server *Server server *Server
conn net.Conn conn net.Conn
key []byte e_key []byte
fe_key []byte
whichKey int
} }
func newClient(server *Server, conn net.Conn, key []byte) *Client { func newClient(server *Server, conn net.Conn) *Client {
return &Client{ return &Client{
server: server, server: server,
conn: conn, conn: conn,
key: key, e_key: []byte(protocol.DEFAULT_KEY),
whichKey: USE_E,
} }
} }
@ -41,7 +50,12 @@ func (client *Client) Send(data interface{}, typeID uint32) {
tmp = append(tmp, pkt.Buf...) tmp = append(tmp, pkt.Buf...)
// encrypt typeID & body // encrypt typeID & body
protocol.EncryptData(tmp, client.key) switch client.whichKey {
case USE_E:
protocol.EncryptData(tmp, client.e_key)
case USE_FE:
protocol.EncryptData(tmp, client.fe_key)
}
// write packet body // write packet body
if _, err := client.conn.Write(tmp); err != nil { if _, err := client.conn.Write(tmp); err != nil {
@ -50,6 +64,34 @@ func (client *Client) Send(data interface{}, typeID uint32) {
log.Printf("sent!") log.Printf("sent!")
} }
func (client *Client) Login(pkt *protocol.Packet) {
var loginPkt protocol.SP_CL2LS_REQ_LOGIN
pkt.Decode(&loginPkt)
log.Printf("Got packet: %#v", loginPkt)
// !! TODO
resp := &protocol.SP_LS2CL_REP_LOGIN_SUCC{
SZID: loginPkt.SZID,
ICharCount: 0,
ISlotNum: 1,
IPaymentFlag: 1,
IOpenBetaFlag: 0,
UISvrTime: uint64(time.Now().Unix()),
}
client.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC)
client.e_key = protocol.CreateNewKey(
resp.UISvrTime,
uint64(resp.ICharCount+1),
uint64(resp.ISlotNum+1),
)
client.fe_key = protocol.CreateNewKey(
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)),
uint64(loginPkt.IClientVerC),
1,
)
}
func (client *Client) ClientHandler() { func (client *Client) ClientHandler() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
@ -78,22 +120,14 @@ func (client *Client) ClientHandler() {
} }
// decrypt && grab typeID // decrypt && grab typeID
protocol.DecryptData(tmp[:sz], client.key) protocol.DecryptData(tmp[:sz], client.e_key)
typeID := int(binary.LittleEndian.Uint32(tmp[:4])) typeID := int(binary.LittleEndian.Uint32(tmp[:4]))
log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz) log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz)
pkt := protocol.NewPacket(tmp[4:sz]) pkt := protocol.NewPacket(tmp[4:sz])
switch typeID { switch typeID {
case protocol.P_CL2LS_REQ_LOGIN: case protocol.P_CL2LS_REQ_LOGIN:
var loginPkt protocol.SP_CL2LS_REQ_LOGIN client.Login(pkt)
pkt.Decode(&loginPkt)
log.Printf("Got packet: %#v", loginPkt)
client.Send(&protocol.SP_LS2CL_REP_LOGIN_FAIL{
IErrorCode: protocol.LOGIN_FAIL_EULA_ERROR,
SZID: loginPkt.SZID,
}, protocol.P_LS2CL_REP_LOGIN_FAIL)
default: default:
log.Printf("[WARN] unsupported packet ID: %x\n", typeID) log.Printf("[WARN] unsupported packet ID: %x\n", typeID)
} }

View File

@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"log" "log"
"net" "net"
"github.com/CPunch/GopenFusion/protocol"
) )
type Server struct { type Server struct {
@ -42,7 +40,7 @@ func (server *Server) Start() {
return return
} }
client := newClient(server, conn, []byte(protocol.E_KEY)) client := newClient(server, conn)
server.clients[client] = true server.clients[client] = true
go client.ClientHandler() go client.ClientHandler()
fmt.Printf("Client %p connected\n", client) fmt.Printf("Client %p connected\n", client)

View File

@ -5,7 +5,7 @@
Takes raw structures from a decompiled 'Assembly - CSharp.dll' from a main.unity3d fusionfall beta client, Takes raw structures from a decompiled 'Assembly - CSharp.dll' from a main.unity3d fusionfall beta client,
and transpiles them to gopenfusion's custom packet structure & tags. This requires a compiler installed, and transpiles them to gopenfusion's custom packet structure & tags. This requires a compiler installed,
since struct field padding is grabbed via the `offsetof()` C macro. Some manual rearranging of structures since struct field padding is grabbed via the `offsetof()` C macro. Some manual rearranging of structures
from the disassembled source might be needed. This script can also be used to generate c-style structures from the disassembled source might be needed. This script can also be modified to generate c-style structures
(because it already does!) (because it already does!)
usage: ./genstructs.py [IN.cs] > structs.go usage: ./genstructs.py [IN.cs] > structs.go
@ -173,10 +173,8 @@ class StructTranspiler:
def toCStyle(self) -> str: def toCStyle(self) -> str:
source = "typedef struct {\n" source = "typedef struct {\n"
for field in self.fields: for field in self.fields:
source += "\t%s %s;\n" % (field.ctype, field.cname) source += "\t%s %s;\n" % (field.ctype, field.cname)
source += "} %s;\n" % self.name source += "} %s;\n" % self.name
for field in self.fields: for field in self.fields:
@ -229,7 +227,6 @@ class StructTranspiler:
source += "\n}\n" source += "\n}\n"
return source return source
if __name__ == '__main__': if __name__ == '__main__':
inFilePath = sys.argv[1] inFilePath = sys.argv[1]
@ -245,6 +242,7 @@ if __name__ == '__main__':
except: except:
break break
# check for undefined types in structures and patch the sizes
for i in range(len(structs)): for i in range(len(structs)):
struct = structs[i] struct = structs[i]
f = struct.needsPatching() f = struct.needsPatching()
@ -252,10 +250,10 @@ if __name__ == '__main__':
name = struct.fields[f].type name = struct.fields[f].type
for s in structs: for s in structs:
if s.name == name: if s.name == name:
if struct.fields[f].size > 1: if struct.fields[f].size > 1: # was it an array?
structs[i].fields[f].type = ("[%d]" % struct.fields[f].size) + struct.fields[f].type structs[i].fields[f].type = ("[%d]" % struct.fields[f].size) + struct.fields[f].type
structs[i].fields[f].size *= s.size structs[i].fields[f].size *= s.size # field's size was set to 1 even if it wasn't an array
structs[i].fields[f].needsPatching = False structs[i].fields[f].needsPatching = False # mark done
f = struct.needsPatching() f = struct.needsPatching()
@ -272,10 +270,12 @@ if __name__ == '__main__':
compiler.compile(['tmp.c']) compiler.compile(['tmp.c'])
compiler.link_executable(['tmp.o'], 'tmp') compiler.link_executable(['tmp.o'], 'tmp')
# patch padding bytes
lines = subprocess.getoutput(['./tmp']).splitlines() lines = subprocess.getoutput(['./tmp']).splitlines()
for i in range(len(structs)): for i in range(len(structs)):
structs[i].populatePadding(lines[i].split(" ")) structs[i].populatePadding(lines[i].split(" "))
# emit structures
for struct in structs: for struct in structs:
print(struct.toGoStyle()) print(struct.toGoStyle())