49ers win hard-fought game versus Packers