Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added New Sections Related To Memory Management, Communication Security and Process Management #91

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Summary
* [Communication Security](communication-security/README.md)
* [HTTP/TLS](communication-security/http-tls.md)
* [WebSockets](communication-security/websockets.md)
* [gRPC](communication-security/grpc.md)
* [System Configuration](system-configuration/README.md)
* [Database Security](database-security/README.md)
* [Connections](database-security/connections.md)
Expand All @@ -32,6 +33,7 @@ Summary
* [Stored Procedures](database-security/stored-procedures.md)
* [File Management](file-management/README.md)
* [Memory Management](memory-management/README.md)
* [Process Management](process-management/README.md)
* General Coding Practices
* [Cross-Site Request Forgery](general-coding-practices/cross-site-request-forgery.md)
* [Regular Expressions](general-coding-practices/regular-expressions.md)
Expand Down
1 change: 1 addition & 0 deletions src/communication-security/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ The scope of this section covers the following communication channels:

* HTTP/HTTPS
* Websockets
* gRPC

[1]: https://www.owasp.org/index.php/Man-in-the-middle_attack
57 changes: 57 additions & 0 deletions src/communication-security/grpc-code/grpc_client_secured/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"context"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"log"
pb "pentest/grpc/samplebuff"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
Comment on lines +3 to +16

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: fmt

Suggested change
import (
"context"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"log"
pb "pentest/grpc/samplebuff"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
import (
"context"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
"log"
"time"
pb "pentest/grpc/samplebuff"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)


const (
defaultName = "Art Rosenbaum"
)

var (
addr = flag.String("addr", "localhost:10001", "Address of Server")
name = flag.String("name", defaultName, "Name to greet")
)

func main() {
flag.Parse()
b, _ := ioutil.ReadFile("../cert/ca.cert")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ioutil package is deprecated since go1.16. In this case we should use os.ReadFile instead.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's promote best practice of not skipping errors

Suggested change
b, _ := ioutil.ReadFile("../cert/ca.cert")
b, err := ioutil.ReadFile("../cert/ca.cert")
if err != nil {
log.Fatalf("Could read ca.cert: %v", err)
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anatolym Shouldn't the error message be "Couldn't read ca.cert: %v", instead?

cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
fmt.Println("credentials: failed to append certificates")
}

config := &tls.Config{
InsecureSkipVerify: false,
RootCAs: cp,
}

creds := credentials.NewTLS(config)
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(creds))
if err != nil {
log.Fatalf("Could not connect to server: %v", err)
}
defer conn.Close()
c := pb.NewSampleServiceClient(conn)

// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.Greet(ctx, &pb.SendMsg{Name: *name})
Comment on lines +51 to +52

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: fmt

Suggested change
defer cancel()
r, err := c.Greet(ctx, &pb.SendMsg{Name: *name})
defer cancel()
r, err := c.Greet(ctx, &pb.SendMsg{Name: *name})

if err != nil {
log.Fatalf("could not send message: %v", err)
}
log.Printf("Sending message: %s", r.GetMessage())
}
51 changes: 51 additions & 0 deletions src/communication-security/grpc-code/grpc_server_secured/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"net"

pb "pentest/grpc/samplebuff"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

var (
port = flag.Int("port", 10001, "The server port")
)

// server is used to implement sample.GreeterServer.
type server struct {
pb.UnimplementedSampleServiceServer
}
Comment on lines +20 to +23

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would suggest not embedding gRPC service interface to a structure like this. The issue is that this implicitly says the server struct implements the interface, and the program compiles with no issues. Now imaging we remove server.Greet method. The program should still compile, but now it panics in runtime when Greet is called. (it will panic because of nil receiver)

Instead, we can enforce interface implementation check on the compile time like this.

Suggested change
// server is used to implement sample.GreeterServer.
type server struct {
pb.UnimplementedSampleServiceServer
}
// Verify interface implementation on compile.
var _ pb.UnimplementedSampleServiceServer = (*server)(nil)
// server implements sample.GreeterServer interface.
type server struct {}

Note, the interface implementation check will be implicitly performed by compiler by the pb.RegisterSampleServiceServer(s, &server{}) call below. This is ok but explicit check with var _ ... is more obvious for a reader.


// Greet implements sample.GreeterServer
func (s *server) Greet(ctx context.Context, in *pb.SendMsg) (*pb.SendResp, error) {
log.Printf("Received msg: %v", in.GetName())
return &pb.SendResp{Message: "Hey " + in.GetName()}, nil
}

func main() {
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("Could not start the server: %v", err)
}

//Configuring the certificates
creds, err := credentials.NewServerTLSFromFile("../cert/service.pem", "../cert/service.key")

if err != nil {
log.Fatalf("TLS setup failed: %v", err)
}
Comment on lines +38 to +43

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: fmt

Suggested change
//Configuring the certificates
creds, err := credentials.NewServerTLSFromFile("../cert/service.pem", "../cert/service.key")
if err != nil {
log.Fatalf("TLS setup failed: %v", err)
}
// Configuring the certificates.
creds, err := credentials.NewServerTLSFromFile("../cert/service.pem", "../cert/service.key")
if err != nil {
log.Fatalf("TLS setup failed: %v", err)
}


s := grpc.NewServer(grpc.Creds(creds))
pb.RegisterSampleServiceServer(s, &server{})
log.Printf("Server started at: %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("Could not start the server: %v", err)
}
}
212 changes: 212 additions & 0 deletions src/communication-security/grpc-code/samplebuff/sample.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/communication-security/grpc-code/samplebuff/sample.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

option go_package = "github.com/pypalkar23/go-rpc-cis5209/sample";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, this name should be changed to something else like pentest/grpc/samplebuff?


package sample;


service SampleService {
rpc Greet (SendMsg) returns (SendResp);
}

message SendMsg {
string name = 1;
}

message SendResp{
string message = 1;
}
Loading